MySQL 客户端与MySQL server建立连接后,就可以执行SQL语句了。

如何查看一个连接上是否正在执行SQL语句,或者连接是否处于空闲呢?

下面我们做下测试。

1.查看连接的空闲时间

首先看下测试程序。

代码中,每3s执行一条sql语句。

//conn_idle_time.go
package main import (
"database/sql"
"log"
"time" _ "github.com/go-sql-driver/mysql" ) var DB *sql.DB
var dataBase = "root:Aa123456@tcp(127.0.0.1:3306)/?loc=Local&parseTime=true" func mysqlInit() {
var err error
DB, err = sql.Open("mysql", dataBase)
if err != nil {
log.Fatalln("open db fail:", err)
} DB.SetMaxOpenConns(1) err = DB.Ping()
if err != nil {
log.Fatalln("ping db fail:", err)
}
} func main() {
mysqlInit() for {
execSql()
time.Sleep(3*time.Second)
}
} func execSql() {
var connection_id int
err := DB.QueryRow("select CONNECTION_ID()").Scan(&connection_id)
if err != nil {
log.Println("query connection id failed:", err)
return
} log.Println("connection id:", connection_id)
}

启动程序,输出结果如下:

2019/10/13 12:20:59 connection id: 5
2019/10/13 12:21:02 connection id: 5
2019/10/13 12:21:05 connection id: 5
2019/10/13 12:21:08 connection id: 5
2019/10/13 12:21:11 connection id: 5
2019/10/13 12:21:14 connection id: 5
2019/10/13 12:21:17 connection id: 5
2019/10/13 12:21:20 connection id: 5
2019/10/13 12:21:23 connection id: 5
2019/10/13 12:21:26 connection id: 5
2019/10/13 12:21:29 connection id: 5

可以看到,连接MySQL的connection id 为5。

接着,通过show processlist查看连接情况:

mysql> show processlist;
+----+------+-----------------+------+---------+------+----------+------------------+
| Id | User | Host | db | Command | Time | State | Info |
+----+------+-----------------+------+---------+------+----------+------------------+
| 3 | root | localhost | NULL | Query | 0 | starting | show processlist |
| 5 | root | localhost:51823 | NULL | Sleep | 0 | | NULL |
+----+------+-----------------+------+---------+------+----------+------------------+
2 rows in set (0.00 sec) mysql> show processlist;
+----+------+-----------------+------+---------+------+----------+------------------+
| Id | User | Host | db | Command | Time | State | Info |
+----+------+-----------------+------+---------+------+----------+------------------+
| 3 | root | localhost | NULL | Query | 0 | starting | show processlist |
| 5 | root | localhost:51823 | NULL | Sleep | 3 | | NULL |
+----+------+-----------------+------+---------+------+----------+------------------+
2 rows in set (0.00 sec) mysql> show processlist;
+----+------+-----------------+------+---------+------+----------+------------------+
| Id | User | Host | db | Command | Time | State | Info |
+----+------+-----------------+------+---------+------+----------+------------------+
| 3 | root | localhost | NULL | Query | 0 | starting | show processlist |
| 5 | root | localhost:51823 | NULL | Sleep | 1 | | NULL |
+----+------+-----------------+------+---------+------+----------+------------------+
2 rows in set (0.00 sec) mysql> show processlist;
+----+------+-----------------+------+---------+------+----------+------------------+
| Id | User | Host | db | Command | Time | State | Info |
+----+------+-----------------+------+---------+------+----------+------------------+
| 3 | root | localhost | NULL | Query | 0 | starting | show processlist |
| 5 | root | localhost:51823 | NULL | Sleep | 3 | | NULL |
+----+------+-----------------+------+---------+------+----------+------------------+
2 rows in set (0.00 sec) mysql> show processlist;
+----+------+-----------------+------+---------+------+----------+------------------+
| Id | User | Host | db | Command | Time | State | Info |
+----+------+-----------------+------+---------+------+----------+------------------+
| 3 | root | localhost | NULL | Query | 0 | starting | show processlist |
| 5 | root | localhost:51823 | NULL | Sleep | 2 | | NULL |
+----+------+-----------------+------+---------+------+----------+------------------+
2 rows in set (0.00 sec) mysql> show processlist;
+----+------+-----------------+------+---------+------+----------+------------------+
| Id | User | Host | db | Command | Time | State | Info |
+----+------+-----------------+------+---------+------+----------+------------------+
| 3 | root | localhost | NULL | Query | 0 | starting | show processlist |
| 5 | root | localhost:51823 | NULL | Sleep | 1 | | NULL |
+----+------+-----------------+------+---------+------+----------+------------------+
2 rows in set (0.00 sec)

