I am trying to connect to mysql database on amazon through a php script, but I am shown this error:

Warning: mysql_connect() [function.mysql-connect]: Lost connection to MySQL server at 'reading initial communication packet', system error: 111

I have tried and searched places and did the following things:

  • In "/etc/mysql/my.cnf" I commented out the line bind address: to allow the acccess to all.
  • checked /etc/hosts.allow and /etc/hosts.deny and made sure that there are no rules present that may cause

But still no luck. Please suggest any other way.

Thanks, Madhup


do you connect from php to ?

are you sure you have restarted your mysql after changing the configuration file?

can you connect to mysql using command line:

mysql -uroot -psomePass -h127.0.0.1

do you see on what port mysql listens? can it be it is non-standard port?

netstat -lanp|grep mysqld|grep LISTEN

is it possible that your iptables does not allow connections over loopback? run

iptables -I INPUT -i lo -j ACCEPT
iptables -I OUTPUT -o lo -j ACCEPT
  • I have got it working right now. The problem was that mysql was not getting restarted. It failed to restart but today it restarted, any suggestion or idea why it was not restarting or it happens sometimes without any reason. – Madhup Singh Yadav May 17 '10 at 10:03
  • @madhup corrupted/removed by mistake pidfile comes to my mind as possible excuse for that behavior. – pQd May 17 '10 at 11:26

Here's a MySQL forum post that should help.


how is binding mysql to allowing access to all? if you're connecting from an external server, you'll need to bind mysql to the external IP address, so that you can connect remotely.

you may also wish to add some firewall rules, i.e.

/sbin/iptables -A INPUT -p tcp --dport 3306 -s <php_host_ip> -j ACCEPT
/sbin/iptables -A INPUT -p tcp --dport 3306 -j DROP

to block access to mysql from any other hosts, even though mysql is supposed to take care of itself, it's better to have two locked doors, than one.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.