Linuxc multi-threaded connection to the mysql database, each time it is a short connection, the connection is released after the operation, sometimes the phenomenon of mysql_real_connect hanging, hanging timeout mysql
Linux c multi-threaded connection to the mysql database is a short connection. After the operation, the connection is released. Sometimes, mysql_real_connect is suspended, and the timeout mysql
[Symptom ]:
1. Linux c multi-threaded connection to the mysql database. Every time it is a short connection, the connection will be released after the operation. Sometimes mysql_real_connect will be suspended.
2. When mysql_real_connect returns a timeout value, the following error is returned: Lostconnection to MySQL server at 'reading initial communication packet ', systemerror: 104. After the error is returned, the thread number does not change and continues to run.
[Initial Cause Analysis ]:
1. mysql_real_connect connects to the database and does not explicitly call the timeout time, reconnect or something. The default value is used:
2. yesterday, I analyzed and tested the source code of c mysqlclient and found the default values of the following configuration items (if no attribute is set before mysql_real_connect is called,/etc/my. cnf is not configured either) as follows:
Connect_timeout = 0
Read_timeout = 0
Write_timeout = 0
Reconnect = 0 // 1 indicates automatic Reconnection
Therefore, these parameters must be explicitly set.
{Net = {vio = 0x7b8f150, options = {connect_timeout = 0, read_timeout = 0, write_timeout = 0 ,}
3. The error code in Mysqlclient is as follows (failed to read the parsing package after establishing a connection with the server ):
/*
Part 1: Connection established, read and parse first packet
*/
If (pkt_length = cli_safe_read (mysql) = packet_error)
{
If (mysql-> net. last_errno = CR_SERVER_LOST)
Set_mysql_extended_error (mysql, CR_SERVER_LOST, unknown_sqlstate,
ER (CR_SERVER_LOST_EXTENDED ),
"Reading initial communication packet ",
Errno );
Fprintf (myfp, "% s after cli_safe_read packet_error \ n", cur_time ());
Goto error;
}
The stack captured when the thread is blocked is as follows:
#0 0x0000003ebe0c5f3b in read () from/lib64/libc. so.6
#1 0x00007f240dd91430 in vio_read () from/usr/lib/libmysql. so.16.0.0
#2 0x00007f240dcf9b05 in my_real_read () from/usr/lib/libmysql. so.16.0.0
#3 0x00007f240dcf9e38 in my_net_read () from/usr/lib/libmysql. so.16.0.0
#4 0x00007f240dcec396 in cli_safe_read () from/usr/lib/libmysql. so.16.0.0
#5 0x00007f240dceea17 in mysql_real_connect () from/usr/lib/libmysql. so.16.0.0