这个博文,将只是简单的记录一下,我们的数据库操作和使用中,加索引加不上去,分析的过程,其实比较简单,就是看有没有连接进程还在操作表。有的话,将其停掉(不影响业务的场景下)。

今天的主角是:

SHOW [FULL] PROCESSLIST

官方文档的描述如下:

SHOW PROCESSLIST shows you which threads are running. You can also get this information from the
INFORMATION_SCHEMA PROCESSLIST table or the mysqladmin processlist command. If you have
the PROCESS privilege, you can see all threads. Otherwise, you can see only your own threads (that is,
threads associated with the MySQL account that you are using). If you do not use the FULL keyword, only
the first characters of each statement are shown in the Info field.

意思就是说上述指令是用来查看那些线程正在运行,你也可以得到这些信息,从INFORMATION_SCHEMA PROCESSLIST这个表,或者通过mysqladmin processlist指令。如果你有PROCESS权限,你可以查看所有的线程。否则,你只能查看你自己当前账户的线程。如果你没有使用FULL关键字,你只能查看每个记录中Info字段里面的前100个字符

具体操作:

mysql> show processlist;
ERROR (HY000): MySQL server has gone away
No connection. Trying to reconnect...
Connection id:
Current database: advsql +-------+-----------+-------------------+--------+---------+-------+-------+------------------+
| Id | User | Host | db | Command | Time | State | Info |
+-------+-----------+-------------------+--------+---------+-------+-------+------------------+
| | tkcssuser | 10.90.13.61: | tkcss | Sleep | | | NULL |
| | tkcssuser | 10.90.13.8: | tkcss | Sleep | | | NULL |
| | root | localhost | advsql | Query | | init | show processlist |
+-------+-----------+-------------------+--------+---------+-------+-------+------------------+
rows in set (0.01 sec)

通过查询information_schema的processlist表:

mysql> select * from information_schema.processlist;
+-------+-----------+-------------------+--------+---------+-------+-----------+----------------------------------------------+
| ID | USER | HOST | DB | COMMAND | TIME | STATE | INFO |
+-------+-----------+-------------------+--------+---------+-------+-----------+----------------------------------------------+
| | root | localhost | advsql | Query | | executing | select * from information_schema.processlist |
| | tkcssuser | 10.90.13.61: | tkcss | Sleep | | | NULL |
| | tkcssuser | 10.90.13.8: | tkcss | Sleep | | | NULL |
+-------+-----------+-------------------+--------+---------+-------+-----------+----------------------------------------------+
rows in set (0.00 sec)

或者mysqladmin processlist指令:

[root@localhost ~]# mysqladmin processlist -u root -p
Enter password:
+-------+-----------+-------------------+--------+---------+-------+-------+------------------+
| Id | User | Host | db | Command | Time | State | Info |
+-------+-----------+-------------------+--------+---------+-------+-------+------------------+
| | tkcssuser | 10.90.13.61: | tkcss | Sleep | | | |
| | tkcssuser | 10.90.13.8: | tkcss | Sleep | | | |
| | root | localhost | advsql | Sleep | | | |
| | root | localhost | | Query | | init | show processlist |
+-------+-----------+-------------------+--------+---------+-------+-------+------------------+

关于这个命令的价值,官方的介绍如下:

The SHOW PROCESSLIST statement is very useful if you get the “too many connections” error message
and want to find out what is going on. MySQL reserves one extra connection to be used by accounts that
have the SUPER privilege, to ensure that administrators should always be able to connect and check the
system (assuming that you are not giving this privilege to all your users). Threads can be killed with the KILL statement.

运行的线程,可以通过KILL指令将其杀掉。这个信息非常重要。

