如题,本章主要讲下当服务器出现 ERROR 1040: Too many connections错误时的一些处理心得。

max_connections查看

## 查看最大连接数
SHOW VARIABLES LIKE "max_connections";
+-----------------+-------+
| Variable_name | Value |
+-----------------+-------+
| max_connections | 512 |
+-----------------+-------+ ## 查看已使用最大连接数
SHOW VARIABLES LIKE 'Max_used_connections';
+----------------------+-------+
| Variable_name | Value |
+----------------------+-------+
| Max_used_connections | 499 |
+----------------------+-------+

  

处理方案

这个问题一般有两种处理方案,解决方案非常容易,我们只需要增加max_connections连接数即可。

增加当前会话的mysql最大连接数

SET GLOBAL max_connections = 1000;

上面mysql连接值临时增加到1000,但仅适用于当前会话。一旦我们重新启动mysql服务或重新启动系统,该值将重置为默认值。

永久增加mysql最大连接数

为了永久增加mysql连接数,我们需要编辑mysql配置文件,即/etc/my.cnf。

sudo vim /etc/my.cnf

## 修改
max_connections = 1000

保存文件重启MySQL即可生效。

扩多少合适?

Max_connextions并不是越大越好的,那么如何配置?

方式一

对于提高MySQL的并发,很大程度取决于内存,官方提供了一个关于innodb的内存计算方式:

innodb_buffer_pool_size
+ key_buffer_size
+ max_connections * (sort_buffer_size + read_buffer_size + binlog_cache_size)
+ max_connections * 2MB

方式二

安装比例扩容:

max_used_connections / max_connections * 100% = [85, 90]%

最大使用连接数/最大连接数达到了80%~90%区间,就建议进行优化或者扩容了。

扩展

以下也涉及几种常见的影响MySQL性能的情况:

线程

SHOW STATUS LIKE  'Threads%';
+-------------------+--------+
| Variable_name | Value |
+-------------------+--------+
| Threads_cached | 1 |
| Threads_connected | 217 |
| Threads_created | 29 |
| Threads_running | 88 |
+-------------------+--------+ SHOW VARIABLES LIKE 'thread_cache_size';
+-------------------+-------+
| Variable_name | Value |
+-------------------+-------+
| thread_cache_size | 10 |
+-------------------+-------+
  • Threads_cached 线程在缓存中的数量
  • Threads_connected 当前打开的连接数
  • Threads_created 创建用于处理连接的线程数。
  • Threads_running 未休眠的线程数

如果Threads_created大,则可能要增加thread_cache_size值。缓存未命中率可以计算为Threads_created / Connections

查看表锁情况

SHOW GLOBAL STATUS LIKE 'table_locks%';
+-----------------------+-------+
| Variable_name | Value |
+-----------------------+-------+
| Table_locks_immediate | 90 |
| Table_locks_waited | 0 |
+-----------------------+-------+
  • Table_locks_immediate 立即获得表锁请求的次数
  • Table_locks_waited 无法立即获得对表锁的请求的次数,需要等待。这个值过高说明性能可能出现了问题,并影响连接的释放

慢查询

show variables like '%slow%';

+---------------------------+----------------------------------------------+
| Variable_name | Value |
+---------------------------+----------------------------------------------+
| slow_launch_time | 2 |
| slow_query_log | On |
+---------------------------+----------------------------------------------+

线程详情

## 查看每个线程的详细信息
SHOW PROCESSLIST;
+--------+----------+------------------+--------------+---------+-------+-------------+------------------+
| Id | User | Host | db | Command | Time | State | Info |
+--------+----------+------------------+--------------+---------+-------+-------------+------------------+
| 3 | xxxadmin | localhost | NULL | Sleep | 1 | cleaning up | NULL |
| 4 | xxxadmin | localhost | NULL | Sleep | 0 | cleaning up | NULL |
| 5 | xxxadmin | localhost | NULL | Sleep | 6 | cleaning up | NULL |
+--------+----------+------------------+--------------+---------+-------+-------------+------------------+

总结

当然,以上只是一个大概的解决思路,无论使用哪一种方式,都需要结合实际业务场景去扩容。

另外,对于生产环境,设置恰当的告警阈值,也是很有必要的。

最后,在编程时,由于用MySQL语句调用数据库执行SQL,会分配一个线程操作MySQL,所以在结束调用后,需要回收连接,避免泄漏。

参考文章

https://dev.mysql.com/doc/refman/5.7/en/server-system-variables.html#sysvar_max_connections

https://dev.mysql.com/doc/refman/5.7/en/server-status-variables.html