可以看到localhost:51823是连接MySQL server使用的端口,当Command列为Sleep时,表示当前连接是空闲的,Time列显示了处于当前状态的时间。

每执行一次SQL语句,Sleep状态的Time都会被重置为0.

如果当前连接一直没有执行SQL语句,那么Sleep状态的Time会一直增加,直到达到连接最大时间(由参数wait_timeout控制),最后连接断开。

再来看下,如何查看正在执行的SQL语句。

2.查看连接正在执行的SQL

将上面示例程序执行的SQL语句改为:

select SLEEP(10)

查看连接情况:

mysql> show processlist;
+----+------+-----------------+------+---------+------+------------+------------------+
| Id | User | Host | db | Command | Time | State | Info |
+----+------+-----------------+------+---------+------+------------+------------------+
| 3 | root | localhost | NULL | Query | 0 | starting | show processlist |
| 6 | root | localhost:52186 | NULL | Query | 1 | User sleep | select SLEEP(10) |
+----+------+-----------------+------+---------+------+------------+------------------+
2 rows in set (0.00 sec) mysql> show processlist;
+----+------+-----------------+------+---------+------+------------+------------------+
| Id | User | Host | db | Command | Time | State | Info |
+----+------+-----------------+------+---------+------+------------+------------------+
| 3 | root | localhost | NULL | Query | 0 | starting | show processlist |
| 7 | root | localhost:52200 | NULL | Query | 3 | User sleep | select SLEEP(10) |
+----+------+-----------------+------+---------+------+------------+------------------+
2 rows in set (0.00 sec) mysql> show processlist;
+----+------+-----------------+------+---------+------+------------+------------------+
| Id | User | Host | db | Command | Time | State | Info |
+----+------+-----------------+------+---------+------+------------+------------------+
| 3 | root | localhost | NULL | Query | 0 | starting | show processlist |
| 7 | root | localhost:52200 | NULL | Query | 7 | User sleep | select SLEEP(10) |
+----+------+-----------------+------+---------+------+------------+------------------+
2 rows in set (0.00 sec) mysql> show processlist;
+----+------+-----------------+------+---------+------+------------+------------------+
| Id | User | Host | db | Command | Time | State | Info |
+----+------+-----------------+------+---------+------+------------+------------------+
| 3 | root | localhost | NULL | Query | 0 | starting | show processlist |
| 7 | root | localhost:52200 | NULL | Query | 8 | User sleep | select SLEEP(10) |
+----+------+-----------------+------+---------+------+------------+------------------+
2 rows in set (0.00 sec) mysql> show processlist;
+----+------+-----------------+------+---------+------+------------+------------------+
| Id | User | Host | db | Command | Time | State | Info |
+----+------+-----------------+------+---------+------+------------+------------------+
| 3 | root | localhost | NULL | Query | 0 | starting | show processlist |
| 7 | root | localhost:52200 | NULL | Query | 10 | User sleep | select SLEEP(10) |
+----+------+-----------------+------+---------+------+------------+------------------+
2 rows in set (0.00 sec) mysql> show processlist;
+----+------+-----------------+------+---------+------+----------+------------------+
| Id | User | Host | db | Command | Time | State | Info |
+----+------+-----------------+------+---------+------+----------+------------------+
| 3 | root | localhost | NULL | Query | 0 | starting | show processlist |
| 7 | root | localhost:52200 | NULL | Sleep | 12 | | NULL |
+----+------+-----------------+------+---------+------+----------+------------------+
2 rows in set (0.00 sec)

从输出可以看到,CommandQuery表示正在执行语句,Info列显示了正在执行的具体SQL语句。

