301

I tried to import a large sql file through phpMyAdmin...But it kept showing error

'MySql server has gone away'

What to do?

Himanshu
  • 31,810
  • 31
  • 111
  • 133
FrancisMV123
  • 3,419
  • 4
  • 20
  • 20
  • 3
    What are your values for max_allowed_packet and wait_timeout? – daemonofchaos Sep 14 '12 at 13:17
  • 2
    you can try to double `max_allowed_packet`. A bit crude, but if that works you can find a sane value. – Nanne Sep 14 '12 at 13:19
  • I see it often, by chance. But I cannot reproduce the same error again. And, hence realized that it might be dynamic. Sometimes, if the client sends too many SQLs to the server (eg. from a loop), this can happen. Finding the actual reason for this error is important. Check your query logs for repeating patterns of SQLs too to find out the clues. – Bimal Poudel Sep 02 '14 at 07:45
  • 1
    sometimes you get this when your disk space is low – pramodtech Oct 17 '14 at 06:33
  • I ran into this issue on my site, which is on hosted server. I do not have access to my.ini. What can I do now? –  Jun 28 '17 at 15:42
  • Possible duplicate of [ERROR 2006 (HY000): MySQL server has gone away](https://stackoverflow.com/questions/10474922/error-2006-hy000-mysql-server-has-gone-away) – kenorb Nov 13 '18 at 15:24
  • Compression flag in `mysqli` can also limit any query size to 16M. See https://stackoverflow.com/a/48915262/4110077 – Anse Oct 16 '19 at 08:01

22 Answers22

433

As stated here:

Two most common reasons (and fixes) for the MySQL server has gone away (error 2006) are:

Server timed out and closed the connection. How to fix:

  1. check that wait_timeout variable in your mysqld’s my.cnf configuration file is large enough. On Debian: sudo nano /etc/mysql/my.cnf, set wait_timeout = 600 seconds (you can tweak/decrease this value when error 2006 is gone), then sudo /etc/init.d/mysql restart. I didn't check, but the default value for wait_timeout might be around 28800 seconds (8 hours).

  2. Server dropped an incorrect or too large packet. If mysqld gets a packet that is too large or incorrect, it assumes that something has gone wrong with the client and closes the connection. You can increase the maximal packet size limit by increasing the value of max_allowed_packet in my.cnf file. On Debian: sudo nano /etc/mysql/my.cnf, set max_allowed_packet = 64M (you can tweak/decrease this value when error 2006 is gone), then sudo /etc/init.d/mysql restart.

Edit:

Notice that MySQL option files do not have their commands already available as comments (like in php.ini for instance). So you must type any change/tweak in my.cnf or my.ini and place them in mysql/data directory or in any of the other paths, under the proper group of options such as [client], [myslqd], etc. For example:

[mysqld]
wait_timeout = 600
max_allowed_packet = 64M

Then restart the server. To get their values, type in the mysql client:

> select @@wait_timeout;
> select @@max_allowed_packet;
Gustavo Straube
  • 3,744
  • 6
  • 39
  • 62
GBD
  • 15,847
  • 2
  • 46
  • 50
  • 81
    In my case it was the **max_allowed_packet** variable. It was set to a default of 1 MB (you can see this by running the `SHOW VARIABLES` MySQL query) and the file I was importing had a few very large records. I opened the **my.ini** file (I was running Windows) and set the value to this: `max_allowed_packet = 64M`, restarted MySQL and re-ran the import. – Brent Matzelle Nov 08 '12 at 16:27
  • I was uploading large files to the database with webissues. This helped, thanks... :-) – inf3rno Jun 22 '13 at 15:25
  • @BrentMatzelle That certainly took care of "Mysql has gone away" problem. Thanks! – asprin Jul 06 '13 at 10:18
  • On windows its my.ini and updating max_allowed_packet worked for me while trying to import via cmd line – Banning Dec 04 '13 at 19:08
  • @BrentMatzelle Thank you! That was the cure for my issue, even though `wait_timeout` wasn't even present. Also running on Windows. – ingredient_15939 Feb 04 '14 at 00:16
  • Nice. I was trying to pipe in a 24M backup, raising max_allowed_packet to 64M did the trick. – Stephen Smith May 15 '14 at 19:10
  • 1
    Worked! Note that on (windows) xampp installations the my.ini file will be under xampp/mysql/bin/my.ini – Lulu Mar 02 '15 at 12:32
  • 2
    I can't find `wait_timeout` line – Oki Erie Rinaldi Mar 03 '15 at 02:25
  • 2
    Beware that my.ini file contains 2 values of `max_allowed_packet`. Change both, just to be sure. I made the mistake of only changing the second value of `max_allowed_packet`, and not looking for other values. That cost me a good 30 minutes of stress. – Sorin C Dec 16 '15 at 22:14
  • excellent! also if you're under MAMP grab this, modify the values as per this answer and put it in `/Application/MAMP/conf/` and restart MAMP https://gist.github.com/fragje/9953959 worked like a charm for me – Robert Sinclair Jun 15 '16 at 05:29
  • there are no such variables in this very file. and if they are added, starting the mysql service doesnt work anymore. – phil294 Aug 04 '16 at 12:00
  • 6
    in case of windows(with Xampp) it was my.ini file and innodb_lock_wait_timeout – Ananda Nov 26 '16 at 17:45
  • My SQL file size is 50M. In case of windows(with Xampp) it was my.ini file and modified max_allowed_packet = 60M. It's run successful. – Giang Phan Sep 17 '17 at 04:18
  • @GBD, everything makes sense. Except that I need to understand you saying : **"check that wait_timeout ... is large enough** and **set `wait_timeout = 600` when default value was 28800"**, I need to make up my mind. Should I increase or decrease `wait_timeout` value? – user10089632 Jan 23 '18 at 07:58
  • I tried it with 28800 on windows, but I get: `The value you entered (28800) is out of range. The number of seconds must be between 10 and 9999.` – Black May 27 '18 at 18:10
  • What does SELECT @@version; display for you? See the appropriate refman for details on the range. – Wilson Hauck Nov 05 '19 at 14:51
  • In Mac, i cannot find folder mysql inside etc folder. – Bhimashankar Sutar Dec 10 '20 at 11:35
