MySQL Lost connection to MySQL server during query

For more than 3 hours I’ve been trying to find a solution to the error I kept receiving when doing a basic query to MySQL through PHP. It kept returning back to me “Lost connection to MySQL server during query”. Right away I went to my PHP, MySQL and Apache logs to find more information. I came back with nothing more than the error message I already received. So as I usually do off I went to Google and IRC for some help. Neither were leading into any direction to a solution. Even after finding the MySQL documentation talking about the “Lost Connection” or “server has gone away” error I didn’t find a solution. I started talking to dhartmei on EFNet in the #OpenBSD channel for some assistance. He stepped me through doing the following at the command prompt.

# nc -v 127.0.0.1 3306
Connection to 127.0.0.1 3306 port [tcp/mysql] succeeded!
Host ‘localhost.domain.com’ is not allowed to connect to this MySQL

# netstat -an | grep 3306
tcp 0 0 *.3306 *.* LISTEN

# mysql -h 127.0.0.1 -u username -p database
Enter password:
ERROR 2013: Lost connection to MySQL server during query

This determined that port 3306 was listening for connections however localhost/127.0.0.1 returned an error message of “Host ‘localhost’ is not allowed to connect to this MySQL”. If you haven’t caught on already this means I forgot to set MySQL permissions correctly for the user. Remember that localhost and 127.0.0.1 is different according to MySQL permissions.

A quick GRANT (i.e. grant all ON database.table TO username@localhost identified by ‘password-here’) command in MySQL resolved the problem.

2 thoughts on “MySQL Lost connection to MySQL server during query

  1. Hi,
    i want to database connection between putty -mysql and PHP. i got this error “Lost connection to MySQL server during query”, how to solve this problem, please Help me.

    Regards,
    Bhushan.

  2. Not sure I understand what you are asking. Putty is just a SSH client. Are you saying you connect into the server using Putty (ssh) then try to connect to MySQL but then get “lost connection” error message? I require more details, as much as possible.