查看MySQL 连接信息--连接空闲时间及正在执行的SQL的更多相关文章

  1. 查看mysql 库信息和表结构与表创建方法

    一.查看基础信息 1.查看数据库存储位置 show global variables like "%datadir%"; 2.查看数据库的版本 select version(); ...

  2. 查看mysql相关信息

    查看本机mysql的相关信息,执行以下SQL即可: SHOW VARIABLES LIKE "%char%";

  3. mysql中如何在命令行中,执行一个SQL脚本文件?

    需求描述: 在mysql数据库的使用中,有的时候,需要直接在shell的命令行中,执行某个SQL脚本文件, 比如,要初始化数据库,创建特定的存储过程,创建表等操作,这里进行一个基本的测试. 一般情况, ...

  4. 如何查看MySQL connection id连接id

    每个MySQL连接,都有一个连接ID,可以通过 connection_id()查看. 连接id也可以通过以下方式查看: show processlist中id列 information_schema. ...

  5. 查看MySQL数据的连接

      show processlist;   select host from information_schema.processlist;   查看那台机器及连接数 select host, cur ...

  6. mysql 查看mysql相关信息

    登入数据库的时候: select @@version; select version(); 复制代码 mysql> select @@version; +-----------+ | @@ver ...

  7. mysql工具——使用mysqlshow查看mysql对象信息,查看mysql表大小

    关键词:查看表大小,mysqlshow mysqlshow --count -uroot -p test

  8. 查看mysql数据库的所有配置信息和服务器的各种状态

    查看MySQL服务器配置信息 mysql> show variables; 2, 查看MySQL服务器运行的各种状态值 mysql> show global status;

  9. 查看mysql是否安装成功和mysql的版本信息

    转自:https://blog.csdn.net/hellocsz/article/details/81241204 使用快捷键win+R打开 进入mysql的安装目录下的\bin(本人安装路劲为E: ...

随机推荐

  1. 介绍两个好玩的和Github相关的Chrome扩展

    1. Octotree 默认的github网页里的代码显示没有我们在IDE里看到的直观,即代码文件所在的文件夹无法以树形层级结构显示在屏幕左边. 安装Octotree之后: 方便多了. 2. Isom ...

  2. 7.JSP技术

    JSP和Servlet一样,都是用于开发动态web资源的技术 JSP的两个特性:1.写JSP代码就像是在写HTML代码,但JSP技术允许在页面中编写java代码(理解为一种特殊的HTML) 2.JSP ...

  3. python3 中的cls和self的区别 静态方法和类方法的区别

      一般来说,要使用某个类的方法,需要先实例化一个对象再调用方法. 而使用@staticmethod或@classmethod,就可以不需要实例化,直接类名.方法名()来调用. 这有利于组织代码,把某 ...

  4. 【坑】Spring中抽象父类属性注入,子类调用父类方法使用父类注入属性

    运行环境 idea 2017.1.1 spring 3.2.9.RELEASE 需求背景 需要实现一个功能,该功能有2个场景A.B,大同小异 抽象一个抽象基类Base,实现了基本相同的方法BaseMe ...

  5. 【2】Kafka概念及原理

    1.Kafka背景 1.1.Kafka概要  Apache Kafka是一个开源的.轻量级的.分布式的.可分区的.可复制备份的.基于zookeeper协调管理的分布式流式消息系统.由Scala写成,支 ...

  6. 批量kill指定名称的进程

     以Airflow举例: ps -ef | grep “airflow" | grep -v grep | cut -c 9-15 | xargs kill -9   分析: ps -ef  ...

  7. C# Winfrom 自定义控件添加图标

    Winfrom自定义控件添加自定义图标实现方式: 1.新建UserControl——略 2.寻找合适的图标文件——将文件和控件放置同一目录下(相同目录.自定义控件类名.图标文件名相同) 注:如果路径不 ...

  8. python基础:流程控制案例:

    1,简述编译型与解释型的语言,且分别列出你知道的哪些语言属于编译型,哪些属于解释型. 答:简单理解编译型语言类似谷歌翻译,整篇读入整篇翻译,代表语言有C语言,解释型语言类似同   声传译,读入一行翻译 ...

  9. asterisk搭建goip

    利用asterisk来搭建goip使用,步骤如下: 第一步:先需要搭建centos服务器并优化 搭建centos服务器就省略: 优化centos服务器,进行如下步骤: 关闭防火墙:systemctl ...

  10. Acwing-286-选课(树上DP)

    链接: https://www.acwing.com/problem/content/288/ 题意: 学校实行学分制. 每门的必修课都有固定的学分,同时还必须获得相应的选修课程学分. 学校开设了 N ...