本地程式連線 localhost 耗時 ~1 秒,改用 127.0.0.1 瞬間連線。根因:Windows 7/8 預設 localhost 解析為 IPv6 ::1,MySQL 預設僅監聽 IPv4 127.0.0.1,IPv6 連線超時後才回退 IPv4。
C:\> ping localhost 回覆來自 ::1: 時間<1ms ← IPv6 位址
# PHP PDO $dsn = 'mysql:host=127.0.0.1;dbname=test;charset=utf8mb4'; # JDBC jdbc:mysql://127.0.0.1:3306/test?useSSL=false # Python host='127.0.0.1'
# C:\Windows\System32\drivers\etc\hosts 127.0.0.1 localhost # ::1 localhost ← 註解或刪除此行
存檔後 ipconfig /flushdns 重新解析。
[mysqld] bind-address = :: # 或雙協定 bind-address = *
需 MySQL 5.5.3+,重啟服務生效。
/etc/hosts 127.0.0.1 localhost # ::1 localhost 註解
或確認 MySQL bind-address = 0.0.0.0 (全介面) 或 :: (IPv6)。