MySQL is a popular database used by many organizations and websites. Sometimes, while using MySQL, you may get an error saying ‘2006, MySQL Server has gone away’. This is true if you have long running queries and the server dies halfway through. In this article, we will learn how to fix this issue.
How to Fix 2006 MySQL Server Has Gone Away
One of the most common reasons why this error occurs is due to a low value of max_allowed_packet in MySQL. You can modify it in my.ini (Windows)/my.cnf (Linux) file.
Open this file in text editor.
$ vi /etc/my.cnf
Look for the following line under [mysqld] section
Increase max_allowed_packet value to 8M or 16M. max_allowed_packet is the maximum size in bytes of a packet or a generated/intermediate string. Basically, the default value of this parameter is 4Mb and we increase it to 8Mb or 16Mb. Create this line if it does not exist.
Restart MySQL server to apply changes.
$ service mysqld restart
If you have a really large database (>1Gb) then log into MySQL and run the following command to set max_allowed_packet to 100Mb.
set global max_allowed_packet=104857600
Sometimes this error may occur because of a low wait_timeout value in my.cnf. wait_timeout is the timeout value in seconds that server waits for connection to become active before closing it. If the above solution does not work for you, then open my.ini/my.cnf file and look for the following line under [mysqld] section.
wait_timeout = ...
Set it to the following value to increase it.
[mysqld] ... # 1 hour wait_timeout = 3600
Save and close the file. Restart MySQL server to apply changes.
There are also other but less common reasons for this error. Sometimes you may get this error because your server memory is full. If that is the case, then you can check free memory on your system, using the following command.
$ free -h
In such cases, you may need to close some of your other applications to free up memory.
In this article, we have learnt how to fix 2006 MySQL Server has gone away. This basically happens because there is not enough resources allocated to the database or timeout value is too low.
How to Check if JS Object has Property
How to Auto Increment With Prefix As Primary Key in MySQL
How to Set Initial Value & Auto Increment in MySQL