`

mysql 2006 错误

阅读更多

使用libmysqlclient出现core,日志显示

mysql 遇到2013 ,2006错误(MySQL server has gone away)

core文件信息如下:

 

(gdb) bt
#0  0x000000000051eb12 in my_real_read (net=0x7fc8e80009e0, complen=0x7fc9ce1fb438) at /opt/mysql-5.5.41/sql/net_serv.cc:839
#1  0x000000000051eed5 in my_net_read (net=0x7fc8e80009e0) at /opt/mysql-5.5.41/sql/net_serv.cc:1034
#2  0x00000000005081aa in cli_safe_read (mysql=0x7fc8e80009e0) at /opt/mysql-5.5.41/sql-common/client.c:735
#3  0x00000000005088b2 in cli_read_query_result (mysql=0x7fc8e80009e0) at /opt/mysql-5.5.41/sql-common/client.c:3837
#4  0x000000000050622c in mysql_real_query (mysql=0x7fc8e80009e0, query=<value optimized out>, length=<value optimized out>)
    at /opt/mysql-5.5.41/sql-common/client.c:3924

 

(gdb) p *net
$10 = {vio = 0x0, buff = 0x0, buff_end = 0x7fc8e80071a0 "\020b", write_pos = 0x7fc8e80051a0 "\n", read_pos = 0x7fc8e80051a0 "\n", fd = 43, 
  remain_in_buf = 0, length = 0, buf_length = 0, where_b = 0, max_packet = 8192, max_packet_size = 1073741824, pkt_nr = 2, compress_pkt_nr = 2, 
  write_timeout = 31536000, read_timeout = 31536000, retry_count = 1, fcntl = 0, return_status = 0x0, reading_or_writing = 0 '\000', 
  save_char = 0 '\000', unused1 = 0 '\000', unused2 = 0 '\000', compress = 0 '\000', unused3 = 0 '\000', unused = 0x0, last_errno = 2006, 
  error = 0 '\000', unused4 = 0 '\000', unused5 = 0 '\000', 
  last_error = "MySQL server has gone away\000rver during query", '\000' <repeats 467 times>, sqlstate = "HY000", extension = 0x0}

 

 

上面NET->vio指针为NULL,因此出core,这个vio是用于vector io的。

解决办法:

http://dev.mysql.com/doc/refman/5.6/en/gone-away.html 写道
Some other common reasons for the MySQL server has gone away error are:

You (or the db administrator) has killed the running thread with a KILL statement or a mysqladmin kill command.

You tried to run a query after closing the connection to the server. This indicates a logic error in the application that should be corrected.

A client application running on a different host does not have the necessary privileges to connect to the MySQL server from that host.

You got a timeout from the TCP/IP connection on the client side. This may happen if you have been using the commands: mysql_options(..., MYSQL_OPT_READ_TIMEOUT,...) or mysql_options(..., MYSQL_OPT_WRITE_TIMEOUT,...). In this case increasing the timeout may help solve the problem.
即相应mysql变量中设置NET_READ_TIMEOUT,NET_WRITE_TIMEOUT(如:SET GLOBAL NET_READ_TIMEOUT=600)
You have encountered a timeout on the server side and the automatic reconnection in the client is disabled (the reconnect flag in the MYSQL structure is equal to 0).

You are using a Windows client and the server had dropped the connection (probably because wait_timeout expired) before the command was issued.
即相应mysql变量中设置WAIT_TIME_OUT (如:SET GLOBAL WAIT_TIMEOUT= 86400)
The problem on Windows is that in some cases MySQL does not get an error from the OS when writing to the TCP/IP connection to the server, but instead gets the error when trying to read the answer from the connection.

The solution to this is to either do a mysql_ping() on the connection if there has been a long time since the last query (this is what Connector/ODBC does) or set wait_timeout on the mysqld server so high that it in practice never times out.

You can also get these errors if you send a query to the server that is incorrect or too large. If mysqld receives a packet that is too large or out of order, it assumes that something has gone wrong with the client and closes the connection. If you need big queries (for example, if you are working with big BLOB columns), you can increase the query limit by setting the server's max_allowed_packet variable, which has a default value of 4MB (1MB before MySQL 5.6.6). You may also need to increase the maximum packet size on the client end. More information on setting the packet size is given in Section B.5.2.10, “Packet Too Large”.

An INSERT or REPLACE statement that inserts a great many rows can also cause these sorts of errors. Either one of these statements sends a single request to the server irrespective of the number of rows to be inserted; thus, you can often avoid the error by reducing the number of rows sent per INSERT or REPLACE.

You also get a lost connection if you are sending a packet 16MB or larger if your client is older than 4.0.8 and your server is 4.0.8 and above, or the other way around.

It is also possible to see this error if host name lookups fail (for example, if the DNS server on which your server or network relies goes down). This is because MySQL is dependent on the host system for name resolution, but has no way of knowing whether it is working—from MySQL's point of view the problem is indistinguishable from any other network timeout.

You may also see the MySQL server has gone away error if MySQL is started with the --skip-networking option.

Another networking issue that can cause this error occurs if the MySQL port (default 3306) is blocked by your firewall, thus preventing any connections at all to the MySQL server.

You can also encounter this error with applications that fork child processes, all of which try to use the same connection to the MySQL server. This can be avoided by using a separate connection for each child process.

You have encountered a bug where the server died while executing the query.

但 采用官方的说法,未能解决问题。还是在程序中进行判断,至少避免core dump错误:

int err = mysql_errorno(mysql_con);
if(mysql_con && err==2006 && err == 2013)
        {
            mysql_close(mysql_con);
            mysql_con =NULL;
        }
//TODO: Reconnet to mysql

 

  • 给mysql连接,添加自动连接,问题再没有发生。发生这种故障,有可能是超时断了连接。设置方式:
  •         my_bool my_true= TRUE;
            mysql_options(mysql_con, MYSQL_OPT_RECONNECT, &my_true);
     另外,可以结合mysql_ping。如果发现已经端口,再重新连接。
    int err_code = mysql_ping(mysql_con);
     

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics