我正在使用 libmysql C API,该mysql_real_connect
调用仅在我使用主机的真实 IP 地址时才有效 - 不是localhost
. 如果我localhost
用作主机,我会收到以下错误:
错误 2003 (HY000): 无法连接到 'localhost' (0) 上的 MySQL 服务器
身份验证应该没问题,因为使用的用户拥有本地主机和任何主机的所有权限。
顺便说一句,它是 Windows 7 上的 XAMPP 安装。
I had the same problem and the solution is to uninstall and disable IPv6 support, because if I had IPv6 support enabled then ping localhost
command returns ::1: instead 127.0.0.1 as i have written in etc\hosts file, and sample mysql connect program written in C using libmysql do not connect when as host parameter to mysql_real_connect() i supplied "localhost". Now, after disabling IPv6 works both options ("localhost" or "127.0.0.1").
P.S.: I have tried this "solution" only in Windows XP SP3 Pro Czech. In Windows 7 and Linux I don't try this.
您的 DNS 已损坏(是否ping localhost
按预期工作?)或“本地主机”被特殊处理的方式是问题所在。
host 的值可以是主机名或 IP 地址。如果 host 为 NULL 或字符串“localhost”,则假定连接到本地主机。对于 Windows,如果服务器启用了共享内存连接,则客户端使用共享内存连接进行连接。否则,使用 TCP/IP。
见http://dev.mysql.com/doc/refman/5.0/en/mysql-real-connect.html
检查mysql如何绑定其端口:
root@dam2k:~# netstat -natp | grep 3306 tcp 0 0 0.0.0.0:3306 0.0.0.0:* LISTEN 2215/mysqld