mysql> status
mysql> show processlist; +----+------+-----------------+-------+---------+------+-------+------------------+ | Id | User | Host | db | Command | Time | State | Info | +----+------+-----------------+-------+---------+------+-------+------------------+ | 1 | root | localhost:40214 | b3log | Sleep | 18 | | NULL | | 2 | root | localhost:40215 | b3log | Sleep | 18 | | NULL | | 3 | root | localhost:40216 | b3log | Sleep | 18 | | NULL | | 4 | root | localhost:40217 | b3log | Sleep | 18 | | NULL | | 5 | root | localhost:40218 | b3log | Sleep | 18 | | NULL | | 6 | root | localhost:40219 | b3log | Sleep | 18 | | NULL | | 7 | root | localhost:40220 | b3log | Sleep | 18 | | NULL | | 8 | root | localhost:40221 | b3log | Sleep | 18 | | NULL | | 9 | root | localhost:40222 | b3log | Sleep | 18 | | NULL | | 10 | root | localhost:40223 | b3log | Sleep | 18 | | NULL | | 97 | root | localhost | NULL | Query | 0 | NULL | show processlist | +----+------+-----------------+-------+---------+------+-------+------------------+ 11 rows in set (0.00 sec)
| Qcachequeriesin_cache | 0 | | Qcachetotalblocks | 0 | | Queries | 16137 | | Questions | 2 | | Rpl_status | AUTH_MASTER | | Selectfulljoin | 0 | | Selectfullrange_join | 0 | | Select_range | 0 | | Selectrangecheck | 0 | | Select_scan | 0 | | Slaveheartbeatperiod | 0.000 | | Slaveopentemp_tables | 0 | | Slavereceivedheartbeats | 0 | | Slaveretriedtransactions | 0 | | Slave_running | OFF | | Slowlaunchthreads | 0 | | Slow_queries | 0 | | Sortmergepasses | 0 | | Sort_range | 0 | | Sort_rows | 0 | | Sort_scan | 0 | | Sslacceptrenegotiates | 0 | | Ssl_accepts | 0 | | Sslcallbackcache_hits | 0 | | Ssl_cipher | | | Sslcipherlist | | | Sslclientconnects | 0 | | Sslconnectrenegotiates | 0 | | Sslctxverify_depth | 0 | | Sslctxverify_mode | 0 | | Ssldefaulttimeout | 0 | | Sslfinishedaccepts | 0 | | Sslfinishedconnects | 0 | | Sslsessioncache_hits | 0 | | Sslsessioncache_misses | 0 | | Sslsessioncache_mode | NONE | | Sslsessioncache_overflows | 0 | | Sslsessioncache_size | 0 | | Sslsessioncache_timeouts | 0 | | Sslsessionsreused | 0 | | Sslusedsessioncacheentries | 0 | | Sslverifydepth | 0 | | Sslverifymode | 0 | | Ssl_version | | | Tablelocksimmediate | 2135 | | Tablelockswaited | 0 | | Tclogmaxpagesused | 0 | | Tclogpage_size | 0 | | Tclogpage_waits | 0 | | Threads_cached | 0 | | Threads_connected (连接数) | 11 | | Threads_created | 98 | | Threads_running | 1 | | Uptime | 28851 | | Uptimesinceflush_status | 28851 | +------------------------------------------+-------------+ 312 rows in set (0.00 sec)
如果不能看到全部 ,按ctrl + pageup或pagedown,进行上下翻页
如果觉得参数太多,可以筛选一些有兴趣的参数
命令:show status like ‘%变量%’
2. 这是是查询数据库当前设置的最大连接数
3. 杀掉线程
直接使用kill命令杀掉show processlist命令中Id一列的线程
mysql> show processlist;
show processlist;
+—–+——+———–+———+———+——-+——-+——————+
| Id | User | Host | db | Command | Time | State | Info |
+—–+——+———–+———+———+——-+——-+——————+
| 657 | prog | localhost | weather | Sleep | 28619 | | NULL |
| 782 | prog | localhost | weather | Sleep | 853 | | NULL |
| 785 | prog | localhost | NULL | Query | 0 | NULL | show processlist |
+—–+——+———–+———+———+——-+——-+——————+
3 rows in set (0.00 sec)
mysql>kill 657
Or, from the command line, to kill process 782
今天有一台mysql服务器突然连接数暴增,并且等待进程全部被锁...因为问题解决不当,导致被骂...OTL
总结:以后要快速定位错误,布置解决方案
登录到mysql客户端后,使用status命令也能获得thread连接数以及当前连接的id
或者用
show full processlist
看一下所有连接进程,注意查看进程等待时间以及所处状态 是否locked
如果进程过多,就把进程打印下来,然后查看
mysql -e 'show full processlist;' > 111
查找非locked的进程,一般就是当前执行中卡死,导致后面的进程排队的原因。
4. 跟踪线程
当MySQL繁忙的时候运行show processlist,会发现有很多行输出,每行输出对应一个MySQL连接。怎么诊断发起连接的进程是哪个?它当前正在干嘛呢?
首先,需要通过TCP Socket而不是Unix Socket连接MySQL,这样在show processlist的输出中就会有来源端口号。如下,
mysql> show processlist;
+——–+——–+—————–+——+———+——+——-+——————+
| Id | User | Host | db | Command | Time | State | Info |
+——–+——–+—————–+——+———+——+——-+——————+
| 277801 | mydbuser | localhost:35558 | mydb | Sleep | 1 | | NULL |
| 277804 | mydbuser | localhost:35561 | mydb | Sleep | 1 | | NULL |
| 277805 | mydbuser | localhost:35562 | mydb | Sleep | 0 | | NULL |
+——–+——–+—————–+——+———+——+——-+——————+
在Host列有来源IP和端口号,然后我们从连接机器查看端口号是谁打开的,
[root@localhost ~]# netstat -ntp | grep 35558
… 124.115.0.68:35558 ESTABLISHED 18783/httpd
可知进程18783发起的MySQL连接来源端口是35558,然后就可以用strace观察这个进程了。如果是Apache的PHP脚本,还可以用proctitle模块( http://pecl.php.net/package/proctitle/ )设置脚本的状态信息。
lsof也能根据端口号显示进程号,细节请参考手册。
http://www.mysqlperformanceblog.com/2007/02/08/debugging-sleeping-connections-with-mysql/
参考:
1. linux下查看mysql的当前连接情况
http://symphony.b3log.org/article/1373287442880
2. 查看mysql连接进程及连接用户
http://blog.csdn.net/andymu077/article/details/7011521
3. 查看mysql数据库连接数、并发数相关信息
http://blog.chinaunix.net/uid-20382003-id-3022768.html
4. 如何实时查看mysql当前连接数
http://blog.csdn.net/classicbear/article/details/7664042
5. 转:mysql show processlist命令 详解
http://renxiangzyq.iteye.com/blog/835397