1. 程式人生 > >django的mysql設置和mysql服務器閑置時間設置

django的mysql設置和mysql服務器閑置時間設置

prop nbsp val log backends color 避免 don like

服務器啟動後,每個進程都會主動連接到mysql,要是長時間沒有數據交互,mysql會自動斷開連接。

show variables like ‘%timeout%‘;


閑置連接的超時時間由wait_timeout控制,默認8小時。

技術分享圖片

django的database設置:通過設置CONN_MAX_AGE<8小時,讓客戶端主動斷開閑置的連接,避免客戶端因閑置超時發生連接錯誤

DATABASES = {
    default: {
        # ENGINE: django.db.backends.sqlite3,
        # NAME: os.path.join(BASE_DIR, 
db.sqlite3), ENGINE: django.db.backends.mysql, NAME: conf.get(mysql, database), USER: conf.get(mysql, username), PASSWORD: conf.get(mysql, password), # 密碼, HOST: conf.get(mysql, host), PORT: conf.get(mysql, port
), # mysql服務器的設置是:閑置時間超過8個小時則服務端主動斷開連接,因此這裏設置客戶端最多顯示6個小時,就主動斷開連接, # 下次連接時,重新建立新的連接,參考:https://docs.djangoproject.com/en/2.2/ref/databases/ Persistent connections CONN_MAX_AGE: 60*60*6, # 數據庫每個連接斷開的時間設置 OPTIONS: { # "init_command": "SET sql_mode=‘STRICT_TRANS_TABLES‘
", # init_command: "SET innodb_strict_mode=1", # https://django-mysql.readthedocs.io/en/latest/checks.html init_command: "SET sql_mode=‘STRICT_TRANS_TABLES‘, innodb_strict_mode=1", charset: utf8mb4, }, # Tell Django to build the test database with the utf8mb4 character set TEST: { CHARSET: utf8mb4, COLLATION: utf8mb4_general_ci, }, # 使每一個http請求都是事務性的 # ATOMIC_REQUESTS: True } }

原文細讀:

Persistent connections¶

Persistent connections avoid the overhead of re-establishing a connection to the database in each request. They’re controlled by the CONN_MAX_AGE parameter which defines the maximum lifetime of a connection. It can be set independently for each database.

The default value is 0, preserving the historical behavior of closing the database connection at the end of each request. To enable persistent connections, set CONN_MAX_AGE to a positive number of seconds. For unlimited persistent connections, set it to None.

Connection management¶ Django opens a connection to the database when it first makes a database query. It keeps
this connection open and reuses it in subsequent requests. Django closes the connection once it exceeds the maximum age defined by CONN_MAX_AGE or when it isn’t usable any longer. In detail, Django automatically opens a connection to the database whenever it needs one and doesn’t have one already — either because this is the first connection, or because the previous connection was closed. At the beginning of each request, Django closes the connection if it has reached its maximum age. If your database terminates idle connections after some time, you should set CONN_MAX_AGE to a lower value, so that Django doesn’t attempt to use a connection that has been terminated by the database server. (This problem may only affect very low traffic sites.) At the end of each request, Django closes the connection if it has reached its maximum age or if it is in an unrecoverable error state. If any database errors have occurred while processing the requests, Django checks whether the connection still works, and closes it if it doesn’t. Thus, database errors affect at most one request; if the connection becomes unusable, the next request gets a fresh connection.

Caveats¶ Since each thread maintains its own connection, your database must support at least
as many simultaneous connections as you have worker threads. Sometimes a database won’t be accessed by the majority of your views, for example because it’s the database of an external system, or thanks to caching. In such cases, you should set CONN_MAX_AGE to a low value or even 0, because it doesn’t make sense to maintain a connection that’s unlikely to be reused. This will help keep the number of simultaneous connections to this database small. The development server creates a new thread for each request it handles, negating the effect of persistent connections. Don’t enable them during development. When Django establishes a connection to the database, it sets up appropriate parameters, depending on the backend being used. If you enable persistent connections, this setup is no longer repeated every request. If you modify parameters such as the connection’s isolation level or time zone, you should either restore Django’s defaults at the end of each request, force an appropriate value at the beginning of each request, or disable persistent connections.

參考:

https://www.cnblogs.com/li1234yun/p/7729800.html

https://docs.djangoproject.com/en/2.2/ref/databases/

django的mysql設置和mysql服務器閑置時間設置