一般的错误是:
错误: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;
}