2

My php code fails to connect to the mysql database on my web server. I get the following error:

mysql_connect(): Lost connection to MySQL server at 'reading initial communication packet', system error: 110 in filename at line 71.

The same code works fine when run from my development machine pointed at the mysql database on the server I'm trying to run this from (I copied the file up to the server and I'm trying to run it via ssh on that server). I have verified that the mysql user in the script can connect to the database from the server by running mysql from the ssh command line on that server using the same user name and password as specified in my php script.

here is my code:

function jsw_set_cnxn($env){
        global $env;
        $cnxn = mysql_connect($env['db_svr'], $env['db_usr'], $env['db_pwd']) 
               or die ('DB Connection Error: ' . mysql_error());
        mysql_select_db ($env['db'], $cnxn);
    return $cnxn;
}
Scott
  • 674
  • 1
  • 7
  • 15
  • [A possibly related](http://stackoverflow.com/questions/961928/could-not-connect-lost-connection-to-mysql-server-at-reading-initial-communicat) Ruby question – Charles Sprayberry Jul 29 '11 at 22:53
  • googling a little shows a lot of results on the subject. this is one of the links that may help you out: http://forums.mysql.com/read.php?52,152265,152265 – marcelog Jul 29 '11 at 23:10
  • @Charles - I saw that question when I searched, it did not help – Scott Jul 29 '11 at 23:27
  • @marcelog I googled first, and checked out many forum posts including the one you mentioned. Nothing fit exactly. – Scott Jul 29 '11 at 23:30
  • I should mention that there are several php web sites on this server and they all connect with no problem using essentially the same code. – Scott Jul 29 '11 at 23:32
  • uhmm.. can you see the other scripts? if they can connect you should too. there must be a difference somewhere (i.e: host:port) – marcelog Jul 30 '11 at 00:01

1 Answers1

1

I've seen this error caused when using the servers IP address when it expects localhost as the server address.

This is because the server is the machine mysql expects but the wrong host coming in. Try using localhost.

Tim Davies
  • 824
  • 5
  • 17