我试图通过phpMyAdmin导入一个大的sql文件…但它一直显示错误

“MySql服务器已经消失”

怎么办呢?


当前回答

我更新了“max_allowed_packet”到1024M,但它仍然不能工作。结果发现我的部署脚本正在运行:

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

如果要这样做,请确保从命令行显式地指定一个更大的数字。

其他回答

如果你使用默认值运行,那么你有很多空间来优化你的mysql配置。

我建议的第一步是将max_allowed_packet增加到128M。

然后下载MySQL Tuning Primer脚本并运行它。它将为您的配置提供几个方面的建议,以获得更好的性能。

另外,还要在MySQL和PHP中调整超时值。

你要导入的文件有多大(文件大小),你能使用mysql命令行客户端而不是PHPMyAdmin导入文件吗?

如下所述:

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: 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). 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.

编辑:

注意,MySQL选项文件没有它们的命令作为注释可用(例如在php.ini中)。所以你必须在my.cnf或my.ini中输入任何更改/调整,并将它们放在mysql/data目录或任何其他路径下,在适当的选项组下,如[client], [myslqd]等。例如:

[mysqld]
wait_timeout = 600
max_allowed_packet = 64M

然后重新启动服务器。要得到它们的值,在mysql客户端中输入:

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

我得到一个类似的错误。要解决这个问题,只需打开my.ini文件,在第36行更改最大允许数据包大小的值。max_allowed_packet = 20M

如果您的数据包括BLOB数据:

请注意,从命令行导入数据似乎会阻塞在BLOB数据上,导致'MySQL server has gone away'错误。

为了避免这种情况,重新创建mysqldump,但是使用——hex-blob标记:

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

这将写出数据文件与十六进制值,而不是二进制之间的其他文本。

PhpMyAdmin也有选项“转储十六进制符号的二进制列(例如,“abc”变成0x616263)”,这工作得很好。

请注意,存在一个长期存在的错误(截至2015年12月),这意味着GEOM列不能转换: 备份一个表几何列使用mysqldump? 所以使用像PhpMyAdmin这样的程序似乎是唯一的解决方案(上面提到的选项可以正确地转换GEOM列)。

发生这种情况的另一个原因是内存不足。检查/var/log/messages,确保你的my.cnf没有设置成导致mysqld分配比你的机器更多的内存。

您的mysqld进程实际上可以被内核杀死,然后由“safe_mysqld”进程重新启动,而您却没有意识到这一点。

使用top并在它运行时观察内存分配,看看您的净空是多少。

在更改my.cnf之前对其进行备份。