简体   繁体   English

MySQL 导入大 sql 文件时服务器已经消失

[英]MySQL Server has gone away when importing large sql file

I tried to import a large sql file through phpMyAdmin...But it kept showing error我试图通过phpMyAdmin导入一个大的sql文件...但是它一直显示错误

'MySql server has gone away' 'MySql 服务器已经消失'

What to do?该怎么办?

As stated here :如前所述这里

Two most common reasons (and fixes) for the MySQL server has gone away (error 2006) are: MySQL 服务器消失(错误 2006)的两个最常见原因(和修复)是:

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.检查mysqld 的my.cnf 配置文件中的wait_timeout 变量是否足够大。 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 .在 Debian 上: sudo nano /etc/mysql/my.cnf ,设置wait_timeout = 600秒(当错误 2006 消失时,您可以调整/减少此值),然后sudo /etc/init.d/mysql restart I didn't check, but the default value for wait_timeout might be around 28800 seconds (8 hours).我没有检查,但 wait_timeout 的默认值可能约为 28800 秒(8 小时)。

  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.如果 mysqld 收到一个太大或不正确的数据包,它会假定客户端出现问题并关闭连接。 You can increase the maximal packet size limit by increasing the value of max_allowed_packet in my.cnf file.您可以通过增加 my.cnf 文件中 max_allowed_pa​​cket 的值来增加最大数据包大小限制。 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 .在 Debian 上: sudo nano /etc/mysql/my.cnf ,设置max_allowed_packet = 64M (当错误 2006 消失时,您可以调整/减少此值),然后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).请注意,MySQL 选项文件的命令尚未作为注释提供(例如在 php.ini 中)。 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:因此,您必须在my.cnfmy.ini键入任何更改/调整,并将它们放在mysql/data目录或任何其他路径中,在适当的选项组下,例如[client][myslqd]等。例如:

[mysqld]
wait_timeout = 600
max_allowed_packet = 64M

Then restart the server.然后重启服务器。 To get their values, type in the mysql client:要获取它们的值,请在 mysql 客户端中键入:

> select @@wait_timeout;
> select @@max_allowed_packet;

For me this solution didn't work out so I executed对我来说,这个解决方案没有奏效,所以我执行了

SET GLOBAL max_allowed_packet=1073741824;

in my SQL client.在我的 SQL 客户端中。

If not able to change this with MYSql service running, you should stop the service and change the variable in "my.ini" file.如果无法在 MYSql 服务运行时更改此设置,则应停止该服务并更改“my.ini”文件中的变量。

For example:例如:

max_allowed_packet=20M

If you are working on XAMPP then you can fix the MySQL Server has gone away issue with following changes..如果您正在使用 XAMPP,那么您可以通过以下更改修复 MySQL 服务器已消失的问题。

open your my.ini file my.ini location is (D:\\xampp\\mysql\\bin\\my.ini)打开你的 my.ini 文件 my.ini 位置是 (D:\\xampp\\mysql\\bin\\my.ini)

change the following variable values更改以下变量值

max_allowed_packet = 64M
innodb_lock_wait_timeout = 500

If you are running with default values then you have a lot of room to optimize your mysql configuration.如果您使用默认值运行,那么您有很大的空间来优化您的 mysql 配置。

The first step I recommend is to increase the max_allowed_packet to 128M.我建议的第一步是将 max_allowed_pa​​cket 增加到 128M。

Then download the MySQL Tuning Primer script and run it.然后下载MySQL Tuning Primer 脚本并运行它。 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.还要考虑在 MySQL 和 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?您要导入的文件有多大(文件大小),您能否使用 mysql 命令行客户端而不是 PHPMyAdmin 导入文件?

If you are using MAMP on OS X, you will need to change the max_allowed_packet value in the template for MySQL.如果您在 OS X 上使用 MAMP,则需要更改 MySQL 模板中的max_allowed_packet值。

  1. You can find it at: File > Edit template > MySQL my.cnf您可以在以下位置找到它:文件 > 编辑模板 > MySQL my.cnf

  2. Then just search for max_allowed_packet , change the value and save.然后只需搜索max_allowed_packet ,更改值并保存。

I solved my issue with this short /etc/mysql/my.cnf file :我用这个简短的 /etc/mysql/my.cnf 文件解决了我的问题:

[mysqld]
wait_timeout = 600
max_allowed_packet = 100M

