本篇教程在示例步驟中使用了以下版本的軟件。操作時,請您以實際軟件版本為准。
- 操作系統:
Ubuntu 16.04.3 LTS (GNU/Linux 4.4.0-105-generic x86_64)
- MySQL 版本:
MySQL 5.7.24-0ubuntu0.16.04.1 (Ubuntu)
- MySQL 可視化工具:
Navicat for MySQL 10.1.7-enterprise
一、MySQL 下載與安裝
- 更新
apt
:# apt-get update
- 這里使用的是
apt
軟件包安裝:- 安裝
mysql-server
:# apt-get install mysql-server
(安裝的過程中會提示您為root
用戶設置密碼、確認密碼並按下回車ok
即可。
- 安裝
- 測試 MySQL 是否安裝成功:
# netstat -tap | grep mysql
- 打印類似信息為安裝成功:
tcp 0 0 localhost:mysql *:* LISTEN 6902/mysqld
- 打印類似信息為安裝成功:
- 使用
root
用戶登陸 MySQL:mysql -uroot -p
- 輸入密碼:
輸入剛才安裝過程中設置的密碼
- 到這一步你可能會遇到這樣的錯誤:
ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: YES)
- 打開
cat /etc/mysql/my.cnf
后你會發現該配置文件引入了!includedir /etc/mysql/conf.d/
和!includedir /etc/mysql/mysql.conf.d/
- 打開配置文件
vim /etc/mysql/mysql.conf.d/mysqld.cnf
- 按
i
鍵進入編輯模式 - 在
[mysqld]
后面任意一行添加skip-grant-tables
用來跳過密碼驗證的過程 - 按 Esc 鍵退出編輯模式,輸入
:wq
保存並關閉文件。
- 輸入密碼:
- 重啟 MySQL:
service mysql restart
二、修改 MySQL 編碼為 utf8
- 登陸 MySQL:
mysql -uroot -p
,並輸入登陸密碼 - 查看 MySQL 編碼:
\s
(參見附錄1)或show variables like '%char%';
(參見附錄2) - 打開配置文件
vim /etc/mysql/mysql.conf.d/mysqld.cnf
- 按
i
鍵進入編輯模式 - 在
[mysqld]
中lc-messages-dir = /usr/share/mysql
的后面添加character_set_server=utf8
- 按 Esc 鍵退出編輯模式,輸入
:wq
保存並關閉文件。
- 按
- 打開配置文件
vim /etc/mysql/conf.d/mysql.cnf
- 按
i
鍵進入編輯模式 - 在
[mysql]
后面添加default-character-set=utf8
- 按 Esc 鍵退出編輯模式,輸入
:wq
保存並關閉文件。
- 按
- 重啟 MySQL:
service mysql restart
- 登陸 MySQL 后使用
\s
或show variables like '%char%';
查看修改結果。
三、配置 MySQL 遠程連接
注意:在 MySQL 中所執行的語句 / 命令最好都以 ;
結束;遇到使用 utf8
編碼的地方則需要使用 utf8
,而不是 utf-8
。
- 設置 MySQL 允許遠程訪問:
- 首先編輯配置文件:
vim /etc/mysql/mysql.conf.d/mysqld.cnf
- 按
i
鍵進入編輯模式 - 注釋掉
# bind-address = 127.0.0.1
- 按 Esc 鍵退出編輯模式,輸入
:wq
保存並關閉文件。
- 首先編輯配置文件:
- 登陸 MySQL,查看 MySQL 用戶:
select host,user from mysql.user;
- 創建新用戶:
create user '用戶名'@'%' identified by '密碼';
- 授權遠程連接 MySQL 的用戶:
grant all privileges on *.* to '用戶名'@'%' identified by '密碼';
%
表示所有的電腦都可以連接,也可以設置某個IP地址
進行連接- 更新用戶密碼:
update user set password=password("新密碼") where user="root";
- 更改權限過程中可能會出現的問題:
ERROR 1819 (HY000): Your password does not satisfy the current policy requirements
,提示密碼不符合要求,這應該是安裝的時候,您選擇了密碼強度為Strong
。 - 這里使用更改密碼策略:
set global
更改密碼策略:set global validate_password_policy=0;
- 設置密碼長度:
set global validate_password_length=6;
- 更新密碼:set password=Password('123456');
- 退出 MySQL:
quit
或\q
或exit;
- 使用新密碼重新登陸 MySQL,繼續進行授權操作。
- 刷新權限表,使配置生效:
flush privileges;
- 重啟 MySQL:
service mysql restart
- 添加監聽端口號
3306
:iptables -I INPUT -p tcp --dport 3306 -j ACCEPT
- 查看是否被監聽:
iptables -L -n
- 設置防火牆打開
3306
端口:ufw allow 3306
- 查看是否被監聽:
- 最重要的一步,登錄阿里雲 —> 控制台 —> 雲服務器ECS —> 網絡和安全 —> 安全組,在入方向點擊配置規則進行配置,
3306
端口是訪問服務器 MySQL 的,沒有的話就添加規則,端口范圍選擇3306/3306
,授權策略為允許
,授權協議為MySQL(3306)
,授權對象設置為0.0.0.0/0
,允許所有外部 IP 訪問。- 查看 3306 端口是否被監聽:
netstat -an | grep 3306
- 顯示
tcp6 0 0 :::3306 :::* LISTEN
- 查看 3306 端口是否被監聽:
- 遠程連接 MySQL 測試,這里偷懶使用了 Windows 的命令行工具(參見附錄3)
四. 參考鏈接
附錄 1
- 查看 MySQL 版本信息與編碼:
\s
mysql> \s
--------------
mysql Ver 14.14 Distrib 5.7.24, for Linux (x86_64) using EditLine wrapper
Connection id: 3
Current database:
Current user: root@localhost
SSL: Not in use
Current pager: stdout
Using outfile: ''
Using delimiter: ;
Server version: 5.7.24-0ubuntu0.16.04.1 (Ubuntu)
Protocol version: 10
Connection: Localhost via UNIX socket
Server characterset: latin1
Db characterset: latin1
Client characterset: utf8
Conn. characterset: utf8
UNIX socket: /var/run/mysqld/mysqld.sock
Uptime: 16 sec
Threads: 1 Questions: 5 Slow queries: 0 Opens: 107 Flush tables: 1 Open tables: 26 Queries per second avg: 0.312
--------------
附錄 2
- 查看 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)
- 設置 MySQL 編碼為
utf8
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)
附錄 3
- CMD 遠程訪問 Ubuntu 16.04 下 MySQL 測試:
C:\Users\wumz>mysql -h 公網IP地址 -u 授權用戶 -p
Enter password: ********
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 4
Server version: 5.7.24-0ubuntu0.16.04.1 (Ubuntu)
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql>