MySql: can connect normally to local db, can connect via TCP remotely, but cannot connect via TCP locally

connectionMySQL

Host A is running a mysql server. On A, I can connect to the db:

hostA: mysql -p

I have allowed user X from host B to connect. I am able to connect via this:

hostB: mysql -h A --protocol=TCP -p -u X

However, I cannot connect to A from A via TCP:

hostA: mysql -h 127.0.0.1 --protocol=tcp -p

It says:

ERROR 2003 (HY000): Can't connect to MySQL server on '127.0.0.1' (111)

I've tried the following addresses:

localhost, 127.0.0.1, <hostname>, <actual ip>

The bind-address in my.cnf is the actual IP address.

Any thoughts? Can anyone suggest further diagnostics?

Best Answer

Your command:

hostA: mysql -h 127.0.0.1 --protocol=tcp -p

means to connect to the server over TCP socket using the loopack IP address 127.0.0.1.

It seems that your mysql daemon (mysqld) is listening on the IP address of one of the interfaces. You can confirm that by looking at my.cnf file:

bind-address  = your_server_ip_addr

and using:

$ sudo netstat -lnp | grep mysql

If you specify the bind-address as:

  1. 0.0.0.0: listen on all interfaces, or
  2. 127.0.0.1: listen locally only

your command should work.

To fix the problem, you need either to change your command to be like:

mysql -h your_sever_ip_addr --protocol=tcp -p

or change the bind-address appropriately.