MySQL Study之--“too many connections”错误解决方案

MySQL Study之--“too many connections”错误解决方案

错误信息如下:
Can not connect to MySQL server

Error: Too many connections
Errno.: 1040

Similar error report has beed dispatched to administrator before.

以下是mysql.com网站的相关说明: 
       If you get a Too many connections error when you try to connect to the mysqld server, this means that all available connections are in use by other clients. 
The number of connections allowed is controlled by the max_connections system variable. Its default value is100. If you need to support more connections, you should restart mysqld with a larger value for this variable.
      mysqld actually allows max_connections+1 clients to connect. The extra connection is reserved for use by accounts that have the SUPER privilege. By granting the SUPER privilege to administrators and not to normal users (who should not need it), an administrator can connect to the server and useSHOW PROCESSLIST to diagnose problems even if the maximum number of unprivileged clients are connected. See Section 13.5.4.19, “SHOW PROCESSLIST Syntax”. 
      The maximum number of connections MySQL can support depends on the quality of the thread library on a given platform. Linux or Solaris should be able to support 500-1000 simultaneous connections, depending on how much RAM you have and what your clients are doing. Static Linux binaries provided by MySQL AB can support up to 4000connections.

解决方法:


1、修改my.cnf配置文件

[[email protected] ~]# cat /etc/my.cnf
[mysqld]
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
user=mysql
# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0
innodb_data_file_path=ibdata1:12M;ibdata2:10M:autoextend
sql_mode=STRICT_TRANS_TABLES ,NO_ENGINE_SUBSTITUTION
slow_query_log=true
slow_query_log_file = "/var/lib/mysql/rh64-slow.log"
long_query_time=1
log-queries-not-using-indexes=true
max_connections=1000

2、重启mysql server

[[email protected] ~]# service mysql restart
Shutting down MySQL (Percona Server)..                     [  OK  ]
Starting MySQL (Percona Server).                           [  OK  ]
[[email protected] ~]# mysql -u root -p
Enter password:
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 1
Server version: 5.6.25-73.1-log Percona Server (GPL), Release 73.1, Revision 07b797f
Copyright (c) 2009-2015 Percona LLC and/or its affiliates
Copyright (c) 2000, 2015, 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 variables like ‘%connect%‘;

+-----------------------------------------------+-----------------+
| Variable_name                                 | Value           |
+-----------------------------------------------+-----------------+
| character_set_connection                      | utf8            |
| collation_connection                          | utf8_general_ci |
| connect_timeout                               | 10              |
| disconnect_on_expired_password                | ON              |
| extra_max_connections                         | 1               |
| init_connect                                  |                 |
| max_connect_errors                            | 100             |
| 
max_connections                               | 1000     
       |
| max_user_connections                          | 0               |
| performance_schema_session_connect_attrs_size | 512             |
+-----------------------------------------------+-----------------+
10 rows in set (0.02 sec)

在my.cnf修改参数后,需要重新启动mysql server,如果不能重启,也可以查询连接的进程,然后尝试用kill id关闭一些进程。

mysql> show processlist;
+----+------+-----------+------+---------+------+-------+------------------+-----------+---------------+
| Id | User | Host      | db   | Command | Time | State | Info             | Rows_sent | Rows_examined |
+----+------+-----------+------+---------+------+-------+------------------+-----------+---------------+
|  1 | root | localhost | NULL | Query   |    0 | init  | show processlist |         0 |             0 |
+----+------+-----------+------+---------+------+-------+------------------+-----------+---------------+
1 row in set (0.00 sec)

mysql> kill  id   //查询出的进程 id

总结,解决问题的最终方法:

1.修改配置文件/etc/my.cnf,调整连接参数

2.检查程序代码,对于没有关闭的链接及时进行关闭

时间: 2025-01-04 00:17:57

MySQL Study之--“too many connections”错误解决方案的相关文章

Hibernate MySql "Too Many Connection"错误解决方案

用CXF+Hibernate+Spring+MySql做WebService项目,访问数据库一段时间后,一直出现"Too Many Connection"错误,然后数据库就不能访问了(连接失败,当然不能操作了) 进入MySql Command窗口,键入命令: show processlist; 发现数据库里有几百个连接,而且都是Sleep状态.默认情况下,MySql的最大连接是100 或150 (看你的MySql版本),这么多Sleep的连接,马上就会把连接上占满. 到网上找了n久,都