这里,可以看到SHOW PROCESSLIST指令里面的输出内容,有几个字段,下面给予解释说明,只有搞清楚了这些字段的含义,才对我们的实际项目问题分析才有价值:

  • Id:

    The connection identifier. This is the same type of value displayed in the ID column of the
    INFORMATION_SCHEMA.PROCESSLIST table, the PROCESSLIST_ID column of the Performance
    Schema threads table, and returned by the CONNECTION_ID() function.

  • User:

    The MySQL user who issued the statement. If this is system user, it refers to a nonclient thread
    spawned by the server to handle tasks internally. This could be the I/O or SQL thread used on replication
    slaves or a delayed-row handler. unauthenticated user refers to a thread that has become
    associated with a client connection but for which authentication of the client user has not yet been done.
    event_scheduler refers to the thread that monitors scheduled events. For system user, there is no
    host specified in the Host column.

  • Host:

    The host name of the client issuing the statement (except for system user where there is no host).
    SHOW PROCESSLIST reports the host name for TCP/IP connections in host_name:client_port
    format to make it easier to determine which client is doing what.

  • db:

     The default database, if one is selected, otherwise NULL.

  • Command:

     The type of command the thread is executing. 例如上面的例子中,Sleep,或者Query

  • Time:

    The time in seconds that the thread has been in its current state. For a slave SQL thread, the value is
    the number of seconds between the timestamp of the last replicated event and the real time of the slave
    machine.

  • State:

    Most states correspond to very quick operations. If a thread stays in a given state for many seconds,
    there might be a problem that needs to be investigated.

     An action, event, or state that indicates what the thread is doing.

  • Info:

    The statement the thread is executing, or NULL if it is not executing any statement. The statement might
    be the one sent to the server, or an innermost statement if the statement executes other statements. For
    example, if a CALL statement executes a stored procedure that is executing a SELECT statement, the
    Info value shows the SELECT statement.

上述信息的解释当中,对于我们分析问题,尤为重要的是Id,Host,Time,State以及Info字段。这几个当中,第一眼能让我们看出问题的是Time,State和Info字段。我们若想看得更详细的Info字段,请用SHOW FULL PROCESSLIST指令。注意前面的命令解释内容。

