線上 SQLAlchemy資料庫連線滿導致的問題

2021-10-23 09:30:55 字數 4148 閱讀 7370

這是剛做superset專案時遇到的線上問題,現回顧並整理下。

訪問高峰qps增大,然後資料庫連線占用超過了sqlalchemy設定的預設最大數量,且達到超時時間,這導致線上很多使用者看到如下報錯

2018-11

-0411:

27:50,

370/var/www/html/superset/views/base.py base.py[line:56]

: error queuepool limit of size 10 overflow 10 reached, connection timed out, timeout 30

(self.size(

), self.overflow(

), self._timeout)

)(self.size(

), self.overflow(

), self._timeout)

)timeouterror: queuepool limit of size 10 overflow 10 reached, connection timed out, timeout 30

timeouterror: queuepool limit of size 10 overflow 10 reached, connection timed out, timeout 30

timeouterror: queuepool limit of size 10 overflow 10 reached, connection timed out, timeout 30

timeouterror: queuepool limit of size 10 overflow 10 reached, connection timed out, timeout 30

(self.size(

), self.overflow(

), self._timeout)

)

queuepool limit of size overflow reached, connection timed out, timeout 問題

主要是mysql資料庫方面的知識

資料庫的一些系統變數

show variables like '%connect%'

;

可能得到如下,對連線的一些超時,最大限制可以看到;連線數等也是dba監控必然有的一項

+

----

----

----

----

----

----

----

----

----

----

-------

+------

----

-------

+| variable_name | value |+--

----

----

----

----

----

----

----

----

----

----

-----+

----

----

----

-----+

| character_set_connection | utf8 |

| collation_connection | utf8_general_ci |

| connect_timeout |10|

| disconnect_on_expired_password | on |

| extra_max_connections |1|

| init_connect |

|| max_connect_errors |

999999999

|| max_connections |

10240

|| max_user_connections |0|

| performance_schema_session_connect_attrs_size |-1

|+----

----

----

----

----

----

----

----

----

----

-------

+------

----

-------

+10 rows in set

mysql> show full processlist;+--

--+------+

----

----

----

-----+

------+

----

-----+

-------

+-------+

----

----

----

----

-------

+| id | user | host | db | command | time | state | info |+--

--+------+

----

----

----

-----+

------+

----

-----+

-------

+-------+

----

----

----

----

-------

+|2| root | localhost | test | query |

0| init | show full processlist ||8

| root | localhost:

59270

| test | sleep |

38068

|| null ||9

| root | localhost:

59272

| test | sleep |

37914

|| null ||10

| root | localhost:

59288

| test | sleep |

37914

|| null ||11

| root | localhost:

59289

| test | sleep |

708|

| null ||12

| root | localhost:

59290

| test | sleep |

650|

| null |+--

--+------+

----

----

----

-----+

------+

----

-----+

-------

+-------+

----

----

----

----

-------

+6 rows in set (

0.00 sec)

mysql>

而所有這些含義都可以在mysql官方文件中找到,例如如下

SQLALchemy 資料庫遷移

在開發過程中,需要修改資料庫模型,而且還要在修改之後更新資料庫。最直接的方式就是刪除舊表,但這樣會丟失資料。更好的解決辦法是使用資料庫遷移框架,它可以追蹤資料庫模式的變化,然後把變動應用到資料庫中。在flask中可以使用flask migrate擴充套件,來實現資料遷移。並且整合到flask scr...

SQLalchemy連線資料庫

1 連線命令from sqlalchemy import create engine from sqlite3 import dbapi2 as sqlite engine create engine mysql mysqldb username password hostname 3306 dat...

Flask 資料庫SQLAlchemy 基礎講解

資料庫分為關係型資料庫和泛指型資料庫,典型的關係型資料庫sql有oracle sqlserver mysql等,泛指型資料庫nosql又分為用於儲存實時訊息的文件型資料庫mongodb coachdb和用於儲存快取的鍵值對型資料庫redis riak.安裝sqlalchemy外掛程式 pip ins...