114

For me this solution didn't work out so I executed

SET GLOBAL max_allowed_packet=1073741824;

in my SQL client.

If not able to change this with MYSql service running, you should stop the service and change the variable in "my.ini" file.

For example:

max_allowed_packet=20M
TheGabiRod
  • 418
  • 4
  • 14
salsinga
  • 1,957
  • 1
  • 14
  • 26
  • 1
    This worked for me on MAMP (OS X) when the other more popular answers did not. But is it permanent? – atwixtor Feb 20 '15 at 17:09
  • 2
    @atwixtor no, setting variables like this work until the server is restarted. and then they are reset to the default value. – d.raev Apr 08 '15 at 13:27
  • 1
    That did the trick, on a test XAMPP machine on Windows I had to modify both the `php.ini` file (as suggested by @GBD in the answer) and the MySQL config file `my.ini` to make it work. – Gruber Sep 09 '15 at 04:01
  • 2
    I'd suggest adding that you can check the current size with `SHOW variables LIKE 'max_allowed_packet'` – Arth Oct 16 '15 at 15:34
  • This isn't ***permanent*** in that the value will reset when the server restarts (eventually), but it also isn't just for the current session. Setting the `max_allowed_packet` in one client and then importing the large `.sql` file in another session will work fine. – VoteyDisciple Jun 02 '16 at 11:22
  • Worked great for me on OSX El Capitan, I couldn't get my my.cnf changes to apply even after restarting, so this was the only solution that worked for me. – Robby Cowell Aug 15 '16 at 15:44
  • This worked for me too on XAMPP and same in ubuntus MariaDB. – Argoitz Dec 03 '19 at 08:49
