当前位置:网站首页>MySQL ERROR 1040: Too many connections
MySQL ERROR 1040: Too many connections
2022-07-06 10:02:00 【Learn from Tao Ge】
As the title , This chapter mainly talks about when the server appears ERROR 1040: Too many connections Some experience in dealing with mistakes .
max_connections see
## See the maximum number of connections
SHOW VARIABLES LIKE "max_connections";
+-----------------+-------+
| Variable_name | Value |
+-----------------+-------+
| max_connections | 512 |
+-----------------+-------+
## View the maximum number of connections used
SHOW VARIABLES LIKE 'Max_used_connections';
show global status like 'Max_used_connections';
+----------------------+-------+
| Variable_name | Value |
+----------------------+-------+
| Max_used_connections | 499 |
+----------------------+-------+
Treatment scheme
There are generally two solutions to this problem , The solution is very easy , We just need to add max_connections The number of connections is enough .
Add to the current session mysql maximum connection
## modify
max_connections = 1000
above mysql The connection value is temporarily increased to 1000, But only for the current session . Once we restart mysql Service or restart the system , The value will be reset to the default value .
Permanently increase mysql maximum connection
In order to permanently increase mysql The number of connections , We need editors mysql The configuration file , namely /etc/my.cnf.
sudo vim /etc/my.cnf
How much expansion is appropriate ?Max_connextions It's not the bigger the better , So how to configure ?
Mode one
For improving MySQL Concurrent , A lot depends on memory , The official provided a report on innodb Of memory Calculation method :
innodb_buffer_pool_size
+ key_buffer_size
+ max_connections * (sort_buffer_size + read_buffer_size + binlog_cache_size)
+ max_connections * 2MB
Mode two
Installation scale expansion :
max_used_connections / max_connections * 100% = [85, 90]%
Maximum number of connections used / maximum connection Reached 80%~90% Section , Optimize or expand the proposed capacity .
Expand
Here are also several common effects MySQL Performance conditions :
Threads
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 The number of threads in the cache
- Threads_connected The current number of open connections
- Threads_created Number of threads created to handle connections .
- Threads_running Number of threads not sleeping
If Threads_created Big , You may have to add thread_cache_size value . The cache miss ratio can be calculated as Threads_created / Connections
Check the table lock
SHOW GLOBAL STATUS LIKE 'table_locks%';
+-----------------------+-------+
| Variable_name | Value |
+-----------------------+-------+
| Table_locks_immediate | 90 |
| Table_locks_waited | 0 |
+-----------------------+-------+
- Table_locks_immediate The number of times a table lock request was immediately obtained
- Table_locks_waited The number of times a request for a table lock could not be obtained immediately , Need to wait . If this value is too high, there may be a performance problem , And affect the release of connections
The slow query
show variables like '%slow%';
+---------------------------+----------------------------------------------+
| Variable_name | Value |
+---------------------------+----------------------------------------------+
| slow_launch_time | 2 |
| slow_query_log | On |
+---------------------------+----------------------------------------------+
Thread details
## View the details of each thread
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 |
+--------+----------+------------------+--------------+---------+-------+-------------+------------------+
summary
Of course , The above is just a general solution , Either way , Both need to be expanded in combination with actual business scenarios .
in addition , For production environment , Set the appropriate alarm threshold , It's also necessary .
Last , In programming , Because of the use of MySQL Statement calls the database to execute SQL, Will assign a thread operation MySQL, So at the end of the call , Need to recycle connection , Avoid leakage .
Reference resources
边栏推荐
- Installation of pagoda and deployment of flask project
- Tianmu MVC audit II
- The replay block of canoe still needs to be combined with CAPL script to make it clear
- MySQL实战优化高手09 生产经验:如何为生产环境中的数据库部署监控系统?
- [one click] it only takes 30s to build a blog with one click - QT graphical tool
- 在CANoe中通过Panel面板控制Test Module 运行(初级)
- 14 医疗挂号系统_【阿里云OSS、用户认证与就诊人】
- Carolyn Rosé博士的社交互通演讲记录
- The 32 year old programmer left and was admitted by pinduoduo and foreign enterprises. After drying out his annual salary, he sighed: it's hard to choose
- CAPL script printing functions write, writeex, writelineex, writetolog, writetologex, writedbglevel do you really know which one to use under what circumstances?
猜你喜欢

Combined search /dfs solution - leetcode daily question - number of 1020 enclaves

手把手教您怎么编写第一个单片机程序

CANoe下载地址以及CAN Demo 16的下载与激活,并附录所有CANoe软件版本

C miscellaneous two-way circular linked list

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

Some thoughts on the study of 51 single chip microcomputer

Nc17 longest palindrome substring
![17 medical registration system_ [wechat Payment]](/img/b4/f9abfa0fb0447d727078069d888b57.png)
17 medical registration system_ [wechat Payment]

Learning SCM is of great help to society

颜值爆表,推荐两款JSON可视化工具,配合Swagger使用真香
随机推荐
Cooperative development in embedded -- function pointer
cmooc互联网+教育
在CANoe中通過Panel面板控制Test Module 運行(初級)
C miscellaneous shallow copy and deep copy
51单片机进修的一些感悟
寶塔的安裝和flask項目部署
Write your own CPU Chapter 10 - learning notes
AI的路线和资源
手把手教您怎么编写第一个单片机程序
Function description of shell command parser
美新泽西州州长签署七项提高枪支安全的法案
在CANoe中通过Panel面板控制Test Module 运行(高级)
Contest3145 - the 37th game of 2021 freshman individual training match_ C: Tour guide
History of object recognition
Configure system environment variables through bat script
CANoe的数据回放(Replay Block),还是要结合CAPL脚本才能说的明白
How does the single chip microcomputer execute the main function from power on reset?
Contest3145 - the 37th game of 2021 freshman individual training match_ B: Password
CANoe仿真功能之自动化序列(Automation Sequences )
C杂讲 文件 初讲