原创请注明出处

1、max_user_connections
max_user_connections这个参数是单个用户允许连接的最大会话数量,在建立用户的时候也有类似的限制,这里仅仅说的是这个参数

下面是官方文档说明:
The maximum number of simultaneous connections permitted to any given MySQL user account. A
value of 0 (the default) means “no limit.”
This variable has a global value that can be set at server startup or runtime. It also has a read-only
session value that indicates the effective simultaneous-connection limit that applies to the account
associated with the current session. The session value is initialized as follows:
? If the user account has a nonzero MAX_USER_CONNECTIONS resource limit, the session
max_user_connections value is set to that limit.
? Otherwise, the session max_user_connections value is set to the global value.

报错如下:
mysql: [Warning] Using a password on the command line interface can be insecure.
ERROR 1203 (42000): User root already has more than 'max_user_connections' active connections

2、max_connections
max_connections:这个参数是MYSQL服务端允许的最大连接会话数量,没什么好说的
下面是官方文档说明:
The maximum permitted number of simultaneous client connections. By default, this is 151. See
Section B.5.2.7, “Too many connections”, for more information.
Increasing this value increases the number of file descriptors that mysqld requires. If the required
number of descriptors are not available, the server reduces the value of max_connections. See
Section 9.4.3.1, “How MySQL Opens and Closes Tables”, for comments on file descriptor limits.

测试也非常简单报错如下:
[root@testmy ~]#  /mysqldata/mysql5.7/bin/mysql --socket=/mysqldata/mysql5.7/mysqld3307.sock -utestmy -pGelc123123
mysql: [Warning] Using a password on the command line interface can be insecure.
ERROR 1040 (HY000): Too many connections

3、Max_used_connections 、Max_used_connections_time 
这里还大概说一下

mysql> show global status like '%max%';
+-----------------------------------+---------------------+
| Variable_name                     | Value               |
+-----------------------------------+---------------------+
| Max_used_connections              | 7                   |
| Max_used_connections_time         | 2017-05-10 17:10:56 |

这两个状态说的是MYSQL SERVER自上次启动起来最大连接数量和发生的时间,和上面讲参数没任何关系。

? Max_used_connections
The maximum number of connections that have been in use simultaneously since the server started.
? Max_used_connections_time
The time at which Max_used_connections reached its current value. This variable was added in
MySQL 5.7.5

4、back_log
back_log:这个参数的解释相对于比较复杂和难以理解,难以理解是因为大多DBA对LINUX下C编程不熟悉,
这个参数说的就是sokcet编程中的listen调用的时候使用形参back_log
函数原型
int listen(int sockfd, int backlog);          
man page中描述:
DESCRIPTION
       listen() marks the socket referred to by sockfd as a passive socket, that is, as a socket that will be used to accept incoming connection requests using accept(2).
       The sockfd argument is a file descriptor that refers to a socket of type SOCK_STREAM or SOCK_SEQPACKET.
       The  backlog argument defines the maximum length to which the queue of pending connections for sockfd may grow.  If a connection request arrives when the queue is full, the client may receive an error with
       an indication of ECONNREFUSED or, if the underlying protocol supports retransmission, the request may be ignored so that a later reattempt at connection succeeds.
RETURN VALUE
       On success, zero is returned.  On error, -1 is returned, and errno is set appropriately.
sockfd没什么好说的create的时候返回的scoket文件描述符,这里的backlog实际上是一个未决连接的一个队列,如果超过可能值会返回ECONNREFUSED的一个错误,但是如果底层协议支持retransmission,
,这个错误将被忽略然后再次尝试知道成功。它取决于LINUX系统设置/proc/sys/net/core/somaxconn和函数backlog的小值当然LINUX系统默认这个值是128.
下面是MYSQL官方文档描述:
The number of outstanding connection requests MySQL can have. This comes into play when the
main MySQL thread gets very many connection requests in a very short time. It then takes some
time (although very little) for the main thread to check the connection and start a new thread. The
back_log value indicates how many requests can be stacked during this short time before MySQL
momentarily stops answering new requests. You need to increase this only if you expect a large number
of connections in a short period of time.
In other words, this value is the size of the listen queue for incoming TCP/IP connections. Your operating
system has its own limit on the size of this queue. The manual page for the Unix listen() system
call should have more details. Check your OS documentation for the maximum value for this variable.
back_log cannot be set higher than your operating system limit
实际上它说在有大量连接时候,可能出现这样的问题,这里使用了一个short time来描述,也明确告诉你这个
参数和Unix listen() system调用有关。
MYSQL listen 函数调用栈为

点击(此处)折叠或打开

  1. #0 0x0000003ca5ee9880 in listen () from /lib64/libc.so.6
  2. #1 0x00000000016e3482 in inline_mysql_socket_listen (src_file=0x21c5f30 "/root/mysql5.7.14/percona-server-5.7.14-7/sql/conn_handler/socket_connection.cc", src_line=522, mysql_socket=..., backlog=2)
  3. at /root/mysql5.7.14/percona-server-5.7.14-7/include/mysql/psi/mysql_socket.h:1084
  4. #2 0x00000000016e5b1b in TCP_socket::get_listener_socket (this=0x7fffffffdb40) at /root/mysql5.7.14/percona-server-5.7.14-7/sql/conn_handler/socket_connection.cc:522
  5. #3 0x00000000016e41d8 in Mysqld_socket_listener::setup_listener (this=0x339d550) at /root/mysql5.7.14/percona-server-5.7.14-7/sql/conn_handler/socket_connection.cc:808
  6. #4 0x0000000000ecefed in Connection_acceptor<Mysqld_socket_listener>::init_connection_acceptor (this=0x2fd4a90) at /root/mysql5.7.14/percona-server-5.7.14-7/sql/conn_handler/connection_acceptor.h:55
  7. #5 0x0000000000ec089d in network_init () at /root/mysql5.7.14/percona-server-5.7.14-7/sql/mysqld.cc:1864
  8. #6 0x0000000000ec6594 in mysqld_main (argc=52, argv=0x2e97438) at /root/mysql5.7.14/percona-server-5.7.14-7/sql/mysqld.cc:5103
  9. #7 0x0000000000ebd344 in main (argc=9, argv=0x7fffffffe3f8) at /root/mysql5.7.14/percona-server-5.7.14-7/sql/main.cc:25

我们一般编程的时候会这样写:

点击(此处)折叠或打开

  1. if((listen(listenfd,1)) == -1)//这里设置back_log为1
  2. {
  3. perror("listen");
  4. return -1;
  5. }
  6. printf("Accepting connections...\n");
  7. while(1)
  8. {
  9. //cliaddr_len = sizeof(cliaddr);
  10. //4、接受来自客户端的连接生成数据交互socket fd
  11. if((confd = accept(listenfd,(SCOK_ADD)&cliaddr,&cliaddr_len)) == -1 )
  12. {
  13. return 1;
  14. }
  15. else
  16. {
  17. printf("Client ip:%s Port:%d\n",inet_ntop(AF_INET, &cliaddr.sin_addr, str, sizeof(str)), ntohs(cliaddr.sin_port));
  18. if( (ret = pthread_create(&tid,NULL,do_work,(void*)&confd) )!=0)
  19. {
  20. printf("%s\n",strerror(ret));
  21. return 1;
  22. }
  23. if((ret = pthread_detach(tid)) != 0 )
  24. {
  25. printf("%s\n",strerror(ret));
  26. return 1;
  27. }
  28. printf("thread %lu is create and detach\n",tid);
  29. }
  30. //write(confd,"a",1);
  31. //close(confd);
  32. }
  33. close(listenfd);
  34. return 0;
  35. }

这里大概让知道listen() 和 accpet()到底如何使用的,不然光说好像不太好理解。

但是具体代表什么我们还是要参考LINUX 系统编程手册和实际编程的模型我们先来看这样一张图

我刻意讲一部分中文内容截取出来更加方便大家理解。
当服务端忙于处理其他客户端的时候listen()和accept()之间的这种非原子化的流程消耗的时间将会放大,一般来说这里是很短的一段的时间,也就是
MYSQL官方手册说的short time,在这里的客户端connect()过来,还没到服务端accept()函数调用这个时候将会进入未决连接请求队列,有了这个基础
我们来梳理一下这个流程
MYSQL connect()-->>服务端listen()-->>进入未决连接队列-->>服务端accpet()-->>建立连接传输交互
那么这个参数实际控制的是一种阻塞,如果我设置back_log为2,那么同时进入未决连接队列的连接就是2,这个时候如果这两个连接都还没有accpet()
完成那么,又来的新连接只有等待进入这个队列,就是一种阻塞。

这种问题实在不好测试,我使用程序讲back_log设置1模拟了一下大量连接,确实可能出现堵塞问题,但是没找到如何查看back_log当前未决连接个数的输出,
所以说服力也就有限,所以先放到这里,如果以后找到方法模拟一下。

下面资料转自网络
http://blog.chinaunix.net/uid-24782829-id-3456109.html
listen函数仅由TCP服务器调用,它做两件事情:
1、当socket函数创建一个套接口时,它被假设为一个主动套装口,也就是说,它是一个将调用connet发起连接的客户套接口。listen函数把一个未连接的套接口转换成一个被动套接口,指示内核应接受指向该套接口的连接请求。根据TCP状态转换图,调用listen导致套接口从CLOSED状态转换到LISTEN状态。
2、本函数的第二个参数规定了内核应该为相应套接口排队的最大连接个数。
    为了更好的理解backlog参数,我们必须认识到内核为任何一个给定的监听套接口维护两个队列:
1、未完成连接队列(incomplete connection queue),每个这样的SYN分节对应其中一项:已由某个客户发出并到达服务器,而服务器正在等待完成相应的TCP三路握手过程。这些套接口处于SYN_RCVD状态。
2、已完成连接队列(completed connection queue),每个已完成TCP三路握手过程的客户对应其中一项。这些套接口处于ESTABLISHED状态。
      当来自客户的SYN到达时,TCP在未完成连接队列中创建一个新项,然后响应以三路握手的第二个分节:服务器的SYN响应,其中稍带对客户SYN的ACK(即SYN+ACK)。这一项一直保留在未完成连接队列中,直到三路握手的第三个分节(客户对服务器SYN的ACK)到达或者该项超时为止(曾经源自Berkeley的实现为这些未完成连接的项设置的超时值为75秒)。如果三路握手正常完成,该项就从未完成连接队列移到已完成连接队列的队尾。当进程调用accept时,已完成连接队列中的队头项将返回给进程,或者如果该队列为空,那么进程将被投入睡眠,直到TCP在该队列中放入一项才唤醒它。
