1.開發背景
? ? ? ? 開發項目需要使用到數據庫,相對于輕量級的 SQLite,MySql 相對復雜一下,但是可以遠程訪問,還是比較舒服的。
2.開發需求
? ? ? ? Ubuntu 安裝 MySql 服務端,Window 客戶端訪問 Ubuntu 數據庫。
3.開發環境
? ? ? ? Ubuntu20.04 + Window10
4.實現步驟
4.1 安裝 MySql
4.1.1 安裝軟件
# Ubuntu MySQl數據庫服務端
sudo apt install mysql-server
4.1.2 查看狀態
# 查看狀態
sudo systemctl status mysql
4.1.3 啟停?MySql
# 啟動 mySql
sudo systemctl start mysql# 停止 mySql
sudo systemctl stop mysql
4.1.4 設置自啟動
# 開機自啟動
sudo systemctl enable mysql
4.2 配置 MySql
sudo mysql_secure_installation
secure enough. Would you like to setup VALIDATE PASSWORD component?Press y|Y for Yes, any other key for No: nRemove anonymous users? (Press y|Y for Yes, any other key for No) : y
Success.Disallow root login remotely? (Press y|Y for Yes, any other key for No) : y
Success.Remove test database and access to it? (Press y|Y for Yes, any other key for No) : y- Dropping test database...
Success.Reload privilege tables now? (Press y|Y for Yes, any other key for No) : y
Success.All done!
? ? ? ? 截取關鍵配置,除了秘鑰強保護不需要(測試,為了方便為主),其他的都是默認,根據自己需求配置即可。
4.3 登錄 MySql
# root 用戶進入數據庫 測試機默認密碼也是 root
sudo mysql -u root -p
4.4 遠程訪問?MySql
4.4.1 添加用戶
? ? ? ? 盡量不通過 root 訪問,這里創建用戶?yangjinghui
# 創建用戶 yangjinghui 密碼 root
CREATE USER 'yangjinghui'@'%' IDENTIFIED WITH mysql_native_password BY 'root';# 查看用戶信息
SELECT user, host FROM mysql.user;# 用戶授權 所有數據庫
GRANT ALL PRIVILEGES ON *.* TO 'yangjinghui'@'%';# 用戶授權 個別數據庫
#GRANT ALL PRIVILEGES ON mydb.* TO 'yangjinghui'@'%';# 權限生效
FLUSH PRIVILEGES;
4.4.2 聯網配置
# 修改配置 bind-address 修改 禁用 ssl
sudo vi /etc/mysql/mysql.conf.d/mysqld.cnf
#bind-address = 127.0.0.1
bind-address = 0.0.0.0
ssl=0# 防火墻通道
sudo ufw allow 3306# 重啟 mysql
sudo systemctl restart mysql