MySQL error 2006: mysql server has gone away

The Question :

252 people think this question is useful

I’m running a server at my office to process some files and report the results to a remote MySQL server.

The files processing takes some time and the process dies halfway through with the following error:

2006, MySQL server has gone away

I’ve heard about the MySQL setting, wait_timeout, but do I need to change that on the server at my office or the remote MySQL server?

The Question Comments :
  • it depends of that witch server gives the error
  • possible duplicate of ERROR 2006 (HY000): MySQL server has gone away
  • For people getting here from Google: If changing the max_allowed_packet size or wait_timeout amount doesn’t fix it, check your memory usage. I was getting the same error and it was being caused by my server running out of memory. I added a 1GB swap file and that fixed it.
  • @Pikamander2 thanks for the hint!
  • Oh! So it’s all lies? Mysql server actually didn’t go anywhere? It’s still right there in my server? Whao! :))

The Answer 1

34 people think this answer is useful

It may be easier to check if the connection and re-establish it if needed.

See PHP:mysqli_ping for info on that.

The Answer 2

381 people think this answer is useful

I’ve encountered this a number of times and I’ve normally found the answer to be a very low default setting of max_allowed_packet.

Raising it in /etc/my.cnf (under [mysqld]) to 8 or 16M usually fixes it. (The default in MySql 5.7 is 4194304, which is 4MB.)


Note: Just create the line if it does not exist

Note: This can be set on your server as it’s running.

Use set global max_allowed_packet=104857600. This sets it to 100MB.

The Answer 3

43 people think this answer is useful

I had the same problem but changeing max_allowed_packet in the my.ini/my.cnf file under [mysqld] made the trick.

add a line


now restart the MySQL service once you are done.

The Answer 4

38 people think this answer is useful

I used following command in MySQL command-line to restore a MySQL database which size more than 7GB, and it works.

set global max_allowed_packet=268435456;

The Answer 5

17 people think this answer is useful


Message: MySQL server has gone away

Generally you can retry connecting and then doing the query again to solve this problem – try like 3-4 times before completely giving up.

I’ll assuming you are using PDO. If so then you would catch the PDO Exception, increment a counter and then try again if the counter is under a threshold.

If you have a query that is causing a timeout you can set this variable by executing:

SET @@GLOBAL.wait_timeout=300;
SET @@LOCAL.wait_timeout=300;  -- OR current session only

Where 300 is the number of seconds you think the maximum time the query could take.

Further information on how to deal with Mysql connection issues.

EDIT: Two other settings you may want to also use is net_write_timeout and net_read_timeout.

The Answer 6

16 people think this answer is useful

In MAMP (non-pro version) I added


to ...\MAMP\bin\

Credits and more details here

The Answer 7

11 people think this answer is useful

This error is occur due to expire of wait_timeout .

Just go to mysql server check its wait_timeout :

mysql> SHOW VARIABLES LIKE ‘wait_timeout’

mysql> set global wait_timeout = 600 # 10 minute or maximum wait time out you need

The Answer 8

11 people think this answer is useful

There are several causes for this error.

MySQL/MariaDB related:

  • wait_timeout – Time in seconds that the server waits for a connection to become active before closing it.
  • interactive_timeout – Time in seconds that the server waits for an interactive connection.
  • max_allowed_packet – Maximum size in bytes of a packet or a generated/intermediate string. Set as large as the largest BLOB, in multiples of 1024.

Example of my.cnf:

# 8 hours
wait_timeout = 28800
# 8 hours
interactive_timeout = 28800
max_allowed_packet = 256M

Server related:

  • Your server has full memory – check info about RAM with free -h

Framework related:

  • Check settings of your framework. Django for example use CONN_MAX_AGE (see docs)

How to debug it:

  • Check values of MySQL/MariaDB variables.
    • with sql: SHOW VARIABLES LIKE '%time%';
    • command line: mysqladmin variables
  • Turn on verbosity for errors:
    • MariaDB: log_warnings = 4
    • MySQL: log_error_verbosity = 3
  • Check docs for more info about the error

The Answer 9

9 people think this answer is useful

