經過MySQLdb 鏈接mysql,若是長時間不活動,會被mysql斷開,再次請求的時候會致使拋出異常"_mysql_exceptions.OperationalError: (2006, 'MySQL server has gone away')"mysql
這是由於mysql有兩個參數來自動斷開不活躍的鏈接,MySQLdb的鏈接超過這個時間後就會被mysql自動斷開。sql
interactive_timeout
wait_timeoutide
能夠登錄mysql 執行show global variables like "%timeout%" 進行查看this
修改mysql的配置,對上面兩個參數進行修改code
使用conn的ping()方法進行從新鏈接orm
conn = MySQLdb.conn(xxxxx)
conn.ping(True)server
ping(...) Checks whether or not the connection to the server is working. If it has gone down, an automatic reconnection is attempted. This function can be used by clients that remain idle for a long while, to check whether or not the server has closed the connection and reconnect if necessary. New in 1.2.2: Accepts an optional reconnect parameter. If True, then the client will attempt reconnection. Note that this setting is persistent. By default, this is on in MySQL<5.0.3, and off thereafter. Non-standard. You should assume that ping() performs an implicit rollback; use only when starting a new transaction. You have been warned.