MySQL 預設僅允許本地連接 (bind-address=127.0.0.1)。開啟遠程訪問只需三步:建立支援遠程主機的用戶、註釋 bind-address、重啟服務。附帶密碼修改指令與安全加固建議。
GRANT ALL ON *.* TO 'username'@'%' IDENTIFIED BY 'strong_password';
FLUSH PRIVILEGES;
安全建議:生產環境將 '%' 替換為具體應用伺服器 IP 段 (如 '10.0.0.%')。
sudo vim /etc/mysql/my.cnf
# 找到 bind-address = 127.0.0.1 在行首加 # 註釋
# bind-address = 127.0.0.1
或改為 bind-address = 0.0.0.0 監聽所有介面。
sudo /etc/init.d/mysql restart
# 或 systemctl restart mysql
# 客戶端測試
mysql -h server_ip -u username -p
UPDATE mysql.user SET Password=PASSWORD('new_password') WHERE User='username';
FLUSH PRIVILEGES;
GRANT SELECT,INSERT,UPDATE,DELETE ON db.* TO 'app'@'10.0.0.%'validate_password 元件,長度≥12、含大小寫數字特殊字符iptables -A INPUT -p tcp --dport 3306 -s 10.0.0.0/24 -j ACCEPT 僅放行應用網段require_secure_transport=ON 強制加密傳輸audit_log 記錄登入、DDL、DML 關鍵操作建用戶@% → 註釋 bind-address → 重啟服務 三步開啟遠程訪問。生產環境務必配合「IP 白名單 + 最小權限 + SSL + 防火牆 + 審計」五重防護,將攻擊面壓縮至最小。