当我试图获取一个大的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

当前回答

一般的错误是:

错误: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;
}

其他回答

如果它正在重新连接并获得连接ID 2,那么服务器几乎肯定已经崩溃了。

联系服务器管理员,让他们诊断问题。任何非恶意SQL都不应该导致服务器崩溃,mysqldump的输出当然也不应该。

这可能是由于服务器管理员犯了一些较大的操作错误,例如分配的缓冲区大小大于体系结构的地址空间限制,或者大于虚拟内存容量。MySQL错误日志可能会有一些相关的信息;无论如何,如果他们有能力,他们将会监督这一点。

这是一个比较罕见的问题,但我看到过,如果有人复制了整个/var/lib/mysql目录,作为将他们的DB迁移到另一个服务器的一种方式。它不起作用的原因是数据库正在运行并使用日志文件。如果/var/log/mysql.中有日志,它有时会不起作用解决方案是复制/var/log/mysql文件。

这里可能会发生一些事情;

插入运行时间过长,客户端正在断开连接。当它重新连接时,它没有选择数据库,因此出现了错误。这里的一个选项是从命令行运行批处理文件,并在参数中选择数据库,如下所示;

$ mysql db_name < source.sql

另一种方法是通过php或其他语言运行命令。在每个长时间运行的语句之后,您可以关闭并重新打开连接,以确保在每个查询开始时都已连接。

当您创建的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)

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

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