0
点赞
收藏
分享

微信扫一扫

Linux学习笔记021---Centos7 下 MySql too many connections 报错


​​  JAVA技术交流QQ群:170933152​​

1.出现这个问题以后,重启Centos7 都不行,启动了还是报错

2.解决方案:


错误信息如下:

Can not connect to MySQL server

Error: Too many connections

Errno.: 1040

Similar error report has beed dispatched to administrator before.

从官方文档知道​​Linux​​上面编译安装的mysql默认的连接为100个

文档:http://dev.mysql.com/doc/refman/5.0/en/too-many-connections.html

mysql官方告诉我们需要修改max_connections的值,那么我们怎么去修改呢?有两种方法

1、修改配置文件文件

修改/etc/my.cnf这个文件,在[mysqld] 中新增max_connections=N,如果你没有这个文件请从编译源码中的support-files文件夹中复制你所需要的*.cnf文件为到 /etc/my.cnf。我使用的是my-medium.cnf,中型服务器配置。例如我的[mysqld]的内容如下

[mysqld]

port = 3306

socket = /tmp/mysql.sock

skip-locking

key_buffer = 160M

max_allowed_packet = 1M

table_cache = 64

sort_buffer_size = 512K

net_buffer_length = 8K

read_buffer_size = 256K

read_rnd_buffer_size = 512K

myisam_sort_buffer_size = 8M

max_connections=1000 //注意就是加上这个,还是不行

wait_timeout=5//再加上这个,这个文件中有可能,没有这两个值,再后边添加上就行了,注意

要添加到:[mysqld]这个部分


然后重新进一下:

[root@localhost etc]# mysql -u root -p

Enter password: 

ERROR 1040 (HY000): Too many connections

上面如果配置了那个文件,就应该可以进去了

然后再根据下面,去设置一下就可以了:

最近写javaee项目的时候,mysql报了too many connections的错误,百度的内容有一些有问题,所以我重新写一下我的解决方法。



​mysql -u root -p​​ 回车输入密码进入mysql 
Linux学习笔记021---Centos7 下 MySql too many connections 报错_mysql



​show processlist;​​ 
查看连接数,可以发现有很多连接处于sleep状态,这些其实是暂时没有用的,所以可以kill掉



​show variables like "max_connections";​​ 
查看最大连接数,应该是与上面查询到的连接数相同,才会出现too many connections的情况



​set GLOBAL max_connections=1000;​​ 
修改最大连接数,但是这不是一劳永逸的方法,应该要让它自动杀死那些sleep的进程。



​show global variables like 'wait_timeout';​​ 
这个数值指的是mysql在关闭一个非交互的连接之前要等待的秒数,默认是28800s



​set global wait_timeout=300;​​ 
修改这个数值,这里可以随意,最好控制在几分钟内 
Linux学习笔记021---Centos7 下 MySql too many connections 报错_最大连接数_02



​set global interactive_timeout=500;​​ 
修改这个数值,表示mysql在关闭一个连接之前要等待的秒数,至此可以让mysql自动关闭那些没用的连接,但要注意的是,正在使用的连接到了时间也会被关闭,因此这个时间值要合适



批量kill之前没用的sleep连接,在网上搜索的方法对我都不奏效,因此只好使用最笨的办法,一个一个kill


  • ​select concat('KILL ',id,';') from information_schema.processlist where user='root';​​ 先把要kill的连接id都查询出来
  • 复制中间的​​kill id;​​内容到word文档
  • 替换掉符号“|”和回车符(在word中查询^p即可查询到回车符)
  • 把修改过的内容复制回终端,最后按回车执行!



[root@localhost ~]# vim /etc/my.cnf

[root@localhost ~]# systemctl restart mysqld.service

[root@localhost ~]# mysql -u root -p

Enter password: 

Welcome to the MySQL monitor.  Commands end with ; or \g.

Your MySQL connection id is 530

Server version: 5.6.36 MySQL Community Server (GPL)

Copyright (c) 2000, 2017, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its

affiliates. Other names may be trademarks of their respective

owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> show processlist

    -> ;

+-----+------+---------------------+------+---------+------+-------+------------------+

| Id  | User | Host                | db   | Command | Time | State | Info             |

+-----+------+---------------------+------+---------+------+-------+------------------+

| 530 | root | localhost           | NULL | Query   |    0 | init  | show processlist |

| 536 | root | 172.19.128.67:56319 | NULL | Sleep   |   30 |       | NULL             |

| 537 | root | 172.19.128.67:56320 | dbo  | Sleep   |   25 |       | NULL             |

+-----+------+---------------------+------+---------+------+-------+------------------+

3 rows in set (0.00 sec)

mysql> show variables like "max_connections"

    -> ;

+-----------------+-------+

| Variable_name   | Value |

+-----------------+-------+

| max_connections | 214   |

+-----------------+-------+

1 row in set (0.00 sec)

mysql> set GLOBAL max_connections=1000;

Query OK, 0 rows affected (0.00 sec)

mysql> show global variables like 'wait_timeout';

+---------------+-------+

| Variable_name | Value |

+---------------+-------+

| wait_timeout  | 5     |

+---------------+-------+

1 row in set (0.01 sec)

mysql> set gloabl interactive_timeout =500

    -> ;

ERROR 1193 (HY000): Unknown system variable 'gloabl'

mysql> set global interactive_timeout = 500;

Query OK, 0 rows affected (0.00 sec)

mysql> exit

Bye

[root@localhost ~]# 



举报

相关推荐

0 条评论