Web项目打成war包部署到tomcat时报MySQL Access denied for user 'root'@'localhost' (using password: YES)错误解决方案

Web项目使用使用root账号root密码进行部署,通过Eclipse加载到Tomcat服务器可以发布成功,打成war包放到tomcat的webapps目录无法发布成功,报错: jdbc.properties涉及Mysql配置: 错误很明显,与MySQL密码有关,但是两种方式部署项目使用的jdbc.properties配置文件是一样的.所以肯定不是密码不对引起的,只好找度娘,O(∩_∩)O哈哈~. 网上大部分帖通过运行mysql -u root -p命令登录时也会报这个错误,所以,我就尝试这种方

mac系统nginx+php7.2+mysql环境配置错误解决方案

Tip:本文并非是配置的流程指导文件,只是错误解决方案的提示及建议 1.mac自带apache.php环境,需要先关闭,php默认相关环境(包含原来的php-fpm相关文件都清理掉,默认目录:/private/etc/php-fpm.conf)最好删除掉(可自行google下) 2.建议通过brew安装管理nginx,默认网站根目录: /usr/local/var/www brew install nginx 配置nginx解析php(注意这里我设置的是9090端口,因为自己的9000端口冲突了

MySQL Study之--Mysql启动失败“mysql.host”

MySQL Study之--Mysql启动失败"mysql.host" 系统环境: 操作系统:RedHat EL55 DB Soft:  Mysql 5.6.4-m7 通过源码包安装mysql后,在启动mysqld时出现错误: [[email protected] mysql]# bin/mysqld_safe &[1] 15846[[email protected] mysql]# 150610 17:04:36 mysqld_safe Logging to '/usr/lo

MySql Study之--MySql日志管理

MySql Study之--MySql日志管理 一.日志文件类型 MySQL有几个不同的日志文件,可以帮助你找出mysqld内部发生的事情: 日志文件 日志文件信息内容 错误日志 记录启动.运行或停止mysqld时出现的问题.(log_err) 查询日志 记录建立的客户端连接和执行的语句. 更新日志 记录更改数据的语句,不赞成使用该日志. 二进制日志 记录所有更改数据的语句.还用于复制.(bin_log) 慢日志 记录所有执行时间超过long_query_time秒的所有查询或不使用索引的查询.

MySQL Study之--MySQL下图形工具的使用(phpMyAdmin)

MySQL Study之--MySQL下图形工具的使用(phpMyAdmin) 系统环境: RedHat EL6 数据库:  MySQL 5.6.4-m7 phpMyAdmin是一个用PHP编写的软件工具,是以PHP为基础,以Web-Base方式架构在网站主机上的MySQL的数据库管理工具,让管理者可用Web接口管理MySQL数据库.借由此Web接口可以成为一个简易方式输入繁杂SQL语法的较佳途径,尤其要处理大量资料的汇入及汇出更为方便.其中一个更大的优势在于由于phpMyAdmin跟其他PHP

MySQL Study之--MySQL体系结构深入解析

MySQL Study之--MySQL体系结构深入解析 MySQL体系架构 由连接池组件.管理服务和工具组件.sql接口组件.查询分析器组件.优化器组件.缓冲组件.插件式存储引擎.物理文件组成.mysql是独有的插件式体系结构,各个存储引擎有自己的特点. MySQL内存结构: Mysql 进程结构 Mysql不像oracle那样是通过多进程来完成其功能的.默认情况下,InnoDB存储引擎的后台线程有7个: 4个IO thread, 1个master thread, 1个锁(lock)监控线程,

linux安装php出现的各种错误解决方案

configure: error: Try adding --with-zlib-dir=<DIR>. Please check config.log for more information. -------------------------------------------------------------- 解决方案:下载zlib 安装解压缩zlib 然后在后面的参数加上 --with-zlib ./configure --prefix=/usr/local/php --with-

MySQL Study之--Mysql数据库备份工具(mysqldump)

MySQL Study之--Mysql数据库备份工具(mysqldump) 对于Mysql Database的备份方式有很多种,此次文档主要介绍mysqldump工具: mysqldump:      mysqldump工具很多方面类似相反作用的工具mysqlimport.它们有一些同样的选项.但mysqldump能够做更多的事情.它可以把整个数据库装载到一个单独的文本文件中.这个文件包含有所有重建您的数据库所需要的SQL命令.这个命令取得所有的模式(Schema,后面有解释)并且将其转换成DD