当我试图获取一个大的SQL文件(一个大的INSERT查询)时,我得到这个错误。

mysql>  source file.sql
ERROR 2006 (HY000): MySQL server has gone away
No connection. Trying to reconnect...
Connection id:    2
Current database: *** NONE ***

ERROR 2006 (HY000): MySQL server has gone away
No connection. Trying to reconnect...
Connection id:    3
Current database: *** NONE ***

表中没有任何内容被更新。我尝试了删除和恢复表/数据库,以及重新启动MySQL。这些都不能解决问题。

这是我的最大数据包大小:

+--------------------+---------+
| Variable_name      | Value   |
+--------------------+---------+
| max_allowed_packet | 1048576 |
+--------------------+---------+

下面是文件大小:

$ ls -s file.sql 
79512 file.sql

当我尝试另一种方法时……

$ ./mysql -u root -p my_db < file.sql
Enter password: 
ERROR 2006 (HY000) at line 1: MySQL server has gone away

当前回答

如果您已经尝试了所有这些解决方案,特别是将max_allowed_packet增加到最大支持的1GB,您仍然看到这些错误,这可能是您的服务器没有足够的空闲RAM内存可用…

解决方案=将服务器升级到更大的RAM内存,然后再试一次。

注意:我很惊讶这个简单的解决方案在这个话题上讨论了8年多之后还没有被提及……有时候我们开发人员会想太多。

其他回答

当您创建的SCHEMA的COLLATION与转储中使用的不同时,也会出现此错误消息。因此,如果转储包含

CREATE TABLE `mytab` (
..
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;

你也应该在SCHEMA排序中反映这一点:

CREATE SCHEMA myschema COLLATE utf8_unicode_ci;

我一直在模式中使用utf8mb4_general_ci,因为我的脚本来自一个新的V8安装,现在在旧5.7上加载一个DB崩溃了,几乎把我逼疯了。

所以,也许这能帮你节省一些令人沮丧的时间……: -)

(MacOS 10.3, mysql 5.7)

一般的错误是:

错误:2006 (CR_SERVER_GONE_ERROR) - MySQL服务器已经离开

意味着客户端不能向服务器发送问题。


mysql进口

在您通过mysql导入数据库文件的特定情况下,这很可能意味着SQL文件中的一些查询太大而无法导入,并且它们无法在服务器上执行,因此客户端在第一次出现错误时失败。

所以你有以下几种可能性:

为mysql添加force选项(-f)以继续执行其余的查询。 如果数据库有一些与缓存相关的大型查询,这是非常有用的。 增加服务器配置中的max_allowed_packet和wait_timeout(例如~/.my.cnf)。 使用——skip-extended-insert选项来分解大型查询来转储数据库。然后重新导入。 尝试为mysql应用——max-allowed-packet选项。


常见的原因

一般来说,这个错误可能意味着以下几种情况:

a query to the server is incorrect or too large, Solution: Increase max_allowed_packet variable. Make sure the variable is under [mysqld] section, not [mysql]. Don't afraid to use large numbers for testing (like 1G). Don't forget to restart the MySQL/MariaDB server. Double check the value was set properly by: mysql -sve "SELECT @@max_allowed_packet" # or: mysql -sve "SHOW VARIABLES LIKE 'max_allowed_packet'" You got a timeout from the TCP/IP connection on the client side. Solution: Increase wait_timeout variable. You tried to run a query after the connection to the server has been closed. Solution: A logic error in the application should be corrected. Host name lookups failed (e.g. DNS server issue), or server has been started with --skip-networking option. Another possibility is that your firewall blocks the MySQL port (e.g. 3306 by default). The running thread has been killed, so retry again. You have encountered a bug where the server died while executing the query. A client running on a different host does not have the necessary privileges to connect. And many more, so learn more at: B.5.2.9 MySQL server has gone away.


调试

以下是一些专家级调试思路:

检查日志,例如: sudo tail -f $(mysql -Nse "SELECT @@GLOBAL.log_error") 通过mysql, telnet或ping函数(例如PHP中的mysql_ping)测试您的连接。 使用tcpdump嗅探MySQL通信(不适用于套接字连接),例如: Sudo tcpdump -i lo0 -s 1500 -nl -w- port mysql |字符串 在Linux操作系统上,请使用strace。在BSD/Mac上使用dtrace/dtruss,例如: Sudo dtruss -a -fn mysqld 2>&1 参见:开始使用DTracing MySQL

了解更多如何调试MySQL服务器或客户端:26.5调试和移植MySQL。

作为参考,请检查sql-common/client.c文件中负责抛出客户端命令CR_SERVER_GONE_ERROR错误的源代码。

MYSQL_TRACE(SEND_COMMAND, mysql, (command, header_length, arg_length, header, arg));
if (net_write_command(net,(uchar) command, header, header_length,
          arg, arg_length))
{
  set_mysql_error(mysql, CR_SERVER_GONE_ERROR, unknown_sqlstate);
  goto end;
}

我也有同样的问题,但改变my.ini/my.cnf文件下[mysqld]的max_allowed_packet。

添加一条线

max_allowed_packet=500M

现在重新启动MySQL服务,一旦你完成。

如何使用mysql客户端像这样:

mysql -h <hostname> -u username -p <databasename> < file.sql

对于寻找DB导入失败解决方案的Drupal 8用户:

在sql转储文件的末尾,可以命令插入数据到“webprofiler”表。 这是我猜一些调试日志文件,并不是真正重要的网站工作,所以所有这些可以删除。我删除了所有这些插入,包括LOCK TABLES和UNLOCK TABLES(以及两者之间的所有内容)。它在sql文件的最底部。问题描述如下:

https://www.drupal.org/project/devel/issues/2723437

但是除了截断该表之外,没有其他解决方案。

顺便说一句,我尝试了以上答案中的所有解决方案,其他都没有帮助。