I had this error and other related ones, when I imported at 16 GB SQL file.当我导入 16 GB SQL 文件时,我遇到了这个错误和其他相关错误。 For me, editing my.ini and setting the following (based on several different posts) in the [mysqld] section:对我来说,编辑 my.ini 并在 [mysqld] 部分设置以下内容(基于几个不同的帖子):

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.如果您在 Windows 下运行,请转到控制面板、服务并查看 MySQL 的详细信息,您将看到 my.ini 在哪里。 Then after you edit and save my.ini, restart the mysql service (or restart the computer).然后编辑保存my.ini后,重启mysql服务(或重启电脑)。

If you are using HeidiSQL, you can also set some or all of these using that.如果您使用的是 HeidiSQL,您还可以使用它来设置部分或全部。

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.检查 /var/log/messages 并确保您的 my.cnf 没有设置为导致 mysqld 分配比您的机器更多的内存。

Your mysqld process can actually be killed by the kernel and then re-started by the "safe_mysqld" process without you realizing it.你的 mysqld 进程实际上可以被内核杀死,然后在你没有意识到的情况下被“safe_mysqld”进程重新启动。

Use top and watch the memory allocation while it's running to see what your headroom is.使用 top 并在它运行时观察内存分配,看看你的空间是多少。

make a backup of my.cnf before changing it.在更改之前备份 my.cnf。

I updated "max_allowed_packet" to 1024M, but it still wasn't working.我将“max_allowed_pa​​cket”更新为 1024M,但它仍然无法正常工作。 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.如果您这样做,请务必从命令行明确指定一个更大的数字。

If your data includes BLOB data:如果您的数据包括BLOB数据:

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.请注意,从命令行导入数据似乎会阻塞 BLOB 数据,从而导致“MySQL 服务器已消失”错误。

To avoid this, re-create the mysqldump but with the --hex-blob flag:为了避免这种情况,重新创建 mysqldump 但使用--hex-blob标志:

http://dev.mysql.com/doc/refman/5.7/en/mysqldump.html#option_mysqldump_hex-blob 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. PhpMyAdmin 还具有选项“以十六进制表示法转储二进制列(例如,“abc”变为 0x616263)”效果很好。

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?请注意,存在一个长期存在的错误(截至 2015 年 12 月),这意味着不会转换GEOM列: 使用 mysqldump 备份带有 GEOMETRY 列的表? so using a program like PhpMyAdmin seems to be the only workaround (the option noted above does correctly convert GEOM columns).所以使用像 PhpMyAdmin 这样的程序似乎是唯一的解决方法(上面提到的选项可以正确转换 GEOM 列)。

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在 phpmyadmin 的 \\xampp\\mysql\\bin\\my.ini 文件中,我们只得到

[mysqldump]
max_allowed_packet=110M

which is just for mysqldump -u root -p dbname .这仅适用于 mysqldump -u root -p dbname 。 I resolved my issue by replacing above code with我通过替换上面的代码解决了我的问题

max_allowed_packet=110M
[mysqldump]
max_allowed_packet=110M

If it takes a long time to fail, then enlarge the wait_timeout variable.如果需要很长时间才能失败,则增大wait_timeout变量。

If it fails right away, enlarge the max_allowed_packet variable;如果立即失败,请增大max_allowed_packet变量; it it still doesn't work, make sure the command is valid SQL.它仍然不起作用,请确保该命令是有效的 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.此外,如果可行,请考虑将单个 SQL 命令的插入次数限制为 1000 次。您可以创建一个脚本,通过每 n 次插入重新引入 INSERT... 部分,从单个语句中创建多个语句。

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.我遇到了类似的错误..要解决这个问题,只需打开 my.ini 文件..在第 36 行更改最大允许数据包大小的值,即。 max_allowed_packet = 20M max_allowed_pa​​cket = 20M

Make sure mysqld process does not restart because of service managers like systemd.确保 mysqld 进程不会因为 systemd 等服务管理器而重新启动。

I had this problem in vagrant with centos 7. Configuration tweaks didn't help.我在使用 centos 7 的 vagrant 中遇到了这个问题。配置调整没有帮助。 Turned out it was systemd which killed mysqld service every time when it took too much memory.原来是 systemd 每次占用太多内存时都会杀死 mysqld 服务。

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我今天在复制数据库时遇到了类似的错误(MySQL 服务器已经消失......),但是当我尝试重新启动 mysql.server restart 时出现错误

ERROR! The server quit without updating PID ...

