唠唠mysql的连接数
1.查看mysql的当前连接数
[root@localhost ~]# mysqladmin -uroot -p123456 status Uptime: 34070 Threads: 1 Questions: 8678 Slow queries: 0 Opens: 910 Flush tables: 43 Open tables: 4 Queries per second avg: 0.254
Therads 打开的连接数
mysql> show status like ‘%Thread%‘;
+------------------------------------------+-------+
| Variable_name | Value |
+------------------------------------------+-------+
| Delayed_insert_threads | 0 |
| Performance_schema_thread_classes_lost | 0 |
| Performance_schema_thread_instances_lost | 0 |
| Slow_launch_threads | 0 |
| Threads_cached | 0 |
| Threads_connected | 1 |
| Threads_created | 4 |#创建的连接数
| Threads_running | 1 |#打开的连接数
+------------------------------------------+-------+
8 rows in set (0.00 sec
2.查看mysql允许的最大连接数
mysql> show variables like ‘%max_connections%‘; +-----------------+-------+ | Variable_name | Value | +-----------------+-------+ | max_connections | 151 | +-----------------+-------+ 1 row in set (0.00 sec)
3.mysql连接数太多解决方案
http://www.cnblogs.com/lazyball/p/7424243.html
说说mysql的慢查询
慢查询产生的原因一般是因为select设计的条件比较多,比如子查询之类的,数据库一直在插卡在哪里,别的语句也无法运行造成阻塞。开发也不是万能的,也会写一些 吊炸天的sql语句,一眼望去看不到结尾。
1. 查看mysql里有哪些进程在运行
mysql> show processlist; #默认一般显示100行 加上full显示的多 +----+------+-----------+------+---------+------+-------+------------------+ | Id | User | Host | db | Command | Time | State | Info | +----+------+-----------+------+---------+------+-------+------------------+ | 4 | root | localhost | NULL | Sleep | 882 | | NULL | | 6 | root | localhost | NULL | Query | 0 | NULL | show processlist | +----+------+-----------+------+---------+------+-------+------------------+ 2 rows in set (0.00 sec) mysql> show full processlist; +----+------+-----------+------+---------+------+-------+-----------------------+ | Id | User | Host | db | Command | Time | State | Info | +----+------+-----------+------+---------+------+-------+-----------------------+ | 4 | root | localhost | NULL | Sleep | 894 | | NULL | | 6 | root | localhost | NULL | Query | 0 | NULL | show full processlist | +----+------+-----------+------+---------+------+-------+-----------------------+ 2 rows in set (0.00 sec)
[root@localhost ~]# mysqladmin -uroot -p processlist
Enter password:
+----+------+-----------+----+---------+------+-------+------------------+
| Id | User | Host | db | Command | Time | State | Info |
+----+------+-----------+----+---------+------+-------+------------------+
| 4 | root | localhost | | Sleep | 1238 | | |
| 7 | root | localhost | | Query | 0 | | show processlist |
+----+------+-----------+----+---------+------+-------+------------------+
参数解释:
id #ID标识,要kill一个语句的时候很有用
use #当前连接用户
host #显示这个连接从哪个ip的哪个端口上发出
db #数据库名
command #连接状态,一般是休眠(sleep),查询(query),连接(connect)
time #连接持续时间,单位是秒
state #显示当前sql语句的状态
info #显示这个sql语句
##慢查询发生的时候,state状态一般是 watting for table level lock 观察time 看是否运行的时间很久,紧急就杀死,不紧急找开发去,写的什么玩意(自己知道咋优化
的话,找开发炫耀去)
2. 紧急批量杀死mysql慢查询的脚本
先说自己写的吧
#!/bin/bash for id in `mysqladmin -uxxx -h 192.168.1.1 -p‘xxxx‘ processlist | grep Query | grep ‘paigoushop‘ | grep ‘Sending data‘ | awk ‘{print $2}‘` do mysqladmin -uxxx -h 192.168.1.1 -p‘xxx‘ kill ${id} done
#grep有点多 为了准确定位到自己想杀死的sql
下边这个是网上找的,我也没试过 放这里 万一以后自己用呢
#!/bin/bash for i in `mysql -Bse ‘show full processlist;‘ | grep -i select |grep -i "Waiting for table level lock" | awk ‘{print $1}‘` do mysql -Bse "kill $i" done
3. 开启慢查询日志记录
总觉得上边的方法不太好,比较被动,对于一些不懂的人,又要骂运维了,我来学一下:“不,什么情况呀,小杨,你这数据库也太慢了吧,怎么搞得,赶紧给我查。”
说起来都是泪呀,那就不说了。用事实说话,把sql语句拿出来,让开发好好看看写的什么玩意。