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

Windows 7/8 連線本地 MySQL 極慢 (1 秒延遲) 根因與解決:IPv6 localhost 陷阱

     編輯:MySQL 資料庫相關知識

本地程式連線 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 位址

解決方案

方案一:應用程式連線字串改用 127.0.0.1 (最簡單、立即生效)

# 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'

方案二:修改 hosts 檔強制綁定 IPv4

# C:\Windows\System32\drivers\etc\hosts
127.0.0.1 localhost
# ::1 localhost  ← 註解或刪除此行

存檔後 ipconfig /flushdns 重新解析。

方案三:MySQL 同時監聽 IPv6 (my.ini)

[mysqld]
bind-address = ::
# 或雙協定
bind-address = *

需 MySQL 5.5.3+,重啟服務生效。

補充:Linux 同理

/etc/hosts
127.0.0.1 localhost
# ::1 localhost  註解

或確認 MySQL bind-address = 0.0.0.0 (全介面) 或 :: (IPv6)。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved