服务器启动后,每个进程都会主动连接到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': **, # 数据库每个连接断开的时间设置
'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 , 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 , 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服务器闲置时间设置的更多相关文章

  1. django+nginx+xshell简易日志查询,接上<关于《rsyslog+mysql+loganalyzer搭建日志服务器<个人笔记>》的反思>

    纠正一下之前在<关于<rsyslog+mysql+loganalyzer搭建日志服务器<个人笔记>>的反思>中说到的PHP+MySQL太慢,这里只是说我技术不好,没 ...

  2. Ubuntu+Django+Nginx+uWSGI+Mysql搭建Python Web服务器

    Ubuntu+Django+Nginx+uWSGI+Mysql搭建Python Web服务器 闲着无聊的时候部署了一个Django项目玩,用vm虚拟机部署的. 准备工作 我使用的系统是Ubuntu16 ...

  3. mysql笔记05 优化服务器设置

    优化服务器设置 1. MySQL有大量可以修改的参数--但不应该随便去修改.通常只需要把基本的项配置正确(大部分情况下只有很少一些参数时真正重要的),应将更多时间花在schema的优化.索引,以及查询 ...

  4. 解决乱码的方法是,在执行SQL语句之前,将MySQL以下三个系统参数设置为与服务器字符集character-set-server相同的字符集

    character-set-server/default-character-set:服务器字符集,默认情况下所采用的. character-set-database:数据库字符集. characte ...

  5. 第六章 优化服务器设置--高性能MySQL 施瓦茨--读书笔记

    MySql的默认配置不适用于使用大量资源,因为其通用性很高. 不要期望改变配置文件会带来巨大的性能提升.提升大小取决于工作负载,通常可以通过选择适当的配置参数得到两到三倍的性能提升.在这时候,性能提升 ...

  6. 如何通过命令行创建和设置一个MySQL用户

    我想要在MySQL服务器上创建一个新的用户帐号,并且赋予他适当的权限和资源限制.如何通过命令行的方式来创建并且设置一个MySQL用户呢? 要访问一个MySQL服务器,你需要使用一个用户帐号登录其中方可 ...

  7. 关于《rsyslog+mysql+loganalyzer搭建日志服务器<个人笔记>》的反思

    关于<rsyslog+mysql+loganalyzer搭建日志服务器<个人笔记>>的反思--链接--http://www.cnblogs.com/drgcaosheng/p/ ...

  8. 关于MYSQL数据库安装方式及相关设置简要说明

    网上关于MYSQL的教程非常多,但都不是最新的,我这里只是针对最新版本的MY SQL 的安装与设置进行一个简要的说明,大部份操作都相同. 以下是按照WINDOWS 64位操作系统+MY SQL 5.6 ...

  9. Mysql从客户端连接服务器连不上的问题

    Mysql从客户端连接服务器连不上的问题   公司要用Mysql做一个测试,开始在自己的本地建一个Mysql数据库自己本地的程序再连上去,没有遇到过连接不上的问题.这次数据库在服务器上,从本地客户端连 ...

随机推荐

  1. P3512 [POI2010]PIL-Pilots

    P3512 [POI2010]PIL-Pilots我一开始打的O(n^2)(最坏情况)的算法.枚举区间长度.60分 #include<iostream> #include<cstdi ...

  2. django URLconf调度程序

    路由的编写方式是Django2.0和1.11最大的区别所在,Django官方迫于压力和同行的影响,不得不将原来的正则匹配表达式,改为更加简单的path表达式,但依然通过re_path()方法保持对1. ...

  3. React Native之原生模块的开发(Android)学习笔记

      目录 1.为什么我们需要原生模块开发 2.开发Android原生模块的主要流程 3.原生模块开发实战   1.为什么我们需要原生模块开发? 我们在用RN开发App的时候,有时候需要用到一些原生模块 ...

  4. 【学习笔记】python2和python3的input()

    python2中的input()只接受变量作为传入值,非变量内容会报错. >>> user=input("Enter your name:") Enter you ...

  5. c# RSA 加密解密 java.net公钥私钥转换 要解密的模块大于128字节

    有一个和接口对接的任务,对方使用的是java,我方使用的是c#,接口加密类型为RSA,公钥加密私钥解密. 然后就是解决各种问题. 1.转换对方的密钥字符串 由于c#里面需要使用的是xml各式的密钥字符 ...

  6. logstash grok 分割匹配日志

    使用logstash的时候,为了更细致的切割日志,会写一些正则表达式. 使用方法 input { file { type => "billin" path => &qu ...

  7. FireDAC 下的 Sqlite [5] - 数据的插入、更新、删除

    先在空白窗体上添加: TFDConnection.TFDPhysSQLiteDriverLink.TFDGUIxWaitCursor.TFDQuery.TDataSource.TDBGrid(并在设计 ...

  8. spring集成kafka

    一.添加依赖项 compile 'org.springframework.kafka:spring-kafka:1.2.2.RELEASE' 二.发消息(生产者) 2.1 xml配置 <?xml ...

  9. ARM 汇编与C调用的若干问题(一般函数调用情况)

    ARM 汇编与C之间的函数调用需要符合ATPCS,建议函数的形参不超过4个,如果形参个数少于或等于4,则形参由R0,R1,R2,R3四个寄存器进行传递:若形参个数大于4,大于4的部分必须通过堆栈进行传 ...

  10. OpenOCD 0.9.0 release

    OpenOCD 0.9.0 release May 18th, 2015 I’m happy to announce the release of OpenOCD version 0.9.0, fin ...