MySQL server has gone away in Magento



Recently I faced a problem MySQL server has gone away in Magento when I was moved the code from one hosting to another hosting. Actually the problem came into notice when I try to place order sometimes it sends me back to cart page and sometimes after the successful payment via standard PayPal the order status didn’t change and remain as pending payment. Then I trace the problem by checking log files and added log to PayPal IPN and found the problem like SQLSTATE[HY000]: General error: 2006 MySQL server has gone away. So in this post I am trying to explain the problem and solution for this problem.

Explanation: It is clearly visible that this is the MySQL timeout error. When you are using shared hosting and you get the huge traffic, so in this case a huge requests sends to MySQL and your request wait for a long time and finally timeout. Another case, If there is one DB connection and fetch some data from DB and do some stuff longer than DB timeout and now if you try to fetch data with same connection you will get the error MySQL server has gone away.

Solutions: You should use dedicated hosting if you are getting a huge traffic to your website instead of shared hosting. Secondly, you should close the DB connection after fetching data and create new DB connection to fetch data again and close the DB connection. If the problem remain same then you need to increase wait_timeout, connect_timeout and max_allowed_packet in my.ini or my.cnf file.

I have changed my my.ini settings as below:
max_allowed_packet = 32M
wait_timeout = 1800
connect_timeout = 120

To change these in your mysql’s my.cnf/my.ini configuration file follow below steps:
For windows: open mysql-install-dir/bin/my.ini and set wait_timeout = 1800 , max_allowed_packet = 32M and connect_timeout = 120 then save and restart mysql server.

For Linux: open /etc/mysql/my.cnf and set wait_timeout = 1800 , max_allowed_packet = 32M and connect_timeout = 120 then save and restart mysql server.

This solution solved my problem of SQLSTATE[HY000]: General error: 2006 MySQL server has gone away. Hope this will help you also. If you have different solutions please add your comments.