https://dev.mysql.com/doc/refman/8.0/en/memory-use.html

MySQL Calculator

MySQL ERROR 1040: Too many connections的更多相关文章

  1. Go连接MySql数据库Error 1040: Too many connections错误解决

    原文:https://my.oschina.net/waknow/blog/205654 摘要: 使用Go链接数据库时,由于连接释放不当会在一段时间以后产生too many connections的错 ...

  2. mysql 1040 连接数太多 mysql Error 1040 too many connection解决办法

    近在用SpringMVC开发的时候,突然出现1040 too many connection的错误,看错误的意思是连接的人数太多了.百度经验:jingyan.baidu.com 方法/步骤   1 当 ...

  3. max_connections 与 max_used_connections --ERROR 1040: Too many connections

    mysql> show variables like 'max_connections'; +-----------------+-------+ | Variable_name | Value ...

  4. Mysql连接数太多ERROR 1040 (HY000): Too many connections

    数据库连接报错:ERROR 1040 (HY000): Too many connections   1.查看连接数 /usr/local/mysql/bin/mysqladmin -h host - ...

  5. 【已解决】mysql连接出错:ERROR 1040 (HY000): Too many connections

    连接mysql,结果出错: ? 1 ERROR 1040 (HY000): Too many connections 去修改mysql的配置文件,然后添加: ? 1 2 3 4 5 6 7 8 9 1 ...

  6. mysql 链接数满了的错误 ERROR 1040 (HY000): Too many connections

    mysql 链接数满了的错误 ERROR 1040 (HY000): Too many connections 第一种处理方式: ./mysql -u root -p 登录成功后执行以下语句查询当前的 ...

  7. ERROR 1040 (08004): Too many connections

    ERROR 1040 (08004): Too many connections 长期一来,mysql低版本中,当面对连接数陡增而出现too many connections时,往往比较棘手.今天特意 ...

  8. ERROR 1040 (HY000) Too many connections

    C:\Users\Jilil>mysql -u root -pEnter password: *************ERROR 1040 (HY000): Too many connecti ...

  9. [翻译]:MySQL Error: Too many connections

    翻译:MySQL Error: Too many connections   前言: 本文是对Muhammad Irfan的这篇博客MySQL Error: Too many connections的 ...

随机推荐

  1. maven 搭建私服

    博客参考 https://www.cnblogs.com/luotaoyeah/p/3791966.html 整理纯为技术学习 1 . 私服简介 私服是架设在局域网的一种特殊的远程仓库,目的是代理远程 ...

  2. 跟我一起学.NetCore之EF Core 实战入门,一看就会

    前言 还记得当初学习数据库操作时,用ADO.NET一步一步地进行数据操作及查询,对于查询到的数据还得对其进行解析,然后封装返回给应用层:遇到这种重复而繁琐的工作,总有一些大神或团队对其进行封装,从而出 ...

  3. 【Kata Daily 191010】Grasshopper - Summation(加总)

    题目: Summation Write a program that finds the summation of every number from 1 to num. The number wil ...

  4. Loadrunner11简单压测接口教程

    一.需求 使用Loadrunner压测目标接口,要求支持1000并发数. 目标接口:https://www.xxx.com/digitaldata/api/signer/1.0/signerRegis ...

  5. 什么是4G模块 4G模块的工作原理及特点

    什么是4G模块 4G模块,也被叫做4G通信模块或4G DTU模块,他是物联网行业具有4G通信功能的一种产品,通过4G模块,我们可以实现工业设备数据通过无线4G网络传输到远端控制中心,并从控制中心通过4 ...

  6. shell脚本实现---Zabbix5.0快速部署

    shell脚本实现---Zabbix5.0快速部署 zabbix-server快速安装脚本 #!/bin/bash #Zabbix-Server 5.0#author:sunli#mail:sunli ...

  7. Selective Acknowledgment 选项 浅析 2

    来自:http://abcdxyzk.github.io/blog/2013/09/06/kernel-net-sack/ static int tcp_sacktag_write_queue(str ...

  8. Python_selenium案例:

    selenium案例 #coding=utf-8 #select下拉框处理 from selenium import webdriver from selenium.webdriver.common. ...

  9. Linux——CentOS 7 systemctl和防火墙firewalld命令

    一.防火墙的开启.关闭.禁用命令 (1)设置开机启用防火墙:systemctl enable firewalld.service (2)设置开机禁用防火墙:systemctl disable fire ...

  10. a标签中的target

    html中target四种选择_blank._parent._self._top,分别是什么意思? eg:<Cell title="Open link in new window&qu ...