On windows those guys using xampp should use this path xampp/mysql/bin/my.ini and change max_allowed_packet(under section[mysqld])to your choice size. e.g


Again on php.ini(xampp/php/php.ini) change upload_max_filesize the choice size. e.g


Gave me a headache for sometime till i discovered this. Hope it helps.

The Answer 10

9 people think this answer is useful

I was getting this same error on my DigitalOcean Ubuntu server.

I tried changing the max_allowed_packet and the wait_timeout settings but neither of them fixed it.

It turns out that my server was out of RAM. I added a 1GB swap file and that fixed my problem.

Check your memory with free -h to see if that’s what’s causing it.

The Answer 11

8 people think this answer is useful

It was RAM problem for me.

I was having the same problem even on a server with 12 CPU cores and 32 GB RAM. I researched more and tried to free up RAM. Here is the command I used on Ubuntu 14.04 to free up RAM:

sync && echo 3 | sudo tee /proc/sys/vm/drop_caches

And, it fixed everything. I have set it under cron to run every hour.

crontab -e

0 * * * * bash /root/;

And, you can use this command to check how much free RAM available:

free -h

And, you will get something like this:

             total       used       free     shared    buffers     cached
Mem:           31G        12G        18G        59M       1.9G       973M
-/+ buffers/cache:       9.9G        21G
Swap:         8.0G       368M       7.6G

The Answer 12

6 people think this answer is useful

If you are using xampp server :

Go to xampp -> mysql -> bin -> my.ini

Change below parameter :

max_allowed_packet = 500M

innodb_log_file_size = 128M

This helped me a lot 🙂

The Answer 13

5 people think this answer is useful

In my case it was low value of open_files_limit variable, which blocked the access of mysqld to data files.

I checked it with :

mysql> SHOW VARIABLES LIKE 'open%';
| Variable_name    | Value |
| open_files_limit | 1185  |
1 row in set (0.00 sec)

After I changed the variable to big value, our server was alive again :

open_files_limit = 100000

The Answer 14

5 people think this answer is useful

If you are using the 64Bit WAMPSERVER, please search for multiple occurrences of max_allowed_packet because WAMP uses the value set under [wampmysqld64] and not the value set under [mysqldump], which for me was the issue, I was updating the wrong one. Set this to something like max_allowed_packet = 64M.

Hopefully this helps other Wampserver-users out there.

The Answer 15

5 people think this answer is useful

This generally indicates MySQL server connectivity issues or timeouts. Can generally be solved by changing wait_timeout and max_allowed_packet in my.cnf or similar.

I would suggest these values:

wait_timeout = 28800

max_allowed_packet = 8M

The Answer 16

4 people think this answer is useful

For Vagrant Box, make sure you allocate enough memory to the box

config.vm.provider "virtualbox" do |vb|
  vb.memory = "4096"

The Answer 17

4 people think this answer is useful

The unlikely scenario is you have a firewall between the client and the server that forces TCP reset into the connection.

I had that issue, and I found our corporate F5 firewall was configured to terminate inactive sessions that is idle for more than 5 mins.

Once again, this is the unlikely scenario.

The Answer 18

4 people think this answer is useful

It’s always a good idea to check the logs of the Mysql server, for the reason why it went away.

It will tell you.

The Answer 19

3 people think this answer is useful

uncomment the ligne below in your my.ini/my.cnf, this will split your large file into smaller portion

# binary logging format - mixed recommended
# binlog_format=mixed


# binary logging format - mixed recommended

The Answer 20

3 people think this answer is useful

I found the solution to “#2006 – MySQL server has gone away” this error. Solution is just you have to check two files


Path of these files in windows is


In these two files the value of this:

$cfg['Servers'][$i]['host']must be 'localhost' .

In my case it was:

$cfg['Servers'][$i]['host'] = '';

change it to:

"$cfg['Servers'][$i]['host']" = 'localhost';

Make sure in both:

  2. files it must be ‘localhost’.

And last set:

$cfg['Servers'][$i]['AllowNoPassword'] = true;

Then restart Wampserver.

To change phpmyadmin user name and password

You can directly change the user name and password of phpmyadmin through file