31

If you are working on XAMPP then you can fix the MySQL Server has gone away issue with following changes..

open your my.ini file my.ini location is (D:\xampp\mysql\bin\my.ini)

change the following variable values

max_allowed_packet = 64M
innodb_lock_wait_timeout = 500
Mohan Gathala
  • 411
  • 4
  • 4
22

If you are running with default values then you have a lot of room to optimize your mysql configuration.

The first step I recommend is to increase the max_allowed_packet to 128M.

Then download the MySQL Tuning Primer script and run it. It will provide recommendations to several facets of your config for better performance.

Also look into adjusting your timeout values both in MySQL and PHP.

How big (file size) is the file you are importing and are you able to import the file using the mysql command line client instead of PHPMyAdmin?

daemonofchaos
  • 795
  • 4
  • 13
  • Thanks, I already had max_allowed_packet set to 16M, then found this question and raised it to 32M thinking it would surely be enough but then saw your answer and 128M definitely works. I am gathering that the entire SQL statement inside the *.sql file is treated as just one packet? – Elijah Lynn Dec 30 '15 at 21:27
9

If you are using MAMP on OS X, you will need to change the max_allowed_packet value in the template for MySQL.

  1. You can find it at: File > Edit template > MySQL my.cnf

  2. Then just search for max_allowed_packet, change the value and save.

radbyx
  • 9,352
  • 21
  • 84
  • 127
askthebigo
  • 101
  • 1
  • 1
8

I had this error and other related ones, when I imported at 16 GB SQL file. For me, editing my.ini and setting the following (based on several different posts) in the [mysqld] section:

max_allowed_packet      = 110M
innodb_buffer_pool_size=511M
innodb_log_file_size=500M
innodb_log_buffer_size = 800M
net_read_timeout        = 600
net_write_timeout       = 600

If you are running under Windows, go to the control panel, services, and look at the details for MySQL and you will see where my.ini is. Then after you edit and save my.ini, restart the mysql service (or restart the computer).

If you are using HeidiSQL, you can also set some or all of these using that.

BenV136
  • 319
  • 2
  • 5
7

I solved my issue with this short /etc/mysql/my.cnf file :

[mysqld]
wait_timeout = 600
max_allowed_packet = 100M
Dan
  • 1,159
  • 13
  • 8
  • 1
    confirmed this also works for MySQL v5.6.12 on WAMP: on Windows, add the "wait_timeout" and "max_allowed_packet" lines above to the [mysqld] section of: wamp\bin\mysql\mysql_version\my.ini – dean.huczok Jul 03 '16 at 04:05
  • Thanks @dan solved my year old issue haha, I never added those settings under section `[mysqld]` – shivgre Nov 10 '17 at 14:11
  • This works for MySQL v5.7. my-default.ini isn't created by default so you need to create it. – Rodolfo Velasco Nov 22 '17 at 21:40
5

The other reason this can happen is running out of memory. Check /var/log/messages and make sure that your my.cnf is not set up to cause mysqld to allocate more memory than your machine has.

Your mysqld process can actually be killed by the kernel and then re-started by the "safe_mysqld" process without you realizing it.

Use top and watch the memory allocation while it's running to see what your headroom is.

make a backup of my.cnf before changing it.

Robert Longson
  • 118,664
  • 26
  • 252
  • 242
TekOps
  • 179
  • 2
  • 5
4

I got same issue with

$image_base64 = base64_encode(file_get_contents($_FILES['file']['tmp_name']) );
$image = 'data:image/jpeg;base64,'.$image_base64;
$query = "insert into images(image) values('".$image."')";
mysqli_query($con,$query);

In \xampp\mysql\bin\my.ini file of phpmyadmin we get only

[mysqldump]
max_allowed_packet=110M

