Mysql中出现Too many connections的原因及解决方案(设置max_connections重启后不生效原因)

原因

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’;查看即可。

  • 4
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
MySQL "too many connections"错误是由于达到了MySQL的最大连接数限制而导致的。您可以通过以下几个步骤来解决这个问题: 1. 首先,您可以使用命令`show variables like '%max_connections%';`来查看MySQL允许的最大连接数。 这个值通常是由MySQL配置文件max_connections参数设置的,默认情况下为150。 2. 您还可以使用命令`show status like 'Max_used_connections';`来查看MySQL在当前运行期间的最高连接数。 如果这个值接近或等于最大连接数限制,那么可能是因为您的应用程序或数据库负载导致了太多的连接请求。 3. 如果您发现MySQL的最大连接数设置较低,并且您的应用程序需要更多的连接数来处理并发请求,您可以尝试增加最大连接数的限制。可以通过修改MySQL配置文件(my.cnf)max_connections参数来实现。 但是请注意,增加最大连接数也会增加系统资源的使用量,因此需要谨慎地评估您的服务器的硬件和资源限制。 4. 另外,您还可以通过优化您的应用程序和数据库查询来减少连接数。这可以包括使用连接池技术来管理数据库连接,以及优化代码和查询以降低数据库负载。 综上所述,解决MySQL "too many connections"错误的方法包括增加最大连接数限制、优化应用程序和数据库查询,以及使用连接池等技术来管理数据库连接。请根据您的具体情况选择适合的解决方案。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* [Mysql报错:too many connections原因及解决方法](https://blog.csdn.net/B001XFX/article/details/132172241)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] - *3* [MySQL:Too many connections如何解决](https://blog.csdn.net/zhizhengguan/article/details/122083507)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值