vim /etc/mysql/my.cnf
# 註釋或修改
# bind-address = 127.0.0.1
# 推薦改為
bind-address = 0.0.0.0
0.0.0.0 表示監聽所有網卡;若只需特定內網 IP,可填具體內網 IP。
/etc/init.d/mysql restart
# 或
systemctl restart mysqld
mysql -uroot -p
GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY 'your_strong_password' WITH GRANT OPTION;
FLUSH PRIVILEGES;
% 表示任意主機;生產建議限制為具體 IP 段(如 'root'@'192.168.1.%')。WITH GRANT OPTION 允許該用戶再授權,謹慎給予。# iptables
iptables -A INPUT -p tcp --dport 3306 -j ACCEPT
# firewalld
firewall-cmd --zone=public --add-port=3306/tcp --permanent
firewall-cmd --reload
# /etc/hosts.allow(TCP Wrappers,若啟用)
echo "mysqld: ALL" >> /etc/hosts.allow
# 客戶端機器
mysql -h -u root -p
REQUIRE SSL 加密傳輸。三步配置,安全開放遠程訪問。