首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫

MySQL 綁定特定 IP 監聽:bind-address 設定兩種方式

     編輯:MySQL 資料庫相關知識

為安全考量,生產環境 MySQL 應僅監聽必要介面(如 127.0.0.1 或內網 IP)。本文說明透過 my.cnf 與啟動腳本兩種方式設定 bind-address。

方式一:my.cnf 設定檔(推薦,永久生效)

[mysqld]
bind-address = 127.0.0.1

或指定內網 IP:

bind-address = 192.168.1.100

修改後重啟 MySQL:

systemctl restart mysqld
# 或
service mysql restart

方式二:啟動腳本參數(臨時/舊版相容)

  1. 編輯 /etc/init.d/mysql
  2. 找到 start) 區塊的 mysqld_safe 行
  3. 加入 --bind-address=127.0.0.1
$bindir/mysqld_safe --datadir=$datadir \
    --pid-file=$pid_file \
    --bind-address=127.0.0.1 &

重啟:/etc/init.d/mysql restart

驗證監聽介面

netstat -tlnp | grep mysql
# 或
ss -tlnp | grep mysql

預期輸出:

tcp  0  0 127.0.0.1:3306  0.0.0.0:*  LISTEN  1234/mysqld

✅ 僅顯示 127.0.0.1:3306 表示成功限制監聽介面。

⚠️ 注意事項

  • 設定 127.0.0.1 後,**外部機器無法直接連線**,需透過 SSH Tunnel 或應用同機部署
  • 若需多 IP 監聽,MySQL 8.0+ 支援 bind-address = * 或多行 bind-address
  • Docker 環境需額外注意網路模式(host vs bridge)
  • 防火牆(iptables/firewalld)仍建議配合限制 3306 埠存取
  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved