錯誤代碼 10060 (ETIMEDOUT) 表示連線嘗試超時,即客戶端無法在預期時間內與 MySQL 伺服器建立 TCP 連線。這通常不是認證失敗,而是**網路層面的阻隔**。本文提供系統化排查步驟,快速定位並解決問題。
使用 Navicat、Workbench 或命令列連線遠端 MySQL 時,報錯:
Can't connect to MySQL server on 'x.x.x.x' (10060)

bind-address)user.host 欄位)登入 MySQL,查詢用戶 Host 欄位:
SELECT User, Host FROM mysql.user WHERE User = 'your_user';

Host = '%':允許任意 IP 連線 ✅Host = 'localhost' 或 '127.0.0.1':僅允許本機連線 ❌ 需修改為 '%' 或具體客戶端 IP。RENAME USER 'user'@'localhost' TO 'user'@'%';
FLUSH PRIVILEGES;
bind-address編輯配置檔(/etc/my.cnf 或 /etc/mysql/mysql.conf.d/mysqld.cnf):
[mysqld]
# bind-address = 127.0.0.1 ← 註釋掉或改為 0.0.0.0
bind-address = 0.0.0.0

修改後必須重啟 MySQL:systemctl restart mysqld。
Linux (firewalld/iptables):
# firewalld
firewall-cmd --zone=public --add-port=3306/tcp --permanent
firewall-cmd --reload
# iptables
iptables -A INPUT -p tcp --dport 3306 -j ACCEPT
service iptables save

雲廠商安全組(AWS SG / 阿里雲安全組 / 華為雲安全組):
Windows Server 防火牆:
netsh advfirewall firewall add rule name="MySQL 3306" dir=in action=allow protocol=TCP localport=3306
在客戶端機器執行:
# Telnet 測試埠連通性
telnet 3306
# 或 nc (netcat)
nc -zv 3306
# 或 PowerShell
Test-NetConnection -ComputerName -Port 3306
# CentOS 7+
systemctl stop firewalld
# Ubuntu
ufw disable

若關閉防火牆後即可連線,確認是防火牆規則問題,按上述規則放行埠 3306 並重新啟用防火牆。
| 原因 | 檢查/解決 |
|---|---|
| MySQL 服務未啟動 | systemctl status mysqld 確認 running |
| 埠被佔用/衝突 | netstat -antp | grep 3306 或 ss -antp | grep 3306 |
| SELinux 阻擋 | setsebool -P mysql_connect_any 1 或暫時 setenforce 0 |
| 雲端私有網路 (VPC) 路由/ACL 錯誤 | 檢查子網路 ACL、路由表、NAT 閘道配置 |
| 客戶端 VPN/代理干擾 | 關閉 VPN/代理嘗試直連 |
bind-address = 0.0.0.0 (或具體內網 IP)'%' 或客戶端 IPtelnet ip 3306 連通依序核對,99% 的 10060 錯誤可在前 3 步解決。生產環境務必**僅開放必要 IP 存取**,避免直接暴露 3306 至公網。