libmysql mysql_real_connect fails with localhost but works with local IP address

StackOverflow https://stackoverflow.com//questions/9567704

  •  06-12-2019
  •  | 
  •  

Question

I am using the libmysql C API, the mysql_real_connect call only works if I use the real IP address of the host - other than localhost. If I use localhost as host I get the following error:

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

The authentication should be ok, since the used user has all privileges from localhost and any host also.

By the way it is an XAMPP installation on Windows 7.

Was it helpful?

Solution

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.

OTHER TIPS

Either your DNS is broken (Does ping localhost work as expected?) or the way "localhost" is specially treated is the problem.

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.

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

  1. check if in your /etc/hosts file it's defined something like that: 127.0.0.1 localhost
  2. check how mysql is binding its port:

    root@dam2k:~# netstat -natp | grep 3306 tcp 0 0 0.0.0.0:3306 0.0.0.0:* LISTEN 2215/mysqld

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top