python mysql server has gone away to reproduce the error and solutions

Disclaimer: This article is a blogger original article, follow the CC 4.0 BY-SA copyright agreement, reproduced, please attach the original source link and this statement.
This link: https://blog.csdn.net/LJFPHP/article/details/102751743

I. Introduction

       In this chapter show through the pythonconnected database program errors, which leads to mysqlthe wait_timeoutparameters and interactive_timeoutconcepts, 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 10line is executed once the database persistence operations. Sometimes just the file may take several minutes to increase 10the line, this time to operate the database appears: MySQL server has gone awayerror, and the script is stopped.

       Based on the above error, the first thing to reproduce the MySQL server has gone awaysituation 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_timeoutand interactive_timeouttwo 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, 10sthe 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 awaymistakes, 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 =1000but when the actual view, and always found wait_time =1000a 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_timeoutit is affected by interactive_timeoutthe 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/globallevel, 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 sessionthat wait_timeoutwill It is interactive_timeoutoverwritten, in other words if the non-interactive client connection sessionwill not use interactive_timeoutoverwrite wait_timeout, i.e. interactive_timeoutthere 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_timeoutthe 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 sessionit wait_timeoutwill be interactive_timeoutoverwritten, in other words if the non-interactive client connections sessionwill not be used interactive_timeoutto cover out wait_timeout, that is, interactive_timeoutwithout 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 mysqlclient, is that black window, black window in various sqloperations, of course, is certainly taking the tcpagreement. (Small black box mode)

Non-interactive operation:
       is your calling program in your project. For example, one side is the tomcat webserver, while the database server, both how communication? In java web, we tend to choose hibernateeither jdbcto 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_timevalue

Three, python reproduce mysql server has gone away

       Which parameters will result in the end 错误1, what parameters will result in 错误2yet. 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 awayerror.

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 awaywrong, 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 awayerrors.

(3)当interactive_timeout =10,而wait_timeout =1000

       No error, normal program execution. When the code connection mysqltime, corresponding to non-interactive, so at this time interactive_timeoutcorresponds useless, with the wait_timeoutvalue, 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 错误1and 错误2does 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 sqltime, it will directly report MySQL server has gone awayerrors. OK, an error has been reproduced.

3, summary

       This is not an error interactive_timeoutand wait_timeoutthese two parameters affect. It is estimated that, mysqlin the direct execution sqltime, 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 queryerror, and then try to reconnect when throwing MySQL server has gone awayerror.

       We are above code has been trying to reconnect, so the first throw 错误1, then throw 错误2. The direct execution sqltime, 错误1is 错误2to cover, so print out MySQL server has gone away, in fact, 错误1still 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

Guess you like

Origin blog.csdn.net/LJFPHP/article/details/102751743