Bootstrap

mysql message from server: “Too many connections“

背景

新部署完的数据库,一些基本参数未设置时,应用启动后,日志报错如下

2025-01-09 09:54:40.162 [TID: N/A] [main] ERROR c.a.m.studio.runner.ExtraDDLRunner -加载应用动态数据源失败
com.zaxxer.hikari.pool.HikariPool$PoolInitializationException: Failed to initialize pool: Data source rejected establishment of connection,  message from server: "Too many connections"
        at com.zaxxer.hikari.pool.HikariPool.throwPoolInitializationException(HikariPool.java:596)
        at com.zaxxer.hikari.pool.HikariPool.checkFailFast(HikariPool.java:582)

解决方案

 查看数据源现有值

mysql> show variables like 'max_connections';
+-----------------+-------+
| Variable_name   | Value |
+-----------------+-------+
| max_connections | 151   |
+-----------------+-------+
1 row in set (0.00 sec)

设置新值

# set global max_connections = 3000;
Query OK, 0 rows affected (0.00 sec)
mysql> 
mysql> 
mysql> 
mysql> show variables like 'max_connections';
+-----------------+-------+
| Variable_name   | Value |
+-----------------+-------+
| max_connections | 3000  |
+-----------------+-------+
1 row in set (0.00 sec)

理想状态下,主从同步的数据库,持久化时修改my.cnf文件

[mysqld]

max_connections = 3000

然后重新启动数据库。 

总结

Too many connections 错误的根本原因是 MySQL 达到最大连接数限制。解决此问题需要调整 max_connections 设置、确保连接池配置合理、避免连接泄漏,并优化应用程序的数据库使用方式。

;