Monday, June 25, 2012

MYSQL ERROR 2003 (HY000): Can't connect to MySQL server on 'xx.xx.xx.xx' (111)

So I was thrown this error recently, while setting up a testing mysql environment and attempting to access it from a remote host. Usually it's the simple things that are overlooked, so here's how to resolve the error:
ERROR 2003 (HY000): Can't connect to MySQL server on 'xx.xx.xx.xx' (111)

The first thing we can check is to see if the user from the remote host is allowed.

1. Login as root on mysql server
mysql -u root -p

2. Select database and show users.
select * from mysql.user\G

**A vertical list will be displayed. Look at the first two fields for each entry. If the user is only allowed to connect from localhost, this may be the problem.
Example:

Host: localhost
User: mydbuser

A user will have to be defined with the same parameters as mydbuser for the remote host (or hosts)
Here's where your documentation will come in handy (or you can hope the old query exists in the mysql buffer!)

3. Allow remote hosts to connect
grant select,insert,update,delete,create,drop,index,alter on mydbname.* to mydbuser@'192.168.1.%' identified by 'mydbpassword' ;

flush privileges;

Note: if you only want to allow a certain host, specify the IP instead of the wildcard.


The second issue that may cause this error is a MySQL configuration.

1. Open MySQL config file
nano /etc/my.cnf

2. Ensure that the following are commented out.

#skip-external-locking
#skip-networking
#bind-address = xx.xx.xx.xx

Save and exit

3. Restart mysql service
service mysqld start

The third issue that may contribute to this error may be the security configuration rejecting incoming requests on the server.

1. Login as root on db server

2. Add rule to iptables
/sbin/iptables -A INPUT -i eth0 -s 192.168.1.0/24 -p tcp --destination-port 3306 -j ACCEPT

** this grants access to the entire subnet, use a specific IP where applicable.

service iptables save

3. Restart iptables service
service iptables restart


Test from remote host by using the following:
mysql -h 192.168.my.dbip -u mydbuser -p


-noveck



5 comments:

  1. this is very bullshit post.... where did you copy this from ??? How can you even connect to root in remote host, when you have connection error at first ?

    ReplyDelete
  2. I'm very sorry this did not work for you, but it's simpy a documented solution to a problem I faced on my testLAN. Of course the assumption is that you already have shell/SSH access to all the servers.
    This situation was simply that I was not able to login to a remote mysql instance with a defined user.

    -noveck

    ReplyDelete
  3. Your manual helped me fix this issue. I have been using mysql for a very long time and never had a fresh installation (5.5 in this case) come with a my.cnf file that would prevent remote access.

    Installation on Ubuntu 12.04 put my.cnf in /etc/mysql. After hours and hours of ripping my hair out, it turned out the lines

    #skip-external-locking

    #bind-address = xx.xx.xx.xx

    were indeed not commented out by default.

    THANK YOU!

    ReplyDelete
  4. I also faced with this problem, but I just need to comment out the bind-address line, so I can connect to remote host.

    ReplyDelete