2012-03-05 124 views
0

我使用的是libmysql C API,mysql_real_connect調用只適用於我使用主機的真實IP地址 - 除localhost以外。如果我使用localhost作爲東道主我得到 以下錯誤:libmysql mysql_real_connect失敗本地主機,但與本地IP地址

ERROR 2003 (HY000): Can't connect to MySQL server on 'localhost' (0)

認證應該沒問題,因爲所使用的用戶從本地主機和任何主機也是所有權限。

順便說這是在Windows 7

+1

你的mysql服務器可能並沒有監聽所有的網絡接口,只是'外部'接口。 – trojanfoe 2012-03-05 14:11:40

+0

奇怪的是,當我將localhost更改爲127.0.0.1時,它開始工作。 – 2012-03-06 12:38:01

回答

1

我有同樣的問題和解決的辦法就是卸載並禁用IPv6支持,因爲如果我有IPv6支持啓用那麼ping localhost命令返回:: 1:而不是127.0.0.1,因爲我已經寫入etc \ hosts文件,並且使用libmysql編寫的使用C編寫的mysql連接程序在作爲主機參數連接到mysql_real_connect()時提供「localhost」。現在,在禁用IPv6後,兩個選項(「localhost」或「127.0.0.1」)都可用。

P.S .:我只在Windows XP SP3 Pro捷克版上試過這個「解決方案」。在Windows 7和Linux中,我不試試這個。

0

要麼你的DNS被打破(是否ping localhost按預期方式工作?) 或方式「localhost」的經過特殊處理的XAMPP安裝的問題。

The value of host may be either a host name or an IP address. If host is NULL or the string "localhost", a connection to the local host is assumed. For Windows, the client connects using a shared-memory connection, if the server has shared-memory connections enabled. Otherwise, TCP/IP is used.

看到http://dev.mysql.com/doc/refman/5.0/en/mysql-real-connect.html

0
  1. 檢查,如果在你的/ etc/hosts文件時定義的類似的東西:127.0.0.1本地主機
  2. 檢查MySQL是如何結合其端口:

    root @ dam2k:〜#netstat -natp | grep的3306 TCP 0 0 0.0.0.0:3306 0.0.0.0:* LISTEN 2215/mysqld的

+0

啊,如果你正在使用windoze,你可以檢查C:\ windows \ system32 \ drivers \ etc \ hosts中是否存在「127.0.0.1 localhost」,你可以使用「netstat -na」來檢查端口 – dAm2K 2012-03-06 12:51:57