最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Mysql的timeout以及python重連方式

 更新時間:2024年07月18日 08:59:37   作者:有人找你  
這篇文章主要介紹了Mysql的timeout以及python重連方式,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教

 問題

近期使用多線程對數(shù)據(jù)庫操作報錯:

(2013, 'Lost connection to MySQL server during query')

 解決方案

1. 更改timeout參數(shù)

出現(xiàn)這個問題順便就了解了一下mysql中的timeout都有什么:

mysql> show variables like '%timeout%';
+-----------------------------+----------+
| Variable_name               | Value    |
+-----------------------------+----------+
| connect_timeout             | 10       |
| delayed_insert_timeout      | 300      |
| have_statement_timeout      | YES      |
| innodb_flush_log_at_timeout | 1        |
| innodb_lock_wait_timeout    | 50       |
| innodb_rollback_on_timeout  | OFF      |
| interactive_timeout         | 28800    |
| lock_wait_timeout           | 31536000 |
| net_read_timeout            | 30       |
| net_write_timeout           | 60       |
| rpl_stop_slave_timeout      | 31536000 |
| slave_net_timeout           | 60       |
| wait_timeout                | 28800    |
+-----------------------------+----------+
13 rows in set (0.06 sec)

很多參數(shù),網上對于connection lost這個問題有說改wait_timout也有說net_read_timeout和net_write_timeout的。

那么他們之間到底有什么區(qū)別呢?

簡單來說:

  • wait_timeout代表的是前一個sql操作和下一個sql操作的間隔。
  • net_read_timeout和net_write_timeout涉及到網絡傳輸中的包的問題。通常情況下,一個sql語句由多個包組成。客戶端將一個一個包傳給服務端,然后在服務端再將包組合起來變?yōu)閟ql語句執(zhí)行。如果網絡情況太差,導致包遲遲不全,無法組成一個完整的可執(zhí)行語句,mysql服務端就會放棄這個語句的執(zhí)行。而這個等待時間就是net_read/write_timeout。
  • read是服務器從客戶端讀取;write是服務器向客戶端寫。
  • 這兩個參數(shù)都是session level的,可以在每次連接的時候對其進行更改。
  • 此外,需要注意的是,如果問題出在傳輸執(zhí)行一個sql的過程中時,使用ping是無法解決的。Mysql協(xié)議不允許客戶端在一次性數(shù)據(jù)沒有傳輸完畢的時候,向服務器傳輸其他的請求。(python里面的處理就是,如果使用了ping就自動斷開了本次的執(zhí)行操作)

This for example typical cause of aborted connections while using Sphinx with large data sets and large buffers. While indexing Sphinx performs sorts and flushes buffers to the disk every so often which can take long enough to trigger default net_write_timeout on the server side.

You could ask why server does not do any flow control and can’t find out client just is just busy and it is not network issue – well this comes from simplicity of MySQL protocol which does not allow client to talk to the server until full result set is fetched. This is also the reason why you can’t stop fetching for ordinary statements and mysql_free_result will have to complete the fetch internally.

Is there any way to tell the server you need more time besides increasing net_write_timeout ? Not what I know of. You can’t use something like mysql_ping because connection is in the stage of getting the data. You can’t even fetch couple of rows every few seconds to show you’re fetching data because there is buffering happening inside MySQL client library so you never know when real network read will happen.

具體改什么還要視自己的情況而定:

詳細的net write timeout和wait timeout的比較

2. 測試連接,并將斷開的連接重新連接

還有另一個辦法,python使用ping()方法測試連接。

但是要注意ping方法不能在使用流式游標進行迭代獲取數(shù)據(jù)的過程中用,

否則會報錯:

UserWarning: Previous unbuffered result was left incomplete warnings.warn("Previous unbuffered result was left incomplete")

ping()應用于兩個sql語句執(zhí)行之間進行查詢,

這個檢查是為了應付由于超出wait_timeout(而非net_read/write_timeout)服務器自動斷開鏈接的。

總結

以上為個人經驗,希望能給大家一個參考,也希望大家多多支持腳本之家。

相關文章

最新評論

伊金霍洛旗| 耒阳市| 牟定县| 郎溪县| 仙桃市| 庄浪县| 新乡市| 鲁山县| 昌图县| 陵川县| 阿克苏市| 雷山县| 龙游县| 高邮市| 花莲市| 湖北省| 南宫市| 南丹县| 讷河市| 汉阴县| 抚松县| 泸水县| 通化县| 聊城市| 泽库县| 辽源市| 阳朔县| 吐鲁番市| 凉城县| 连州市| 通渭县| 海城市| 临邑县| 南安市| 平江县| 祥云县| 武鸣县| 合肥市| 遂平县| 广元市| 胶南市|