通過(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')"

這是因?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.另外有需要云服務(wù)器可以了解下創(chuàng)新互聯(lián)cdcxhl.cn,海內(nèi)外云服務(wù)器15元起步,三天無(wú)理由+7*72小時(shí)售后在線,公司持有idc許可證,提供“云服務(wù)器、裸金屬服務(wù)器、高防服務(wù)器、香港服務(wù)器、美國(guó)服務(wù)器、虛擬主機(jī)、免備案服務(wù)器”等云主機(jī)租用服務(wù)以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡(jiǎn)單易用、服務(wù)可用性高、性價(jià)比高”等特點(diǎn)與優(yōu)勢(shì),專為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應(yīng)用場(chǎng)景需求。
文章標(biāo)題:MySQLdb不活躍連接自動(dòng)斷開(kāi)的解決方法-創(chuàng)新互聯(lián)
文章位置:http://www.chinadenli.net/article12/deepgc.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供營(yíng)銷(xiāo)型網(wǎng)站建設(shè)、標(biāo)簽優(yōu)化、云服務(wù)器、建站公司、網(wǎng)站導(dǎo)航、做網(wǎng)站
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容