which is just for mysqldump -u root -p dbname . I resolved my issue by replacing above code with

max_allowed_packet=110M
[mysqldump]
max_allowed_packet=110M
Amir Khan
  • 211
  • 1
  • 4
2

I updated "max_allowed_packet" to 1024M, but it still wasn't working. It turns out my deployment script was running:

mysql --max_allowed_packet=512M --database=mydb -u root < .\db\db.sql

Be sure to explicitly specify a bigger number from the command line if you are donig it this way.

rockstardev
  • 13,479
  • 39
  • 164
  • 296
2

If your data includes BLOB data:

Note that an import of data from the command line seems to choke on BLOB data, resulting in the 'MySQL server has gone away' error.

To avoid this, re-create the mysqldump but with the --hex-blob flag:

http://dev.mysql.com/doc/refman/5.7/en/mysqldump.html#option_mysqldump_hex-blob

which will write out the data file with hex values rather than binary amongst other text.

PhpMyAdmin also has the option "Dump binary columns in hexadecimal notation (for example, "abc" becomes 0x616263)" which works nicely.

Note that there is a long-standing bug (as of December 2015) which means that GEOM columns are not converted: Back up a table with a GEOMETRY column using mysqldump? so using a program like PhpMyAdmin seems to be the only workaround (the option noted above does correctly convert GEOM columns).

Community
  • 1
  • 1
fooquency
  • 1,575
  • 3
  • 16
  • 29
1

If it takes a long time to fail, then enlarge the wait_timeout variable.

If it fails right away, enlarge the max_allowed_packet variable; it it still doesn't work, make sure the command is valid SQL. Mine had unescaped quotes which screwed everything up.

Also, if feasible, consider limiting the number of inserts of a single SQL command to, say, 1000. You can create a script that creates multiple statements out of a single one by reintroducing the INSERT... part every n inserts.

e18r
  • 7,578
  • 4
  • 45
  • 40
1

i got a similar error.. to solve this just open my.ini file..here at line no 36 change the value of maximum allowed packet size ie. max_allowed_packet = 20M

parag jain
  • 11
  • 2
1

For GoDaddy shared hosting

On GoDaddy shared hosting accounts, it is tricky to tweak the PHP.ini etc files. However, there is another way and it just worked perfectly for me. (I just successfully uploaded a 3.8Mb .sql text file, containing 3100 rows and 145 cols. Using the IMPORT command in phpMyAdmin, I was getting the dreaded MySQL server has gone away error, and no further information.)

I found that Matt Butcher had the right answer. Like Matt, I had tried all kinds of tricks, from exporting MySQL databases in bite-sized chunks, to writing scripts that break large imports into smaller ones. But here is what worked:

(1) CPANEL ---> FILES (group) ---> BACKUP

(2a) Under "Partial Backups" heading...
(2b) Under "Download a MySQL Database Backup"
(2c) Choose your database and download a backup (this step optional, but wise)

(3a) Directly to the right of 2b, under heading "Restore a MySQL Database Backup"
(3b) Choose the .SQL import file from your local drive
(3c) True happiness will be yours (shortly....) Mine took about 5 seconds

I was able to use this method to import a single table. Nothing else in my database was affected -- but that is what step (2) above is intended to protect against.

Notes:
a. If you are unsure how to create a .SQL import file, use phpMyAdmin to export a table and modify that file structure.

SOURCE: Matt Butcher 2010 Article

cssyphus
  • 37,875
  • 18
  • 96
  • 111
  • Okay, so an immediate downvote, but no comment as to why. Did this info not work for someone? If so, please share - it worked for me, which is why I added this answer. The other above answers didn't work for me, but this solution did. So why the downvote? If you are on GoDaddy and this does not work for you, I would like to know so that I can assist. However, if you are not using GoDaddy shared hosting, why would you downvote this answer just because it doesn't apply **to you** ? – cssyphus Feb 07 '18 at 17:20
