1. I recently developed an IM server in linux and encountered a problem that caused the server to crash: Problem description: The server runs for one night and the system crashes when I log on to the client the next morning, according to the log and gdb information, when mysql_ping () is executed, the crash occurs: (gdb) bt # 00x00110416in _ kernel_vsyscall ()
1. I recently developed an IM server in linux and encountered a problem that caused the server to crash: Problem description: The server runs for one night and the system crashes when I log on to the client the next morning, according to the log and gdb information, when mysql_ping () is executed, (gdb) bt #0 0x00110416 in _ kernel_vsyscall ()
1. I recently developed an IM server in linux and encountered a problem that caused the server to crash:
Problem description: The server runs for one night. the system crashes when you log on to the client the next morning,
According to the log and gdb information, the crash occurs when mysql_ping () is executed:
(Gdb) bt
#0 0x00110416 in _ kernel_vsyscall ()
#1 0x0092f918 in send () from/lib/libpthread. so.0
#2 0x000000e85 in vio_write () from/usr/local/mysql/lib/libmysqlclient. so.18
#3 0x00179a96 in net_write_packet () from/usr/local/mysql/lib/libmysqlclient. so.18
#4 0x00179d98 in net_flush () from/usr/local/mysql/lib/libmysqlclient. so.18
#5 0x00179f20 in net_write_command () from/usr/local/mysql/lib/libmysqlclient. so.18
#6 0x00175a18 in cli_advanced_command () from/usr/local/mysql/lib/libmysqlclient. so.18
#7 0x0016ae7d in mysql_ping () from/usr/local/mysql/lib/libmysqlclient. so.18
You can find out the following problems on the Internet:
The application establishes a connection with the database. If the application does not access the database for more than wait_timeout (8 hours by default) and the connection remains idle, MySQL automatically closes the connection, if you perform a query operation on the connection, the MySQL server has gone away error occurs.
Show variables like "% timeout % ";
One way is to set this value to a large value;
Another good solution is to use mysql_ping. After using mysql_real_connect to connect to the database, use mysql_options (& mysql, MYSQL_OPT_RECONNECT ,... To automatically reconnect. In this way, when the mysql connection is lost, you can use mysql_ping to automatically reconnect to the database. If it is before mysql 5.1.6, execute mysql_options (& mysql, MYSQL_OPT_RECONNECT ,... ), If it is mysql 5.1.6 +, it is enough to execute one operation before connect.
A natural idea is to open a new thread to execute mysql_ping every certain time (for example, 20 seconds). In addition, it does not need to do anything. But obviously, this thread must share a mysql connection with other threads and share the mysql handle. Otherwise, it makes no sense to do so.
Therefore, modify the Code as follows:
1. In the database initialization function, before mysql_real_connect is executed, use mysql_options (& mysql, MYSQL_OPT_RECONNECT ,... )
2. Add a new thread to regularly execute mysql_ping.
Problem Solving