MariaDB数据库连接失败,报错:Error: Too many connections

MariaDB数据库连接失败,报错:Error: Too many connections

问题描述

搭建的zabbix 服务器,今天用navacat连接MariaDB突然出现“Error: Too many connections”。根据字面意思,应该是连接数据库的终端太多,下面修改MariaDB的配置文件解决。

原因分析

1.MariaDB的最大连接数太小(默认100个)
2.空闲等待超时时间太长 (默认28800s)

解决方法

1.打开MariaDB的配置文件50-server.cnf

vi /etc/mysql/mariadb.conf.d/50-server.cnf 

max_connections        = 1000

找到max_connections字段,将最大连接数改为1000

2.查看当前等待超时时间

MariaDB [(none)]> show global variables like 'wait_timeout';
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| wait_timeout  | 28800 |
+---------------+-------+
1 row in set (0.01 sec)

将wait_timeout改为600s

MariaDB [(none)]> set global wait_timeout=600;
Query OK, 0 rows affected (0.00 sec)

再次查看超时时间

MariaDB [(none)]> show global variables like 'wait_timeout';
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| wait_timeout  | 600   |
+---------------+-------+
1 row in set (0.00 sec)

以上修改完成,重启服务器即可!!

另外,查看当前连接数量

MariaDB [(none)]> show processlist;
+------+--------+---------------------+--------+---------+------+-------+------------------+----------+
| Id   | User   | Host                | db     | Command | Time | State | Info             | Progress |
+------+--------+---------------------+--------+---------+------+-------+------------------+----------+
|    1 | zabbix | localhost           | zabbix | Sleep   |   55 |       | NULL             |    0.000 |
|    2 | zabbix | localhost           | zabbix | Sleep   |    4 |       | NULL             |    0.000 |
|    3 | zabbix | localhost           | zabbix | Sleep   |    4 |       | NULL             |    0.000 |
|    4 | zabbix | localhost           | zabbix | Sleep   |    1 |       | NULL             |    0.000 |
|    5 | zabbix | localhost           | zabbix | Sleep   |    2 |       | NULL             |    0.000 |
|    6 | zabbix | localhost           | zabbix | Sleep   |    0 |       | NULL             |    0.000 |
|    7 | zabbix | localhost           | zabbix | Sleep   |    0 |       | NULL             |    0.000 |
|    8 | root   | localhost           | NULL   | Query   |    0 | init  | show processlist |    0.000 |
+------+--------+---------------------+--------+---------+------+-------+------------------+----------+
8 rows in set (0.00 sec)

Command为Sleep表示数据库连接空闲状态
可以手动kill掉Id 即可(3表示进程Id)

MariaDB [(none)]> kill 3;  
Query OK, 0 rows affected (0.00 sec)

你可能感兴趣的:(数据库)