[MySQL] Ubuntu 20.04 LTS 安裝 MySQL Server
開發環境幾乎都已經改用 docker 在跑 MySQL 了, 偶爾還是會遇到需要在主機 / VM 上安裝 MySQL 的情況. 隨著版本迭代, 過去設定方式雖不見得不適用, 但可能有更建議的設定工具, 簡單紀錄.
本文主要參考 [教學][Ubuntu 架站] 在 Ubuntu 20.04 上安裝 MySQL Server | 優程式, 並補充一些問題排除
安裝 MySQL
除非要安裝其他社群維護 (Ex: mariadb) 或特別版本的 MySQL (Ex: AliSQL...), 否則在 Ubuntu 20.04 上安裝 MySQL, 直接透過 apt 安裝即可.
sudo apt update && sudo apt install mysql-server
當提示安裝 MySQL 套件時, 按 y
ENTER
確定安裝.
確認服務正常
MySQL 完成後, 可以檢查 MySQL 服務是否正在運行.
sudo service mysql status
服務正常運行的話, 會看到如下所示的綠色 Active 狀態.
● mysql.service - MySQL Community Server
Loaded: loaded (/lib/systemd/system/mysql.service; enabled; vendor preset: enabled)
Active: active (running) since Wed 2021-09-08 07:16:49 UTC; 18s ago
Main PID: 32713 (mysqld)
Status: "Server is operational"
Tasks: 38 (limit: 4705)
Memory: 356.6M
CGroup: /system.slice/mysql.service
└─32713 /usr/sbin/mysqld
Sep 08 07:16:48 ubuntu-20 systemd[1]: Starting MySQL Community Server...
Sep 08 07:16:49 ubuntu-20 systemd[1]: Started MySQL Community Server.
設定安全性(Security)
mysql_secure_installation
是官方建議為 MySQL Server 配置安全性的小工具.
sudo mysql_secure_installation
如果已經有設置了 root 密碼, 會提示在此處輸入密碼.
驗證密碼插件
Securing the MySQL server deployment.
Connecting to MySQL using a blank password.
VALIDATE PASSWORD PLUGIN can be used to test passwords
and improve security. It checks the strength of password
and allows the users to set only those passwords which are
secure enough. Would you like to setup VALIDATE PASSWORD plugin?
Press y|Y for Yes, any other key for No:
是否要設定驗證密碼插件 (VALIDATE PASSWORD PLUGIN). 除非想要強制執行嚴格的密碼規則, 不然這並不是真正需要的.
如果不想設置驗證密碼插件的話, 直接按 ENTER
.
root 密碼
如果沒有設置過 root 密碼, 會要求設置密碼:
Please set the password for root here.
New password:
Re-enter new password:
如果已有 root 密碼, 會詢問是否更改密碼.
Using existing password for root.
Change the password for root ? ((Press y|Y for Yes, any other key for No) :