原因
Mysql出现Too many connections的错误,这是因为mysql当前连接数超出max_connections导致的(可通过show variables like 'max_connections’查看)。
可以通过show processlist(详解)查看当前mysql的连接数及信息。
解决办法
1、首先你应该检查一下你的代码,是否有出现在操作完数据库后,没有关闭连接的情况。
2、适当的调整max_connections(默认是151),设置方式如下:
通过命令修改的方式,在重启mysql服务后会恢复默认值,临时设置。但好在不需要重启服务。
// 根据自己的需求设置
mysql> set global max_connections = 300;
Query OK, 0 rows affected
mysql> show variables like 'max_connections';
+-----------------+-------+
| Variable_name | Value |
+-----------------+-------+
| max_connections | 300 |
+-----------------+-------+
1 row in set
通过修改配置文件的方式,需要重启mysql服务后生效,永久设置。
Window下:
找到mysql的安装目录下的my.ini配置文件,如果没有就自己创建一个。
[mysqld]
datadir=C:/devlope/Mariadb
port=3369
innodb_buffer_pool_size=3053M
character-set-server=utf8
# 添加一行如下
max_connections=300
[client]
port=3369
plugin-dir=D:/programmeSoftware/MariaDB10.3/lib/plugin
如果你不记得自己的安装目录或找不到配置文件,可以通过如下方式查看。
1、打开任务管理器
2、点击窗口下方打开服务
3、找到mysql服务,单机右键,打开属性
4、找到可执行文件的路径,类似如下
“D:\programmeSoftware\MariaDB10.3\bin\mysqld.exe” “–defaults-file=C:\devlope\Mariadb\my.ini” “MySQL”
你可以看到你的mysql安装路径,及配置文件的路径。
Linux下:
// 查找mysql配置文件所在位置
[root@xx]# whereis my.cnf
my: /etc/my.cnf /etc/my.ini
// 编辑配置文件,进入后按下Insert键
[root@xx]# vim /etc/my.cnf
#
# This group is read both both by the client and the server
# use it for options that affect everything
#
[client-server]
[mysqld]
wait_timeout=60
# 在这里加一行配置如下
max_connections=300
#
# include all files from the config directory
#
!includedir /etc/my.cnf.d
// 编辑完后,按下Esc键,输入:wq!写入保存并退出
:wq!
然后重启服务,登入mysql,使用show variables like ‘max_connections’;查看即可。