Now you can Subscribe using RSS

Submit your Email

2017/10/15

Ubuntu Install MySQL

asd
MySQL 安裝方法很簡單
在 Terminal 下指令進行安裝
$sudo apt-get install mysql-server
*安裝過程會要你輸入 root 預設密碼
安裝完後 mysql 服務就會自動開啟
使用以下指令可以確認 mysql 狀態
查看 mysql 進程
$ps -aux|grep mysql
mysql     1015  0.0  1.7 1246820 145988 ?      Ssl  08:26   0:10 /usr/sbin/mysqld
jrtou    11971  0.0  0.0  21572  1088 pts/1    S+   15:33   0:00 grep --color=auto mysql
檢查 mysql 佔用 port 狀態,預設是3306
$netstat -nlt|grep 3306
tcp        0      0 0.0.0.0:3306            0.0.0.0:*               LISTEN 

mysql 開啟關閉與重新啟動服務指令如下
$sudo /etc/init.d/mysql
sudo: unable to resolve host jrtou-ms-7680
[sudo] password for jrtou: 
Usage: /etc/init.d/mysql start|stop|restart|reload|force-reload|status
看它提示的指令參數應該就很好懂,在 /etc/init.d/mysql []
[]中放置 start,stop.restart 參數就可以控制 服務開關
查看 mysql service 狀態也可以使用 service 指令進行查看
$service mysql status
● mysql.service - MySQL Community Server
   Loaded: loaded (/lib/systemd/system/mysql.service; enabled; vendor preset: en
   Active: active (running) since 一 2017-10-16 08:26:56 CST; 7h ago
  Process: 1016 ExecStartPost=/usr/share/mysql/mysql-systemd-start post (code=ex
  Process: 994 ExecStartPre=/usr/share/mysql/mysql-systemd-start pre (code=exite
 Main PID: 1015 (mysqld)
   CGroup: /system.slice/mysql.service
           └─1015 /usr/sbin/mysqld

 十  16 08:26:55 jrtou-ms-7680 systemd[1]: Starting MySQL Community Server...
 十  16 08:26:56 jrtou-ms-7680 systemd[1]: Started MySQL Community Server.

輸入以下指令登入 mysql
$mysql -u root -p
root 是預設的帳號,輸入完後會要你輸入密碼也就是安裝過程中設定的密碼
登入後會出現以下資訊
$mysql -u root -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 8
Server version: 5.7.19-0ubuntu0.16.04.1 (Ubuntu)

Copyright (c) 2000, 2017, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
成功登入之後先進行一些基本設定
輸入以下指令可以查看 mysql 編碼設定
mysql> show variables like '%char%';
顯示如下
mysql> show variables like '%char%';
+--------------------------+----------------------------+
| Variable_name            | Value                      |
+--------------------------+----------------------------+
| character_set_client     | utf8                       |
| character_set_connection | utf8                       |
| character_set_database   | latin1                     |
| character_set_filesystem | binary                     |
| character_set_results    | utf8                       |
| character_set_server     | latin1                     |
| character_set_system     | utf8                       |
| character_sets_dir       | /usr/share/mysql/charsets/ |
+--------------------------+----------------------------+
8 rows in set (0.00 sec)
可以發現有些設定並不是 utf8 這樣在中文顯示的時候會產生亂碼情況
所以我們要改一下 /etc/mysql/my.cnf 設定檔
在 etc 內的設定檔是屬於 global 設定,複製到 /home/user/下就可以依照使用者分別設定
我們為了方便所以就設定程 global
$sudo vim /etc/mysql/my.cnf
在文件內輸入以下內容
#set databases encode to utf8
[client]
default-character-set=utf8
[mysqld]
character-set-server=utf8
collation-server=utf8_general_ci
#set databases connect ip
bind-address=0.0.0.0
[mysql]
default-character-set=utf8
順便設置一下連線的 ip 位置
如果要使用外網連線只要更改 ip 就可以從外部連線近來
更新完設定檔之後重起 mysql server
sudo /etc/init.d/mysql restart
重新啟動之後在登入一次 mysql 查看一下
$mysql -u root -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 5
Server version: 5.7.19-0ubuntu0.16.04.1 (Ubuntu)

Copyright (c) 2000, 2017, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> show variables like '%char%';
+--------------------------+----------------------------+
| Variable_name            | Value                      |
+--------------------------+----------------------------+
| character_set_client     | utf8                       |
| character_set_connection | utf8                       |
| character_set_database   | utf8                       |
| character_set_filesystem | binary                     |
| character_set_results    | utf8                       |
| character_set_server     | utf8                       |
| character_set_system     | utf8                       |
| character_sets_dir       | /usr/share/mysql/charsets/ |
+--------------------------+----------------------------+
8 rows in set (0.00 sec)
可以看到設定已經更改了
然後退出 mysql 指令查看 port 監聽狀態
#這是未更改的時候
$ netstat -nlt|grep 3306
tcp        0      0 127.0.0.1:3306          0.0.0.0:*               LISTEN
#更改開啟外網連線
$ netstat -nlt|grep 3306
tcp        0      0 0.0.0.0:3306            0.0.0.0:*               LISTEN
這樣就可以開始使用 mysql 資料庫了

0 意見:

張貼留言

Coprights @ 2016, Blogger Templates Designed By Templateism | Templatelib