我们线上的一个问题,就是加索引加不上去,我们将应用停掉还是加不上去,通过SHOW PROCESSLIST指令,发现有好多线程Command处于Query状态。最长的Time字段显示达到230172seconds,换算成小时,都63个小时,见鬼啊!!!!(这个地方有疑问

为了解决问题,立即调用KILL指令,前面介绍时提到的。

KILL [CONNECTION | QUERY] processlist_id

官方介绍:

Each connection to mysqld runs in a separate thread. You can kill a thread with the KILL
processlist_id statement.

参数中的processlist_id,来源于show processlist结果列表中的id字段。

kill指令支持两个可选参数,CONNECTION以及QUERY。

• KILL CONNECTION is the same as KILL with no modifier: It terminates the connection associated with
the given processlist_id, after terminating any statement the connection is executing.
• KILL QUERY terminates the statement the connection is currently executing, but leaves the connection
itself intact.

很简单,connection选项,kill的时候,将连接也断掉,而query选项,kill的过程只是将该指令杀掉,连接还保持。 kill指令不指定connection或者query选项时,默认是connection。

到此,今天要介绍的MySQL的查看数据运行连接状态的内容到此结束。

最后补充一点的是,前面提到的“这个地方有疑问”,真是有疑问,我们的应用3台服务器,6个tomcat应用,全部杀进程停掉了,为何最终SHOW PROCESSLIST里面还会出现有正在运行的QUERY,而且time都是20多万秒,难道一个select要查几天?如下图:

我们的应用在3天内,两个晚上,每天都会杀掉应用,做版本更新操作,怎么说,select也不会出现20多万秒吧?有谁能帮忙分析下,我的怀疑是这些被show processlist查出来的进程,在执行query的时候,MySQL内部发生死锁导致的,数据状态显示这个查询一直在,没有结束,因为死锁,若不杀进程,那个Time显示的时间将会更长。。。

还请高手帮忙分析或者给予一些线索。

MySQL分析数据运行状态利器【SHOW PROCESSLIST】的更多相关文章

  1. MySQL分析数据运行状态利器【show full processlist】

    原文地址:https://www.cnblogs.com/shihuc/p/8733460.html 今天的主角是: SHOW [FULL] PROCESSLIST show full process ...

  2. MySQL分析数据运行状态【SHOW PROCESSLIST】

    这个博文,将只是简单的记录一下,我们的数据库操作和使用中,加索引加不上去,分析的过程,其实比较简单,就是看有没有连接进程还在操作表.有的话,将其停掉(不影响业务的场景下). 今天的主角是: SHOW ...

  3. 使用Sqoop从MySQL导入数据到Hive和HBase 及近期感悟

    使用Sqoop从MySQL导入数据到Hive和HBase 及近期感悟 Sqoop 大数据 Hive HBase ETL 使用Sqoop从MySQL导入数据到Hive和HBase 及近期感悟 基础环境 ...

  4. pt-query-digest怎么分析慢查询日志分析数据

    在进行使用linux系统作为服务器的情况,那几需要进行对linux的服务器进行性能上数据进行抓取之后,就需要对数据中内容进行分析,看数据库中内容是否存在瓶颈上的问题,在进行获取到的数据的慢查日志,将使 ...

  5. 264分析两大利器:264VISA和Elecard StreamEye Tools

    学了264有将近3个月有余,好多时候都在学习老毕的书和反复看JM86的代码,最近才找到264分析两大利器:264VISA和Elecard StreamEye Tools.不由得感叹,恨不逢同时. 简单 ...

  6. mysql插入数据时,中文乱码

    MySQL 插入数据时,中文乱码问题的解决(转) 当向 MySQL 数据库插入一条带有中文的数据形如 insert into employee values(null,'张三','female','1 ...

  7. Mysql删除数据后磁盘空间未释放的解决办法【转】

    转自 Mysql删除数据后,磁盘空间未释放的解决办法 - 今日头条(TouTiao.com)http://toutiao.com/a6303087712678412546/?tt_from=mobil ...

  8. 警惕!MySQL成数据勒索新目标

    据最新报道显示,继MongoDB和Elasticsearch之后,MySQL成为下个数据勒索目标,从2月12日凌晨开始,已有成百上千个开放在公网的MySQL数据库被劫持,删除了数据库中的存储数据,攻击 ...

  9. 利用TPC-H为MYSQL生成数据

    ## 利用TPC-H为MYSQL生成数据 导言 这篇文章是看了joyee写的TPC-H数据导入MySQL教程以及另一篇网上的MySQL TPCH测试工具简要手册 后写的,有些内容是完全转载自以上两篇文 ...

随机推荐

  1. winform 点击控件拖动窗体

    private Point mPoint = new Point(); private void 选择控件_MouseDown(object sender, MouseEventArgs e) { m ...

  2. python day 16作业

    day18天作业及默写1,完成下列功能: 1.1创建一个人类Person,再类中创建3个静态变量(静态字段) animal = '高级动物' soup = '有灵魂' language = '语言' ...

  3. 【Python】多进程-共享变量(Value、string、list、Array、dict)

    #练习:未使用共享变量 from multiprocessing import Process def f(n, a): n = 3.1415927 for i in range(len(a)): a ...

  4. 基于链路的OSPFMD5口令认证

    实验要求:掌握基于链路的OSPFMD5口令认证 拓扑如下: 配置如下: R1enable configure terminal interface s0/0/0ip address 192.168.1 ...

  5. angularJS使用编写KindEditor,UEidtor,jQuery指令,双重绑定

    第一步 <!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8 ...

  6. webgl opengl教程样例

    webgl2样例: http://webglsamples.org opengl教程: https://learnopengl.com/ http://www.opengl-tutorial.org/ ...

  7. Java简单双向链表实现 @version 1.0

    package com.list; /** * 数据结构和算法Java表示 双向链表 * * @version 1.0 * @author 小明 * */ public class MyDoublel ...

  8. Python之路PythonNet,第二篇,网络2

    pythonnet   网络2 问题: 什么是七层模型tcp 和udp区别三次握手和四次挥手************************************************** tcp ...

  9. Unity调用Windows对话框保存时另存为弹框

    Unity开发VR之Vuforia 本文提供全流程,中文翻译. Chinar 坚持将简单的生活方式,带给世人!(拥有更好的阅读体验 -- 高分辨率用户请根据需求调整网页缩放比例) Chinar -- ...

  10. xdoj-1211 (尧老师要教孩子解方程) :多项式除法

    想法: 1 由于所有a[i] 是不为0的整数 所以解x是整数 2 其次解是an的约数 3 分解a[n] 用多项式除法判断约数是否为整式的解 #include<cstdio> #includ ...