通過(guò)MySQLdb 連接mysql,如果長(zhǎng)時(shí)間不活動(dòng),會(huì)被mysql斷開(kāi),再次請(qǐng)求的時(shí)候會(huì)導(dǎo)致拋出異常"_mysql_exceptions.OperationalError: (2006, 'MySQL server has gone away')"
我們提供的服務(wù)有:網(wǎng)站建設(shè)、成都網(wǎng)站建設(shè)、微信公眾號(hào)開(kāi)發(fā)、網(wǎng)站優(yōu)化、網(wǎng)站認(rèn)證、天門ssl等。為超過(guò)千家企事業(yè)單位解決了網(wǎng)站和推廣的問(wèn)題。提供周到的售前咨詢和貼心的售后服務(wù),是有科學(xué)管理、有技術(shù)的天門網(wǎng)站制作公司
這是因?yàn)閙ysql有兩個(gè)參數(shù)來(lái)自動(dòng)斷開(kāi)不活躍的連接,MySQLdb的連接超過(guò)這個(gè)時(shí)間后就會(huì)被mysql自動(dòng)斷開(kāi)。
interactive_timeout
wait_timeout
可以登陸mysql 執(zhí)行show global variables like "%timeout%" 進(jìn)行查看
修改mysql的配置,對(duì)上面兩個(gè)參數(shù)進(jìn)行修改
使用conn的ping()方法進(jìn)行重新連接
conn = MySQLdb.conn(xxxxx)
conn.ping(True)
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.