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

本文涉及的产品
云数据库 RDS MySQL Serverless,0.5-2RCU 50GB
云数据库 RDS MySQL Serverless,价值2615元额度,1个月
简介:

错误信息如下
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配置文件

[root@rh64 ~]# 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

[root@rh64 ~]# service mysql restart
Shutting down MySQL (Percona Server)..                     [  OK  ]
Starting MySQL (Percona Server).                           [  OK  ]
[root@rh64 ~]# 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%';

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
+-----------------------------------------------+-----------------+
| 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.检查程序代码,对于没有关闭的链接及时进行关闭










本文转自 客居天涯 51CTO博客,原文链接:http://blog.51cto.com/tiany/1725955,如需转载请自行联系原作者
相关实践学习
基于CentOS快速搭建LAMP环境
本教程介绍如何搭建LAMP环境,其中LAMP分别代表Linux、Apache、MySQL和PHP。
全面了解阿里云能为你做什么
阿里云在全球各地部署高效节能的绿色数据中心,利用清洁计算为万物互联的新世界提供源源不断的能源动力,目前开服的区域包括中国(华北、华东、华南、香港)、新加坡、美国(美东、美西)、欧洲、中东、澳大利亚、日本。目前阿里云的产品涵盖弹性计算、数据库、存储与CDN、分析与搜索、云通信、网络、管理与监控、应用服务、互联网中间件、移动服务、视频服务等。通过本课程,来了解阿里云能够为你的业务带来哪些帮助     相关的阿里云产品:云服务器ECS 云服务器 ECS(Elastic Compute Service)是一种弹性可伸缩的计算服务,助您降低 IT 成本,提升运维效率,使您更专注于核心业务创新。产品详情: https://www.aliyun.com/product/ecs
目录
相关文章
|
2月前
|
SQL 关系型数据库 MySQL
解决MySQL主从慢同步问题的常见的解决方案:
解决MySQL主从慢同步问题的方法有很多,以下是一些常见的解决方案: 1. 检查网络连接:确保主从服务器之间的网络连接稳定,避免网络延迟或丢包导致数据同步缓慢。 2. 优化数据库配置:调整MySQL的配置参数,如增大binlog文件大小、调整innodb_flush_log_at_trx_commit等参数,以提高主从同步性能。 3. 检查IO线程和SQL线程状态:通过SHOW SLAVE STATUS命令检查IO线程和SQL线程的状态,确保它们正常运行并没有出现错误。 4. 检查主从日志位置:确认主从服务器的binlog文件和位置是否正确,避免由于错误的日志位置导致同步延迟。 5.
173 1
|
2月前
|
关系型数据库 MySQL 数据库
深入探讨MySQL并发事务的问题及解决方案
深入探讨MySQL并发事务的问题及解决方案
82 0
|
3天前
|
关系型数据库 MySQL 数据库连接
用Navicat备份Mysql演示系统数据库的时候出:Too Many Connections
用Navicat备份Mysql演示系统数据库的时候出:Too Many Connections
|
5天前
|
运维 负载均衡 关系型数据库
MySQL高可用解决方案演进:从主从复制到InnoDB Cluster架构
MySQL高可用解决方案演进:从主从复制到InnoDB Cluster架构
|
1月前
|
缓存 NoSQL 关系型数据库
MySQL与Redis的默契协作:解析数据一致性难题与解决方案
MySQL与Redis的默契协作:解析数据一致性难题与解决方案
25 0
MySQL与Redis的默契协作:解析数据一致性难题与解决方案
|
1月前
|
Ubuntu 关系型数据库 MySQL
解决方案:MySQL数据表明明存在,但是就是报错表不存在,原来是需要配置 MySQL 忽略表名大小写!
解决方案:MySQL数据表明明存在,但是就是报错表不存在,原来是需要配置 MySQL 忽略表名大小写!
21 0
|
5月前
|
存储 关系型数据库 MySQL
Flink CDC中mysql cdc 抽取这个时间字段的值为null 有什么好的解决方案吗 ?
Flink CDC中mysql cdc 抽取这个时间字段的值为null 有什么好的解决方案吗 ?
107 0
|
1月前
|
缓存 关系型数据库 MySQL
mysql报Host is blocked because of many connection errors; unblock with ‘mysqladmin flush-hosts‘。
mysql报Host is blocked because of many connection errors; unblock with ‘mysqladmin flush-hosts‘。
12 0
|
3月前
|
关系型数据库 MySQL 数据库
Host 'XXX' is not allowed to connect to this MySQL server 解决方案
Host 'XXX' is not allowed to connect to this MySQL server 解决方案
|
3月前
|
SQL 关系型数据库 MySQL
MySQL - 死锁的产生及解决方案
MySQL - 死锁的产生及解决方案
212 0

推荐镜像

更多