未完成队列(incomplete connection queue)的长度现在由/proc/sys/net/ipv4/tcp_max_syn_backlog设置,在现在大多数最新linux内核都是默认512,这个设置有效的前提是系统的syncookies功能被禁用,如果系统的syncookies功能被启用,那么这个设置是无效的。Syncookies是在内核编译的时候设置的,查看syncookies是否启动:
cat  /proc/sys/net/ipv4/tcp_syncookies
如果是“1”说明已启用,为“0”说明未启用。
那么为syncookies是做什么的呢,为什么它会和未完成队列有关系。简单的说它是为防范SYN Flood攻击的设计。具体请参考“syncookies介绍”(http://baike.baidu.com/view/9033755.htm)。
继续看backlog,如果我们给listen的backlog参数设值超过了/proc/sys/net/core/somaxconn,那么backlog参数的值为自动被改写为/proc/sys/net/core/somaxconn的值,它的默认大小为128.

作者微信:

MYSQL max_user_connections back_log max_connections参数和Max_used_connections相关推荐

  1. MySQL中的max_connections和max_user_connections 及 MySQL服务器最大连接数的合理设置

    max_connections 是指整个mysql服务器的最大连接数: max_user_connections 是指每个数据库用户的最大连接数,比如:虚拟主机可以用这个参数控制每个虚拟主机用户的数据 ...

  2. mysql全局变量之max_connections max_user_connections详解

    之前在网上看到很多关于max_conections & max_user_conenctions变量的解释,但是感觉都有些不太准确,要不就是说的很模糊,因此详读了mysql的官方文档6.3.4 ...

  3. MySQL性能优化之参数配置

    1.目的: 通过根据服务器目前状况,修改MySQL的系统参数,达到合理利用服务器现有资源,最大合理的提高MySQL性能. 2.服务器参数: 32G内存.4个CPU,每个CPU 8核. 3.MySQL目 ...

  4. MySQL性能优化之参数配置 - 愤怒的码农 - 博客园

    MySQL性能优化之参数配置 1.目的: 通过根据服务器目前状况,修改Mysql的系统参数,达到合理利用服务器现有资源,最大合理的提高MySQL性能. 2.服务器参数: 32G内存.4个CPU,每个C ...

  5. Mysql一些重要配置参数的学习与整理(二)

    2019独角兽企业重金招聘Python工程师标准>>> 原文地址:Mysql一些重要配置参数的学习与整理(二) 上一篇,Mysql一些重要配置参数的学习与整理(一)中,我们了解和学习 ...

  6. MySQL 5.7数据库参数优化

    MySQL 5.7数据库参数优化 连接相关参数 max_connections:允许客户端并发连接的最大数量,默认值是151,一般将该参数设置为500-2000 max_connect_errors: ...

  7. Mysql一些重要配置参数的学习与整理(一)

    2019独角兽企业重金招聘Python工程师标准>>> 原文地址:Mysql一些重要配置参数的学习与整理(一) 最近一直在进行mysql的学习,主要是学习mysql的一些配置参数,了 ...

  8. Mysql 配置文件 my.cnf 参数详情参考

    Mysql 配置文件 my.cnf 参数详情参考 参考地址:https://blog.csdn.net/qq_42768234/article/details/110089078 其中有些参数有待查询 ...

  9. mysql 修改wait_timeout_MySQL wait_timeout参数修改

    MySQL wait_timeout参数修改问题,可能经常会有DBA遇到过,下面就试验一下并看看会有什么现象. wait_timeout分为global级及session级别,如未进行配置,默认值为2 ...

最新文章

  1. 剑指offer_第20题_包含min函数的栈_Python
  2. cad怎么卸载干净_百度软件中心助手怎么样干净卸载
  3. jquery 属性操作
  4. 项目Alpha冲刺Day4
  5. 阿里云Https部署网站
  6. RoseHA集群:RHEL+RoseMirror+Oracle【2】
  7. 一周学好python_耗时一周整理的Python资料,包含各阶段所需网站、项目,收藏了?慢慢来...
  8. mysql5.7主从全备恢复_Mysql5.7—运维常用备份方式(超全)
  9. windows连接远程桌面
  10. 【clickhouse】ClickHouse基础、实践、调优全视角解析
  11. centos php日志分析,Centos日志查看分析
  12. javascript 以“年-月-日 时:分:秒”格式显示当前时间
  13. Python爬虫知识点四--scrapy框架
  14. springmvc06 静态资源
  15. 【JAVA笔记——道】Hadoop设计模式--抽象类
  16. Atitit 软件国际化原理与概论
  17. 自动阻止恶意攻击IP,降低青龙被暴力破解的风险
  18. 应用StarRocks实现存储引擎的收敛,保障高查询并发及低延迟要求
  19. 小米电视5和5pro区别
  20. 多个lmg在盒子里在左浮动( float: left)时候出现横向图片中间有缝隙

热门文章

  1. VC连接SQL server2005
  2. 江西理工大学计算机管理技术期末复习(wangzhendong)网络管理与维护
  3. JavaScript如何获得input元素value值
  4. windows中安装Node.js-安装angular-cli
  5. 10大常见的安全漏洞!你知道吗?
  6. 【C语言】12-指向一维数组元素的指针
  7. Python 转义符
  8. 中国版 Ubuntu Kylin 14.04 LTS 麒麟操作系统中文版发布下载 (Ubuntu天朝定制版)
  9. Exchange出站队列堵塞解决思路
  10. 计算机维护常识_系统篇