Ubuntu 系統,MySQL 默認綁定 127.0.0.1 僅允許本地訪問,root 無密碼。
$ mysql -u root
mysql> GRANT ALL PRIVILEGES ON *.* TO 'root'@'localhost' IDENTIFIED BY 'StrongPass123!';
FLUSH PRIVILEGES;
建議: 密碼 ≥12 位,大小寫+數字+特殊字符。
mysql> CREATE DATABASE xoops;
mysql> GRANT ALL PRIVILEGES ON `xoops`.* TO 'xoops_root'@'localhost' IDENTIFIED BY 'AnotherStrongPass!';
FLUSH PRIVILEGES;
業務代碼使用 xoops_root 連接,權限僅限 xoops 庫,root 僅作緊急管理。
GRANT ALL PRIVILEGES ON `xoops`.* TO 'xoops_root'@'%' IDENTIFIED BY 'AnotherStrongPass!';/etc/mysql/my.cnf,找到 bind-address = 127.0.0.1,註釋或改為 0.0.0.0:# bind-address = 127.0.0.1bind-address = 0.0.0.0sudo service mysql restart
sudo ufw allow from 192.168.1.0/24 to any port 3306
# 僅允許應用服務器網段訪問
mysql_secure_installation 交互式完成:移除匿名用戶、禁止 root 遠程登入、刪除 test 庫、刷新權限。skip-name-resolve 禁用 DNS 解析,授權表僅用 IP。require_secure_transport=ON。mysql.user、mysql.db 權限表。