解决Too many connections问题
有些人觉得,解决too many connections问题,灰非简单,down了mysql,修改my.cnf调大max_connections,好吧,你想法是没错的,这的确可以解决问题,但试问对于线上在跑的MySQL,你能随便down吗?嘻嘻,如果不行,只能用另外的方法了
一旦出现了too many connections错误,DBA或者运维人员已经连接不上MySQL去动态修改max_connections了,下面做实验来演示一下:
为了方便演示,我把max_connections调小到4:
mysql> set global max_connections=4;
Query OK, 0 rows affected (0.00 sec) mysql> show variables like 'max_connec%';
+--------------------+-------+
| Variable_name | Value |
+--------------------+-------+
| max_connect_errors | 10 |
| max_connections | 4 |
+--------------------+-------+
2 rows in set (0.00 sec) mysql>
然后打开几个seesion连接mysql,当连接超出4个时,报以下错误:
[root ~]$ mysql -uroot -p123456 -S /data/mysql-5.5/mysql.sock
ERROR 1040 (HY000): Too many connections
提示也非常明显,就是我们配置的连接数太小,现在已经用完了,这时我们只能通过hack的方法,用过gdb直接修改mysqld内存中max_connections的值,具体做法如下:
可能很人的系统环境上没有安装gdb,安装下即可:
[root ~]$ gdb -p $(cat /data/mysql-5.5/localhost.localdomain.pid) -ex "set max_connections=500" -batch
-bash: gdb: command not found
[root ~]$ yum install gdb -y
然后执行gdb -p $(cat /data/mysql-5.5/localhost.localdomain.pid) -ex "set max_connections=500" -batch; cat后面跟的是你当前mysql服务的pid文件,根据自己的存放指定即可,后面是修改mysqld内存中max_connections为多少
也可以这样查看自己的pid文件存放在那里:
mysql> show variables like '%pid%';
+---------------+-------------------------------------------+
| Variable_name | Value |
+---------------+-------------------------------------------+
| pid_file | /data/mysql-5.5/localhost.localdomain.pid |
+---------------+-------------------------------------------+
1 row in set (0.00 sec)
[root ~]$ gdb -p $(cat /data/mysql-5.5/localhost.localdomain.pid) -ex "set max_connections=500" -batch
[New LWP ]
[New LWP ]
[New LWP ]
[New LWP ]
[New LWP ]
[Thread debugging using libthread_db enabled]
0x00007f49f1c82333 in poll () from /lib64/libc.so.
这时已经可以连接mysql了,查看它的max_connections已经不再是之前的4了,而是500了
mysql -uroot -p123456 -S /data/mysql-5.5/mysql.sock
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 6
Server version: 5.5.40-log MySQL Community Server (GPL) Copyright (c) 2000, 2014, 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 global variables like 'max_conn%';
+--------------------+-------+
| Variable_name | Value |
+--------------------+-------+
| max_connect_errors | 10 |
| max_connections | 500 |
+--------------------+-------+
2 rows in set (0.04 sec) mysql>
在Percona5.5的thread_pool里面提供了2个参数extra_port和extra_max_connections预留额外的连接,预防连接满了以后我们无法进入数据库进行相应的管理。
[root msb_5_5_40]$ ./use
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 1
Server version: 5.5.40-36.1 Percona Server (GPL), Release 36.1, Revision 707 Copyright (c) 2009-2014 Percona LLC and/or its affiliates
Copyright (c) 2000, 2014, 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 ((none)) > show variables like '%extra%';
+-----------------------+-------+
| Variable_name | Value |
+-----------------------+-------+
| extra_max_connections | 1 |
| extra_port | 10010 |
+-----------------------+-------+
2 rows in set (0.00 sec)
当出现超出最大链接的错误时,可以指定用户名 密码 端口登录,端口为extra_port设置的,不是mysql服务自身的端口,相信大家都觉得这个功能很贴心吧,哈哈^.^
总结:
通常有两个参数控制控制最大连接数:
max_connections:该实例允许最大的连接数
max_user_connections:该实例允许每个用户的最大连接数
每个人要根本自己业务量,设置合适的值,不要盲目有设置过大,但也不设置过小,因为MySQL在连接数上升的情况下性能下降非常厉害,如果需要大量连接,这时可以引入thread_pool,所以我们需要保持一个原则:系统创建的用户(给应用使用用户)数* max_user_connections < max_connections。这样就不会发生文章开始说的问题。
参考资料:
http://www.mysqlperformanceblog.com/2010/03/23/too-many-connections-no-problem/
http://www.percona.com/doc/percona-server/5.5/performance/threadpool.html
http://www.cnblogs.com/gomysql/p/3834797.html
|
作者:陆炫志 出处:xuanzhi的博客 http://www.cnblogs.com/xuanzhi201111 您的支持是对博主最大的鼓励,感谢您的认真阅读。本文版权归作者所有,欢迎转载,但请保留该声明。 |
解决Too many connections问题的更多相关文章
- JDBC整合c3p0数据库连接池 解决Too many connections错误
前段时间,接手一个项目使用的是原始的jdbc作为数据库的访问,发布到服务器上在运行了一段时间之后总是会出现无法访问的情况,登录到服务器,查看tomcat日志发现总是报如下的错误. Caused by: ...
- libpqxx接口的在linux下的使用,解决psql:connections on Unix domain socket "/tmp/.s.PGSQL.5432"错误
在项目中使用postgresql数据库时要求在windows和linux双平台兼容.于是在windows下使用的接口在linux下爆出异常: psql:connections on Unix doma ...
- Navicat使用常见的两个问题及解决方法,提高开发效率
Navicat使用常见问题 在我们日常开发过程中,一般不会直接使用命令行来操作 MYSQL 数据库,而会选择一些图形化界面去帮助我们来进行此类操作,常用的有:SQLyog(Logo也是小海豚),Nav ...
- Data source rejected establishment of connection, message from server: "Too many connections"解决办法
异常名称 //数据源拒绝从服务器建立连接.消息:"连接太多" com.MySQL.jdbc.exceptions.jdbc4.MySQLNonTransientConnection ...
- (转)MySQL提示“too many connections”的解决办法
link:http://www.cfp8.com/mysql-prompt-too-many-connections-solution.html 今天生产服务器上的MySQL出现了一个不算太陌生的错误 ...
- 解决 -ERR Plaintext authentication disallowed on non-secure (SSL/TLS) connections 方案[sendmail, dovecot]
在linux下安装sendmail比较容易, 但是在配置sendmail时却是比较麻烦的, 特别是对于一些新手来说, 配置过程必须十分小心谨慎, 要知道, 错误是千奇百怪, 但是成功的结果就只有一个, ...
- 解决mysql 1040错误Too many connections的方法
从官方文档知道linux上面编译安装的mysql默认的连接为100个,这样对于网站的需求来说是远远不够的. mysql官方告诉我们需要修改max_connections的值,那么我们怎么去修改呢?有两 ...
- mysql 提示too many connections”的解决办法
最近使用python多线程连接mysq打数据,安装好mysql后,使用500线程连接发现提示:too many connections, 查询方法得知是需要进行配置才行: 产生这种问题的原因是: 连接 ...
- MySQL提示“too many connections”的解决办法
今天生产服务器上的MySQL出现了一个不算太陌生的错误"Too many connections".平常碰到这个问题,我基本上是修改/etc/my.cnf的max_connecti ...
随机推荐
- Memcache 监控
本文介绍zabbix.nagios.cacti对Memcache的监控 一:zabbix企业应用之固定端口监控memcache 一.在客户端 1.到/usr/loca/zabbix/conf/zabb ...
- 位运算卷积-FWT
问题 给出两个幂级数 \(f,g\) ,求 \[ h=\sum _i\sum _jx^{i\oplus j}f_ig_j \] 其中 \(\oplus\) 是可拆分的位运算. 算法 由于位运算具有独立 ...
- oracle 查出一个表中字段值出现次数大于2的所有记录
表web_order 列 name ,businesscode, a.account 周桥 18929609222 3754031157710000妙药 18929609233 3754031157 ...
- 【题解】 bzoj1597: [Usaco2008 Mar]土地购买 (动态规划+斜率优化)
bzoj1597懒得复制,戳我戳我 Solution: 线性DP打牌\(+\)斜率优化 定义状态:\(dp[i]\)到了位置\(i\)最少花费 首先我们要发现,如果有一个小方块能被其他的大方块包围,其 ...
- NOI.AC 20181103 题解
CF 1037B Reach Median 班上 n个同学(n 是奇数)排成一排站队,为了美观,需要大家高度的中位数是 x. 你可以让同学们在脚下垫木板或者稍微蹲一点来达成这个目标.对任意一位同学的 ...
- Python 进程间的通信
#-*-coding:utf-8-*- '''python提供了多种进程间的通信方式,如:Queue,Pipe,Valie+Array等. Queue与Pipe的区别在于Pipe常用来在两个进程间通信 ...
- JAVA实现概率计算(数字不同范围按照不同几率产生随机数)
程序中经常遇到随机送红包之类的情景,这个随机还得指定概率,比如10%的机率可以得到红包.那么java怎么实现一个简单的概率计算了,见如下例子: int randomInt = RandomUtils. ...
- 【洛谷P1491】集合位置
题目大意:求给定的一张无向带权图的次短路. 题解:先跑一遍 spfa 求出从起点到终点的最短路,记录路径.接着枚举删边,并重新跑 spfa,统计最小值即可. 至于为什么 dp 做法不行,暂时还不清楚. ...
- linux查看硬盘详细信息
最近公司新进了一批设备,为了弄清楚厂家有没有忽悠我们,分配给我一个任务 查看磁盘是不是二手的,window上有鲁大师啥的那么linux有什么软件吗? 答案是有的! 叫做smart 具体请查看: htt ...
- mongodb 设置用户密码权限
1 设置管理员账户 use admindb.createUser({ user: "useradmin", pwd: "adminpassword", role ...