netstat -anlp | grep mysql
tcp 0 0 0.0.0.0:3306 0.0.0.0:* LISTEN 638/mysqld
# 0.0.0.0 表示監聽所有網路介面,對外開放,不安全
iptables -A INPUT -p tcp -s $ANY --dport 3306 -j DROP
# 實測無效,因 MySQL 綁定在 0.0.0.0,防火牆規則可能被繞過或順序問題
# 編輯 /etc/init.d/mysql (或 /etc/init.d/mysqld)
# 找到啟動行:
$bindir/mysqld_safe --datadir=$datadir --pid-file=$pid_file >/dev/null 2>&1 &
# 修改為:
$bindir/mysqld_safe --datadir=$datadir --pid-file=$pid_file --bind-address=127.0.0.1 &
/etc/init.d/mysql restart
netstat -l
# 預期結果:
tcp 0 0 localhost:mysql *:* LISTEN
# 或
tcp 0 0 localhost.localdomain:mysql *:* LISTEN
--bind-address=127.0.0.1 讓 MySQL 僅監聽回環介面localhost 或 127.0.0.1 仍可正常連線[mysqld]
bind-address = 127.0.0.1
修改後重啟服務同樣生效,且不需動到啟動腳本,較易維護。
ssh -L 3306:127.0.0.1:3306 user@servernetstat -anlp | grep 3306 驗證綁定狀態