1

Make sure mysqld process does not restart because of service managers like systemd.

I had this problem in vagrant with centos 7. Configuration tweaks didn't help. Turned out it was systemd which killed mysqld service every time when it took too much memory.

tvorog
  • 979
  • 9
  • 6
1

For Xampp, Edit the “my.ini” file at “:\xampp\mysql\bin\”. In the my.ini file, edit the “max_allowed_packet” by increasing the value. XAMPP default value is 1M. I updated it to 10M. Save the my.ini file. Restart your MySQL and Apache server in XAMPP. This solution worked for me. Hopefully, this helps.

allan
  • 111
  • 6
0

I had similar error today when duplicating database (MySQL server has gone away...), but when I tried to restart mysql.server restart I got error

ERROR! The server quit without updating PID ...

This is how I solved it: I opened up Applications/Utilities/ and ran Activity Monitor

 quit mysqld

then was able to solve the error problem with

mysql.server restart
Kingsley Ijomah
  • 3,273
  • 33
  • 25
0

I am doing some large calculations which involves the mysql connection to stay long time and with heavy data. i was facing this "Mysql go away issue". So i tried t optimize the queries but that doen't helped me then i increased the mysql variables limit which is set to a lower value by default.

wait_timeout max_allowed_packet

To the limit what ever suits to you it should be the Any Number * 1024(Bytes). you can login to terminal using 'mysql -u username - p' command and can check and change for these variable limits.

0

If increasing max_allowed_packet doesn't help.

I was getting the same error as you when importing a .sql file into my database via Sequel Pro.

The error still persisted after upping the max_allowed_packet to 512M so I ran the import in the command line instead with:

mysql --verbose -u root -p DatabaseName < MySQL.sql

It gave the following error:

ASCII '\0' appeared in the statement, but this is not allowed unless option --binary-mode is enabled

I found a couple helpful StackOverflow questions:

In my case, my .sql file was a little corrupt or something. The MySQL dump we get comes in two zip files that need to be concatenated together and then unzipped. I think the unzipping was interrupted initially, leaving the file with some odd characters and encodings. Getting a fresh MySQL dump and unzipping it properly worked for me.

Just wanted to add this here in case others find that increasing the max_allowed_packet variable was not helping.

Joshua Pinter
  • 45,245
  • 23
  • 243
  • 245
0

None of the solutions regarding packet size or timeouts made any difference for me. I needed to disable ssl

mysql -u -p -hmyhost.com --disable-ssl db < file.sql

https://dev.mysql.com/doc/refman/5.7/en/encrypted-connections.html

chiliNUT
  • 18,989
  • 14
  • 66
  • 106
0

If this error message gets in during an import from dumps, maybe this answer would give a hint https://stackoverflow.com/a/51432853/6681141 .

Meaning that the connection made from the CLI has some default character sets, may default to operation systems, which is different from the dump file's encoding.

Then, there are two ways to resolve this:

  • If you're using a configuration/extra file for this connection, i.e., bin\mysql.exe --defaults-extra-file=default.ini -uroot db < dump.sql, see this https://stackoverflow.com/a/3513812/6681141 .

  • Or, just add an extra parameter --default-character-set=XXX into the command line bin\mysql.exe --default-character-set=XXX -uroot db < dump.sql, where XXX is the charset to the encoding of the dump file.

  • This does not provide an answer to the question. Once you have sufficient [reputation](https://stackoverflow.com/help/whats-reputation) you will be able to [comment on any post](https://stackoverflow.com/help/privileges/comment); instead, [provide answers that don't require clarification from the asker](https://meta.stackexchange.com/questions/214173/why-do-i-need-50-reputation-to-comment-what-can-i-do-instead). - [From Review](/review/late-answers/34299966) – Rahul Singh May 02 '23 at 14:37
0

innodb_log_file_size =5G Fixed my issue

user63814
  • 11
  • 4