This is how I solved it: I opened up Applications/Utilities/ and ran Activity Monitor我是这样解决的:我打开 Applications/Utilities/ 并运行 Activity Monitor

 quit mysqld

then was able to solve the error problem with然后能够解决错误问题

mysql.server restart

I am doing some large calculations which involves the mysql connection to stay long time and with heavy data.我正在做一些大型计算,其中涉及 mysql 连接以保持长时间和大量数据。 i was facing this "Mysql go away issue".我正面临这个“Mysql 消失问题”。 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.所以我尝试优化查询,但这对我没有帮助,然后我增加了默认设置为较低值的 mysql 变量限制。

wait_timeout max_allowed_packet wait_timeout max_allowed_pa​​cket

To the limit what ever suits to you it should be the Any Number * 1024(Bytes).对于适合您的限制,它应该是 Any Number * 1024(Bytes)。 you can login to terminal using ' mysql -u username - p ' command and can check and change for these variable limits.您可以使用“ mysql -u username -p ”命令登录到终端,并可以检查和更改这些变量限制。

For GoDaddy shared hosting对于 GoDaddy 共享主机

On GoDaddy shared hosting accounts, it is tricky to tweak the PHP.ini etc files.在 GoDaddy 共享主机账户上,调整 PHP.ini 等文件很棘手。 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.) (我刚刚成功上传了一个 3.8Mb .sql 文本文件,包含 3100 行和 145 列。在 phpMyAdmin 中使用 IMPORT 命令,我得到了可怕的MySQL 服务器已经消失的错误,没有进一步的信息。)

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.像 Matt 一样,我尝试了各种技巧,从以一口大小的块导出 MySQL 数据库,到编写将大型导入分解为较小导入的脚本。 But here is what worked:但这是有效的:

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

(2a) Under "Partial Backups" heading... (2a) 在“部分备份”标题下...
(2b) Under "Download a MySQL Database Backup" (2b) 在“下载 MySQL 数据库备份”下
(2c) Choose your database and download a backup (this step optional, but wise) (2c) 选择您的数据库并下载备份(此步骤可选,但明智)

(3a) Directly to the right of 2b, under heading "Restore a MySQL Database Backup" (3a) 直接在 2b 的右侧,在标题“Restore a MySQL Database Backup”下
(3b) Choose the .SQL import file from your local drive (3b) 从本地驱动器中选择 .SQL 导入文件
(3c) True happiness will be yours (shortly....) Mine took about 5 seconds (3c) 真正的幸福将属于你(很快……)我的花了大约 5 秒钟

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.我的数据库中的任何其他内容都没有受到影响——但这就是上面第 (2) 步旨在防止的内容。

Notes:笔记:
a.一种。 If you are unsure how to create a .SQL import file, use phpMyAdmin to export a table and modify that file structure.如果您不确定如何创建 .SQL 导入文件,请使用 phpMyAdmin 导出表并修改该文件结构。

SOURCE: Matt Butcher 2010 Article来源: Matt Butcher 2010 年的文章

If increasing max_allowed_packet doesn't help.如果增加max_allowed_packet没有帮助。

I was getting the same error as you when importing a .sql file into my database via Sequel Pro.通过 Sequel Pro 将.sql文件导入我的数据库时,我遇到了与您相同的错误。

The error still persisted after upping the max_allowed_packet to 512M so I ran the import in the command line instead with:max_allowed_packet512M后错误仍然存​​在,所以我在命令行中运行了导入,而不是:

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:我发现了几个有用的 StackOverflow 问题:

In my case, my .sql file was a little corrupt or something.就我而言,我的.sql文件有点损坏或什么的。 The MySQL dump we get comes in two zip files that need to be concatenated together and then unzipped.我们得到的 MySQL 转储有两个 zip 文件,需要将它们连接在一起然后解压缩。 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.获取一个新的 MySQL 转储并解压缩它对我来说正常工作。

Just wanted to add this here in case others find that increasing the max_allowed_packet variable was not helping.只是想在这里添加这个以防其他人发现增加max_allowed_packet变量没有帮助。

None of the solutions regarding packet size or timeouts made any difference for me.关于数据包大小或超时的解决方案都没有对我产生任何影响。 I needed to disable ssl我需要禁用 ssl

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

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

For AWS RDS Adjust the Customer Owned Parameter Group对于 AWS RDS 调整客户拥有的参数组

MySQL Server has gone away when importing large sql file MySQL 导入大 sql 文件时服务器已经消失

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM