内容简介:MySQL连接管理与资源限制是数据库性能和稳定性的重要保障。本文风哥教程参考MySQL官方文档MySQL Server Administration部分,详细介绍MySQL连接的管理、配置和资源限制设置,帮助DBA高效管理MySQL连接资源。学习交流加群风哥微信: itpux-com
Part01-基础概念与理论知识
1.1 MySQL连接概述
MySQL连接是客户端与服务器之间的通信通道,主要特点包括:
- 每个连接对应一个服务器线程
- 连接会占用服务器内存资源
- 连接数量有限制,受max_connections参数控制
- 连接可以是持久的或临时的
1.2 连接生命周期
MySQL连接的生命周期包括以下阶段:
- 建立连接:客户端发起连接请求,服务器验证身份
- 执行查询:客户端发送SQL语句,服务器执行并返回结果
- 空闲等待:连接等待下一个查询请求
- 断开连接:客户端或服务器主动断开连接
Part02-生产环境规划与建议
2.1 连接数规划
在生产环境中,连接数的规划需要考虑以下因素:
- 服务器硬件:CPU核心数、内存大小等
- 应用程序需求:并发用户数、连接池配置等
- 查询复杂度:复杂查询会消耗更多资源
- 响应时间要求:低延迟要求需要更多资源
最大连接数 = (服务器内存 – 系统内存 – MySQL系统内存) / 每个连接所需内存
每个连接所需内存约为2MB-10MB,取决于配置和查询复杂度
2.2 资源限制规划
资源限制的规划需要根据业务需求和服务器能力来确定:
- 连接超时:避免空闲连接占用资源
- 查询超时:防止长时间运行的查询占用资源
- 内存限制:控制每个连接或查询的内存使用
- CPU限制:控制查询的CPU使用率
Part03-生产环境项目实施方案
3.1 连接参数配置
MySQL提供多个参数用于配置连接相关功能。
mysql> SHOW GLOBAL VARIABLES LIKE ‘%connection%’;
+——————————-+———————-+——————-+
| Variable_name | Value | Variable_source |
+——————————-+———————-+——————-+
| character_set_connection | utf8mb4 | compiled in |
| collation_connection | utf8mb4_0900_ai_ci | compiled in |
| max_connections | 151 | my.cnf |
| max_user_connections | 0 | compiled in |
| performance_schema_max_thread_instances | 151 | compiled in |
+——————————-+———————-+——————-+
mysql> SHOW GLOBAL VARIABLES LIKE ‘%timeout%’;
+———————————–+———-+——————-+
| Variable_name | Value | Variable_source |
+———————————–+———-+——————-+
| connect_timeout | 10 | my.cnf |
| delayed_insert_timeout | 300 | compiled in |
| interactive_timeout | 28800 | my.cnf |
| net_read_timeout | 30 | compiled in |
| net_write_timeout | 60 | compiled in |
| wait_timeout | 28800 | my.cnf |
+———————————–+———-+——————-+
# vi /etc/my.cnf
[mysqld]
# 最大连接数
max_connections = 1000
# 每个用户最大连接数
max_user_connections = 500
# 连接超时时间(秒)
connect_timeout = 10
# 交互式连接超时时间(秒)
interactive_timeout = 7200
# 非交互式连接超时时间(秒)
wait_timeout = 7200
# 网络读取超时时间(秒)
net_read_timeout = 60
# 网络写入超时时间(秒)
net_write_timeout = 120
mysql> SET GLOBAL max_connections = 1000;
Query OK, 0 rows affected (0.00 sec)
mysql> SET GLOBAL wait_timeout = 7200;
Query OK, 0 rows affected (0.00 sec)
# 验证修改
mysql> SHOW GLOBAL VARIABLES LIKE ‘max_connections’;
+—————–+——-+——————-+
| Variable_name | Value | Variable_source |
+—————–+——-+——————-+
| max_connections | 1000 | global |
+—————–+——-+——————-+
1 row in set (0.01 sec)
3.2 资源限制配置
MySQL提供资源限制功能,控制用户或连接的资源使用。
mysql> SHOW GLOBAL VARIABLES LIKE ‘%resource%’;
+————————————–+——-+——————-+
| Variable_name | Value | Variable_source |
+————————————–+——-+——————-+
| max_resource_groups | 32 | compiled in |
| resource_group_enabled | OFF | compiled in |
| resource_group_query_reservation | 50 | compiled in |
| resource_group_cpu_exact_control | OFF | compiled in |
+————————————–+——-+——————-+
mysql> SHOW GLOBAL VARIABLES LIKE ‘max_heap_table_size’;
+———————+———-+——————-+
| Variable_name | Value | Variable_source |
+———————+———-+——————-+
| max_heap_table_size | 16777216 | my.cnf |
+———————+———-+——————-+
mysql> SHOW GLOBAL VARIABLES LIKE ‘tmp_table_size’;
+—————-+———-+——————-+
| Variable_name | Value | Variable_source |
+—————-+———-+——————-+
| tmp_table_size | 16777216 | my.cnf |
+—————-+———-+——————-+
# vi /etc/my.cnf
[mysqld]
# 内存表最大大小
max_heap_table_size = 64M
# 临时表最大大小
tmp_table_size = 64M
# 每个查询允许的最大内存
sort_buffer_size = 2M
read_buffer_size = 2M
read_rnd_buffer_size = 8M
join_buffer_size = 2M
# 最大数据包大小
max_allowed_packet = 64M
# 打开文件数限制
open_files_limit = 65535
3.3 连接池配置
使用连接池管理数据库连接,提高性能和资源利用率。
– 最小连接数:CPU核心数
– 最大连接数:CPU核心数的2-4倍
– 连接超时时间:30-60秒
– 最大空闲时间:600-1800秒
# vi application.properties
spring.datasource.hikari.minimum-idle=4
spring.datasource.hikari.maximum-pool-size=16
spring.datasource.hikari.connection-timeout=30000
spring.datasource.hikari.idle-timeout=1800000
spring.datasource.hikari.max-lifetime=1800000
spring.datasource.hikari.connection-test-query=SELECT 1
Part04-生产案例与实战讲解
4.1 连接监控实战
监控MySQL连接状态,及时发现连接异常。
mysql> SHOW STATUS LIKE ‘Threads%’;
+——————-+——-+——————-+
| Variable_name | Value | Variable_source |
+——————-+——-+——————-+
| Threads_cached | 5 | global |
| Threads_connected | 23 | global |
| Threads_created | 100 | global |
| Threads_running | 2 | global |
+——————-+——-+——————-+
# 查看当前连接详情
mysql> SHOW PROCESSLIST;
+—–+———-+———————+———-+———+——+———-+——————+
| Id | User | Host | db | Command | Time | State | Info |
+—–+———-+———————+———-+———+——+———-+——————+
| 123 | root | localhost | NULL | Sleep | 10 | | NULL |
| 124 | fgedu_user | 192.168.1.100:54321 | fgedudb | Query | 0 | executing| SELECT * FROM fgedu_users |
| 125 | fgedu_user | 192.168.1.100:54322 | fgedudb | Sleep | 5 | | NULL |
| 126 | fgedu_user | 192.168.1.100:54323 | fgedudb | Sleep | 2 | | NULL |
+—–+———-+———————+———-+———+——+———-+——————+
# 查看连接数历史趋势
mysql> SELECT * FROM information_schema.processlist ORDER BY id DESC LIMIT 10;
4.2 资源限制调整
根据监控结果调整资源限制,优化性能。
问题描述:应用程序报告”Too many connections”错误
解决方法:增加最大连接数,优化连接池配置
# grep “Too many connections” /mysql/logs/error.log
2026-04-02T13:00:00.123456+08:00 1234 [Warning] Too many connections
2026-04-02T13:01:00.123456+08:00 1235 [Warning] Too many connections
2026-04-02T13:02:00.123456+08:00 1236 [Warning] Too many connections
# 查看当前连接数
mysql> SHOW GLOBAL STATUS LIKE ‘Threads_connected’;
+——————-+——-+——————-+
| Variable_name | Value | Variable_source |
+——————-+——-+——————-+
| Threads_connected | 151 | global |
+——————-+——-+——————-+
# 查看最大连接数
mysql> SHOW GLOBAL VARIABLES LIKE ‘max_connections’;
+—————–+——-+——————-+
| Variable_name | Value | Variable_source |
+—————–+——-+——————-+
| max_connections | 151 | my.cnf |
+—————–+——-+——————-+
# 增加最大连接数
mysql> SET GLOBAL max_connections = 1000;
Query OK, 0 rows affected (0.00 sec)
# 配置文件中永久修改
# vi /etc/my.cnf
max_connections = 1000
4.3 连接问题排查
排查常见的连接问题,如连接超时、连接拒绝等。
问题描述:客户端连接服务器超时
解决方法:检查网络、防火墙、连接参数等
# ping 192.168.1.1
PING 192.168.1.1 (192.168.1.1) 56(84) bytes of data.
64 bytes from 192.168.1.1: icmp_seq=1 ttl=64 time=0.123 ms
64 bytes from 192.168.1.1: icmp_seq=2 ttl=64 time=0.124 ms
# 检查端口是否开放
# telnet 192.168.1.1 3306
Trying 192.168.1.1…
Connected to 192.168.1.1.
Escape character is ‘^]’.
J
5.7.32-log��x6M!��!�’O7,>4q_@z5mysql_native_password
# 检查防火墙配置
# firewall-cmd –list-ports
3306/tcp
# 检查MySQL监听地址
mysql> SHOW GLOBAL VARIABLES LIKE ‘bind_address’;
+—————+———–+——————-+
| Variable_name | Value | Variable_source |
+—————+———–+——————-+
| bind_address | 0.0.0.0 | my.cnf |
+—————+———–+——————-+
# 检查连接超时参数
mysql> SHOW GLOBAL VARIABLES LIKE ‘connect_timeout’;
+—————–+——-+——————-+
| Variable_name | Value | Variable_source |
+—————–+——-+——————-+
| connect_timeout | 10 | my.cnf |
+—————–+——-+——————-+
Part05-风哥经验总结与分享
5.1 连接管理最佳实践
- 使用连接池:应用程序使用连接池管理数据库连接,减少连接建立和断开的开销
- 合理设置连接数:根据服务器资源和业务需求设置合适的max_connections
- 监控连接状态:定期监控连接数、连接超时等指标
- 优化连接超时:设置合理的wait_timeout和interactive_timeout
- 限制用户连接:使用max_user_connections限制单个用户的连接数
- 关闭空闲连接:应用程序及时关闭不需要的连接
5.2 资源限制最佳实践
- 内存限制:根据服务器内存大小设置合理的内存参数
- 临时表限制:设置tmp_table_size和max_heap_table_size避免临时表过大
- 查询限制:使用max_execution_time限制查询执行时间
- 网络限制:设置max_allowed_packet避免数据包过大
- 文件限制:设置open_files_limit确保服务器可以打开足够的文件
- 定期优化:根据监控结果定期调整资源限制参数
from MySQL:www.itpux.com
本文由风哥教程整理发布,仅用于学习测试使用,转载注明出处:http://www.fgedu.net.cn/10327.html
