Article Directory
- I. Introduction
- Second, understand wait_timeout and interactive_timeout two parameters
- 1, command line operation
- 2, wait_time set failures
- 3, Reference Manual concept, explain two parameters
- 4, then what regarded as an interactive, what it regarded as non-interactive
- Three, python reproduce mysql server has gone away
- 1, two test parameters through code Effect
- (1) When the parameters are set to two time 10s:
- (2)当wait_timeout =10,而interactive_timeout =1000
- (3)当interactive_timeout =10,而wait_timeout =1000
- (4) parameters are provided two 10s, program sleep (15) to see the effect
- 2, reconnection is not provided, the direct implementation of the corresponding look sql
- 3, summary
- Fourth, the complete code capture mysql error and reconnect
I. Introduction
In this chapter show through the python
connected database program errors, which leads to mysql
the wait_timeout
parameters and interactive_timeout
concepts, and then reproduce the program error, to solve the problem at its roots.
Requirements:
python
script file to listen, when to increase the number of files row 10
line is executed once the database persistence operations. Sometimes just the file may take several minutes to increase 10
the line, this time to operate the database appears: MySQL server has gone away
error, and the script is stopped.
Based on the above error, the first thing to reproduce the MySQL server has gone away
situation and see which parameter caused the error is caught by what should be which way. It casually Baidu will be able to know the impact parameter database connection time is generally wait_timeout
and interactive_timeout
two parameters, the following will focus reproduce some mistakes, and summarize the problems encountered
How to capture a variety of errors on python mysql, you can refer to my previous article: python how to capture mysql error
Second, understand wait_timeout and interactive_timeout two parameters
To reproduce the error, we set two parameters are 10s
, that is, 10s
the database is automatically disconnected, take a look at the command line first test.
1, command line operation
Whether or set wait_timeout set interactive_timeout = 10, the command line will execute sql error:
Error 1:
ERROR 2013 (HY000): Lost connection to MySQL server during query
Re-execute sql, it will reconnect error at this point is what we want gone away
Error 2:
mysql> show variables like '%timeout%';
ERROR 2006 (HY000): MySQL server has gone away
No connection. Trying to reconnect...
Connection id: 27
Current database: *** NONE ***
Originally thought will directly report: MySQL server has gone away
mistakes, I did not expect to lose first reported that the database connection errors, bloggers wonder whether these two parameters affect two errors generated.
2, wait_time set failures
The beginning kinda strange, obviously the set wait_time =10
, interactive_timeout =1000
but when the actual view, and always found wait_time =1000
a really odd strange, why this parameter will change ourselves?
The question of amending wait_timeout not in effect:
https://www.cnblogs.com/azhqiang/p/5454000.html
Can be found through this link, in fact, wait_timeout
it is affected by interactive_timeout
the impact, but why is there such a situation?
3, Reference Manual concept, explain two parameters
interactive_timeout:
The number of seconds the server waits for activity on an interactive connection before closing it. An interactive client is defined as a client that uses the CLIENT_INTERACTIVE option to mysql_real_connect()
wait_timeout:
The number of seconds the server waits for activity on a noninteractive connection before closing it. On thread startup, the session wait_timeout value is initialized from the global wait_timeout value or from the global interactive_timeout value,
depending on the type of client (as defined by the CLIENT_INTERACTIVE connect option to mysql_real_connect())
They are session/global
level, simply means that the former is used to describe an interactive client idle timeout, which is used for non-interactive client idle timeout, but here also reveals, if it is an interactive client connections session
that wait_timeout
will It is interactive_timeout
overwritten, in other words if the non-interactive client connection session
will not use interactive_timeout
overwrite wait_timeout
, i.e. interactive_timeout
there is no effect of the. Once the session successful landing, if you want to modify the session level timeout parameters, regardless of interactive or non-interactive is to modify wait_timeout
the parameters to take effect.
reference:
http://blog.itpub.net/7728585/viewspace-2637237/
This article resolved quite clear, very vivid example, in conclusion, that is:
If it is an interactive client connections session
it wait_timeout
will be interactive_timeout
overwritten, in other words if the non-interactive client connections session
will not be used interactive_timeout
to cover out wait_timeout
, that is, interactive_timeout
without any role in the.
4, then what regarded as an interactive, what it regarded as non-interactive
Interactive operation:
In layman's terms, is that you open on your local machine mysql
client, is that black window, black window in various sql
operations, of course, is certainly taking the tcp
agreement. (Small black box mode)
Non-interactive operation:
is your calling program in your project. For example, one side is the tomcat web
server, while the database server, both how communication? In java web
, we tend to choose hibernate
either jdbc
to connect. So this time is non-interactive operation. (Connection Code)
According to the above, the command line before testing belong interactive operation, that we should be is to connect the non-interactive connection, if you want the program to reproduce this problem, you need to set up non-interactive wait_time
value
Three, python reproduce mysql server has gone away
Which parameters will result in the end 错误1
, what parameters will result in 错误2
yet. When the command-line test, it was found always comes first 错误1
, again 错误2
, the two whether the error is affected by two separate parameters of it? Out how to reproduce what we want: mysql server has gone away
error.
By testing the code, do not continue to test the command line. Code for non-interactive operation.
1, two test parameters through code Effect
(1) When the parameters are set to two time 10s:
try:
conn.ping()
except MySQLdb.Error, e:
print 'error'
# sqlError = "Error %d:%s" % (e.args[0], e.args[1])
print e.args[0]
print e.args[1]
try:
conn.ping()
except MySQLdb.OperationalError,e:
print 'error1'
print e
pass
This code is printed as follows:
error
2013
Lost connection to MySQL server during query
error1
(2006, 'MySQL server has gone away')
After testing, or did not directly MySQL server has gone away
wrong, are the first reported loss of database connection, and then reconnect, this time it reported:MySQL server has gone away
(2)当wait_timeout =10,而interactive_timeout =1000
error
2013
Lost connection to MySQL server during query
2013
error1
(2006, 'MySQL server has gone away')
2222_2
And the results of the above agreement, and no separate report MySQL server has gone away
errors.
(3)当interactive_timeout =10,而wait_timeout =1000
No error, normal program execution. When the code connection mysql
time, corresponding to non-interactive, so at this time interactive_timeout
corresponds useless, with the wait_timeout
value, the program is not being given.
(4) parameters are provided two 10s, program sleep (15) to see the effect
import time
time.sleep(15)
error
2013
Lost connection to MySQL server during query
2013
error1
(2006, 'MySQL server has gone away')
Through frequent testing found that appear 错误1
and 错误2
does not seem to manipulate these two parameters, because these two errors always appear together, which is the original conjecture different. But think about it is, you want to operate Mysql
, first check whether there are certainly sub-thread and found that they reported there is no mistake, and then reconnect the failure to report a mistake, more logical
2, reconnection is not provided, the direct implementation of the corresponding look sql
try:
cursor.execute(insert_sql) # 直接执行sql
except MySQLdb.Error, e:
print 'error'
# sqlError = "Error %d:%s" % (e.args[0], e.args[1])
print e.args[0]
print e.args[1]
print e[0]
try:
cursor.execute(insert_sql)
except MySQLdb.OperationalError, e:
print 'error1'
print e
pass
Print results are as follows:
error
2006
MySQL server has gone away
2006
error1
(2006, 'MySQL server has gone away')
Found here, in direct execution sql
time, it will directly report MySQL server has gone away
errors. OK, an error has been reproduced.
3, summary
This is not an error interactive_timeout
and wait_timeout
these two parameters affect. It is estimated that, mysql
in the direct execution sql
time, the thread pool will take the initiative to look for the child, can not find the time to throw an Lost connection to MySQL server during query
error, and then try to reconnect when throwing MySQL server has gone away
error.
We are above code has been trying to reconnect, so the first throw 错误1
, then throw 错误2
. The direct execution sql
time, 错误1
is 错误2
to cover, so print out MySQL server has gone away
, in fact, 错误1
still exist.
Fourth, the complete code capture mysql error and reconnect
code show as below:
try:
cursor.execute(insert_sql)
except MySQLdb.OperationalError: # 先检测数据库连接的问题,检测无误则执行sql
print 'connect error1'
cursor = MakeDbConnection() # 重新连接sql,函数里面的是connect()方法
try:
cursor.execute(insert_sql)
except MySQLdb.ProgrammingError: # 捕获sql语句执行的错误
try:
xxxxxxxxxxxxxxx # 根据各自的需求,重新调整下
cursor.execute(insert_sql) #重新执行sql
except:
pass
except UnicodeDecodeError: # 捕获编码错误,如捕获代码不是utf-8的错误
pass
So you can avoid the loss of a database connection error, to capture the connection fails, reconnect, and then execute the code can be, my mother no longer have to worry about my own stopped the script.
end