2012-03-05 204 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