Transfer: https://www.cnblogs.com/ay-a/p/10520425.html
MySql idle connection is automatically disconnected problem caused 8 hours
I. Description of the problem
Recently encountered a strange problem MySql database, historical data at night several times a day before also normal storage, the next morning to see real-time data storage as normal, historical data is not stored. For a long time I did not find the problem. Later careful thought, a change in the historical data set was stored, the data is not been the same, it has not write data to the database, resulting in long MySql connections do not automatically disconnected. Then Baidu a bit length Mysql valid idle connections, a look at say 8 hours is automatically disconnected. Ah, feeling a little bit, it should be is the reason.
Second, Troubleshooting
According to the information found in MySql View has two parameters, the effective duration can be set idle connection, respectively, interactive_timeout
and wait_timeout
may be provided in the mysql configuration file. According to this, the investigation can be carried out.
- Use the
show variables like '%timeout%';
command to query the values of the above two parameters, a check is 28800 (in seconds, just 8 hours). - Mysql modify the configuration file
interactive_timeout
andwait_timeout
are 100, 100 seconds without using so connected automatically closed. - Mysql restart the service to start inserting data into the database, intermediate pause 3 minutes (to ensure that an idle connection has been disconnected), you can use the
show status like 'Threads%'
command to view the current number of connections, the query resultThreads_connected
is the number of current connections. - Continue inserting data into mysql, really did not look at the data on a storage problem successfully reproduce and find out why.
Third, to solve the problem
- See the code data storage section, the code found there to disconnect and reconnect to do, but judges that the connection is valid partial write a problem, there is no check
mysql_query
return value, the data is stored when no failure log. - Modify the code, automatically reconnect the disconnected, check
mysql_query
the log and generating a return value, so that each database operation result has been recorded. Note: mysql c / c ++ APImysql_query
returns 0 for a successful operation, non-zero for the operation failed.
I. Description of the problem
Recently encountered a strange problem MySql database, historical data at night several times a day before also normal storage, the next morning to see real-time data storage as normal, historical data is not stored. For a long time I did not find the problem. Later careful thought, a change in the historical data set was stored, the data is not been the same, it has not write data to the database, resulting in long MySql connections do not automatically disconnected. Then Baidu a bit length Mysql valid idle connections, a look at say 8 hours is automatically disconnected. Ah, feeling a little bit, it should be is the reason.
Second, Troubleshooting
According to the information found in MySql View has two parameters, the effective duration can be set idle connection, respectively, interactive_timeout
and wait_timeout
may be provided in the mysql configuration file. According to this, the investigation can be carried out.
- Use the
show variables like '%timeout%';
command to query the values of the above two parameters, a check is 28800 (in seconds, just 8 hours). - Mysql modify the configuration file
interactive_timeout
andwait_timeout
are 100, 100 seconds without using so connected automatically closed. - Mysql restart the service to start inserting data into the database, intermediate pause 3 minutes (to ensure that an idle connection has been disconnected), you can use the
show status like 'Threads%'
command to view the current number of connections, the query resultThreads_connected
is the number of current connections. - Continue inserting data into mysql, really did not look at the data on a storage problem successfully reproduce and find out why.
Third, to solve the problem
- See the code data storage section, the code found there to disconnect and reconnect to do, but judges that the connection is valid partial write a problem, there is no check
mysql_query
return value, the data is stored when no failure log. - Modify the code, automatically reconnect the disconnected, check
mysql_query
the log and generating a return value, so that each database operation result has been recorded. Note: mysql c / c ++ APImysql_query
returns 0 for a successful operation, non-zero for the operation failed.