当前位置:网站首页>MySQL ERROR 1040: Too many connections
MySQL ERROR 1040: Too many connections
2022-07-06 09:05:00 【向涛歌学习】
如题,本章主要讲下当服务器出现 ERROR 1040: Too many connections错误时的一些处理心得。
max_connections查看
## 查看最大连接数
SHOW VARIABLES LIKE "max_connections";
+-----------------+-------+
| Variable_name | Value |
+-----------------+-------+
| max_connections | 512 |
+-----------------+-------+
## 查看已使用最大连接数
SHOW VARIABLES LIKE 'Max_used_connections';
show global status like 'Max_used_connections';
+----------------------+-------+
| Variable_name | Value |
+----------------------+-------+
| Max_used_connections | 499 |
+----------------------+-------+
处理方案
这个问题一般有两种处理方案,解决方案非常容易,我们只需要增加max_connections连接数即可。
增加当前会话的mysql最大连接数
## 修改
max_connections = 1000
上面mysql连接值临时增加到1000,但仅适用于当前会话。一旦我们重新启动mysql服务或重新启动系统,该值将重置为默认值。
永久增加mysql最大连接数
为了永久增加mysql连接数,我们需要编辑mysql配置文件,即/etc/my.cnf。
sudo vim /etc/my.cnf
扩多少合适?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,所以在结束调用后,需要回收连接,避免泄漏。
参考
边栏推荐
- CANoe的数据回放(Replay Block),还是要结合CAPL脚本才能说的明白
- Embedded development is much more difficult than MCU? Talk about SCM and embedded development and design experience
- 51单片机进修的一些感悟
- 五月集训总结——来自阿光
- Control the operation of the test module through the panel in canoe (primary)
- Libuv thread
- May brush question 01 - array
- Hard core! One configuration center for 8 classes!
- 【深度学习】语义分割:论文阅读:(CVPR 2022) MPViT(CNN+Transformer):用于密集预测的多路径视觉Transformer
- 四川云教和双师模式
猜你喜欢

Automation sequences of canoe simulation functions

MapReduce instance (VII): single table join
![[NLP] bert4vec: a sentence vector generation tool based on pre training](/img/fd/8e5e1577b4a6ccc06e29350a1113ed.jpg)
[NLP] bert4vec: a sentence vector generation tool based on pre training

How can I take a shortcut to learn C language in college
![[deep learning] semantic segmentation: paper reading: (CVPR 2022) mpvit (cnn+transformer): multipath visual transformer for dense prediction](/img/f1/6f22f00843072fa4ad83dc0ef2fad8.png)
[deep learning] semantic segmentation: paper reading: (CVPR 2022) mpvit (cnn+transformer): multipath visual transformer for dense prediction

零基础学习单片机切记这四点要求,少走弯路

CAPL script printing functions write, writeex, writelineex, writetolog, writetologex, writedbglevel do you really know which one to use under what circumstances?

Hero League rotation chart manual rotation

C杂讲 双向循环链表

Control the operation of the test module through the panel in canoe (primary)
随机推荐
硬件工程师的真实前途我说出来可能你们不信
C#/. Net phase VI 01C Foundation_ 01: running environment, process of creating new C program, strict case sensitivity, meaning of class library
C杂讲 浅拷贝 与 深拷贝
Automation sequences of canoe simulation functions
Tianmu MVC audit II
Download address of canoe, download and activation of can demo 16, and appendix of all canoe software versions
The real future of hardware engineers may not be believed by you if I say so
单片机实现模块化编程:思维+实例+系统教程(实用程度令人发指)
How does the single chip microcomputer execute the main function from power on reset?
Summary of May training - from a Guang
Control the operation of the test module through the panel in canoe (primary)
Configure system environment variables through bat script
五月刷题01——数组
Segmentation sémantique de l'apprentissage profond - résumé du code source
Redis distributed lock implementation redison 15 questions
在CANoe中通过Panel面板控制Test Module 运行(初级)
Some thoughts on the study of 51 single chip microcomputer
Solve the problem of too many small files
C杂讲 文件 续讲
oracle sys_ Context() function