These two lines

$cfg['Servers'][$i]['user'] = 'root';
$cfg['Servers'][$i]['password'] = '';

Here you can give new user name and password. After changes save the file and restart WAMP server.

The Answer 21

3 people think this answer is useful

There is an easier way if you are using XAMPP. Open the XAMPP control panel, and click on the config button in mysql section.
enter image description here

Now click on the my.ini and it will open in the editor. Update the max_allowed_packet to your required size.

enter image description here

Then restart the mysql service. Click on stop on the Mysql service click start again. Wait for a few minutes. enter image description here enter image description here

Then try to run your Mysql query again. Hope it will work.

The Answer 22

3 people think this answer is useful

MAMP 5.3, you will not find my.cnf and adding them does not work as that max_allowed_packet is stored in variables.

One solution can be:

  1. Go to http://localhost/phpmyadmin
  2. Go to SQL tab
  3. Run SHOW VARIABLES and check the values, if it is small then run with big values
  4. Run the following query, it set max_allowed_packet to 7gb:

    set global max_allowed_packet=268435456;

For some, you may need to increase the following values as well:

set global wait_timeout = 600;
set innodb_log_file_size =268435456;

The Answer 23

2 people think this answer is useful

I got Error 2006 message in different MySQL clients software on my Ubuntu desktop. It turned out that my JDBC driver version was too old.

The Answer 24

2 people think this answer is useful

This might be a problem of your .sql file size.

If you are using xampp. Go to the xampp control panel -> Click MySql config -> Open my.ini.

Increase the packet size.

max_allowed_packet = 2M -> 10M

The Answer 25

0 people think this answer is useful

For users using XAMPP, there are 2 max_allowed_packet parameters in C:\xampp\mysql\bin\my.ini.

The Answer 26

0 people think this answer is useful

This error happens basically for two reasons.

  1. You have a too low RAM.
  2. The database connection is closed when you try to connect.

You can try this code below.

# Simplification to execute an SQL string of getting a data from the database
def get(self, sql_string, sql_vars=(), debug_sql=0):
        self.cursor.execute(sql_string, sql_vars)
        return self.cursor.fetchall()
    except (AttributeError, MySQLdb.OperationalError):
        self.cursor.execute(sql_string, sql_vars)
        return self.cursor.fetchall()

It mitigates the error whatever the reason behind it, especially for the second reason.

If it’s caused by low RAM, you either have to raise database connection efficiency from the code, from the database configuration, or simply raise the RAM.

The Answer 27

0 people think this answer is useful

Just in case this helps anyone:

I got this error when I opened and closed connections in a function which would be called from several parts of the application. We got too many connections so we thought it might be a good idea to reuse the existing connection or throw it away and make a new one like so:

  public static function getConnection($database, $host, $user, $password)
 if (!self::$instance) {
  return self::newConnection($database, $host, $user, $password);
 } elseif ($database . $host . $user != self::$connectionDetails) {

self::$instance->query(‘KILL CONNECTION_ID()’); self::$instance = null; return self::newConnection($database, $host, $user, $password); } return self::$instance; } Well turns out we’ve been a little too thorough with the killing and so the processes doing important things on the old connection could never finish their business. So we dropped these lines

  self::$instance->query('KILL CONNECTION_ID()');
  self::$instance = null;

and as the hardware and setup of the machine allows it we increased the number of allowed connections on the server by adding

max_connections = 500

to our configuration file. This fixed our problem for now and we learned something about killing mysql connections.

The Answer 28

0 people think this answer is useful

I had the same problem in docker adding below setting in docker-compose.yml:

    image: mysql:8.0
    command: --wait_timeout=800 --max_allowed_packet=256M --character-set-server=utf8 --collation-server=utf8_general_ci --default-authentication-plugin=mysql_native_password
      - ./docker/mysql/data:/var/lib/mysql
      - ./docker/mysql/dump:/docker-entrypoint-initdb.d
      - 3306:3306

The Answer 29

-6 people think this answer is useful

If you know you’re going offline for a while, you can close your connection, do your processing, reconnect and write your reports.

Add a Comment