Ubuntu16.04安裝MySQL


  本篇教程在示例步驟中使用了以下版本的軟件。操作時,請您以實際軟件版本為准。

  • 操作系統: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 下載與安裝

  1. 更新 apt# apt-get update
  2. 這里使用的是 apt 軟件包安裝:
    • 安裝 mysql-server# apt-get install mysql-server(安裝的過程中會提示您為 root 用戶設置密碼、確認密碼並按下回車 ok 即可。
  3. 測試 MySQL 是否安裝成功:# netstat -tap | grep mysql
    • 打印類似信息為安裝成功: tcp 0 0 localhost:mysql *:* LISTEN 6902/mysqld
  4. 使用 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 保存並關閉文件。
  5. 重啟 MySQL:service mysql restart

二、修改 MySQL 編碼為 utf8

  1. 登陸 MySQL:mysql -uroot -p,並輸入登陸密碼
  2. 查看 MySQL 編碼:\s(參見附錄1)show variables like '%char%';(參見附錄2)
  3. 打開配置文件 vim /etc/mysql/mysql.conf.d/mysqld.cnf
    • i 鍵進入編輯模式
    • [mysqld]lc-messages-dir = /usr/share/mysql 的后面添加 character_set_server=utf8
    • 按 Esc 鍵退出編輯模式,輸入 :wq 保存並關閉文件。
  4. 打開配置文件 vim /etc/mysql/conf.d/mysql.cnf
    • i 鍵進入編輯模式
    • [mysql] 后面添加 default-character-set=utf8
    • 按 Esc 鍵退出編輯模式,輸入 :wq 保存並關閉文件。
  5. 重啟 MySQL:service mysql restart
  6. 登陸 MySQL 后使用 \sshow variables like '%char%'; 查看修改結果。

三、配置 MySQL 遠程連接

  注意:在 MySQL 中所執行的語句 / 命令最好都以 ; 結束;遇到使用 utf8 編碼的地方則需要使用 utf8,而不是 utf-8

  1. 設置 MySQL 允許遠程訪問:
    • 首先編輯配置文件:vim /etc/mysql/mysql.conf.d/mysqld.cnf
    • i 鍵進入編輯模式
    • 注釋掉 # bind-address = 127.0.0.1
    • 按 Esc 鍵退出編輯模式,輸入 :wq 保存並關閉文件。
  2. 登陸 MySQL,查看 MySQL 用戶:select host,user from mysql.user;
  3. 創建新用戶:create user '用戶名'@'%' identified by '密碼';
  4. 授權遠程連接 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\qexit;
    • 使用新密碼重新登陸 MySQL,繼續進行授權操作。
  5. 刷新權限表,使配置生效:flush privileges;
  6. 重啟 MySQL:service mysql restart
  7. 添加監聽端口號 3306iptables -I INPUT -p tcp --dport 3306 -j ACCEPT
    • 查看是否被監聽:iptables -L -n
    • 設置防火牆打開 3306 端口:ufw allow 3306
  8. 最重要的一步,登錄阿里雲 —> 控制台 —> 雲服務器ECS —> 網絡和安全 —> 安全組,在入方向點擊配置規則進行配置,3306 端口是訪問服務器 MySQL 的,沒有的話就添加規則,端口范圍選擇 3306/3306,授權策略為 允許,授權協議為 MySQL(3306),授權對象設置為 0.0.0.0/0,允許所有外部 IP 訪問。
    • 查看 3306 端口是否被監聽:netstat -an | grep 3306
    • 顯示 tcp6 0 0 :::3306 :::* LISTEN
  9. 遠程連接 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>


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM