[笔记]MySQL查看事务信息

我们可以使用 status 获取当前连接的状态:

mysql> status -- 查看当前连接状态
--------------
D:\mysql\bin\mysql.exe  Ver 14.14 Distrib 5.7.36, for Win64 (x86_64) -- 当前连接使用的客户端

Connection id:          3  -- 当前连接id(线程id)
Current database:       test -- 当前连接数据库
Current user:           root@localhost -- 当前连接用户
SSL:                    Cipher in use is ECDHE-RSA-AES128-GCM-SHA256
Using delimiter:        ; -- 当前连接分隔符
Server version:         5.7.36-log MySQL Community Server (GPL)
Protocol version:       10
Connection:             localhost via TCP/IP
Server characterset:    utf8
Db     characterset:    utf8
Client characterset:    gbk
Conn.  characterset:    gbk
TCP port:               3306
Uptime:                 2 hours 23 min 1 sec

Threads: 2  Questions: 38  Slow queries: 0  Opens: 121  Flush tables: 1  Open tables: 114  Queries per second avg: 0.004
--------------

Connection id 表示当前会话的连接线程id,当前线程id为 3


除此之外,还可以使用 show processlist 查看当前MySQL 服务器上存在哪些连接:

mysql> show processlist\G
*************************** 1. row ***************************
     Id: 3        -- 当前线程id
   User: root     -- 当前线程登录用户
   Host: localhost:65447 -- 线程登录主机
     db: test     -- 线程使用数据库
Command: Query    -- 当前线程执行命令类型
   Time: 0
  State: starting  -- 当前连接状态
   Info: show processlist  -- 当前连接执行SQL
*************************** 2. row ***************************
     Id: 4
   User: root
   Host: localhost:65431
     db: test
Command: Sleep
   Time: 405
  State:
   Info: NULL
2 rows in set (0.00 sec)

MySQL 会为每个到来的连接新建一个线程


使用SQL可以查看当前都有哪些事务信息:

select * from information_schema.innodb_trx\G

示例:

mysql> select * from information_schema.innodb_trx\G
*************************** 1. row ***************************
                    trx_id: 6274333      -- 事务id
                 trx_state: RUNNING      -- 事务状态
               trx_started: 2022-10-10 16:16:05     -- 事务开始时间
     trx_requested_lock_id: NULL       -- 事务请求锁定的 id
          trx_wait_started: NULL       -- 事务是否处于等待状态
                trx_weight: 24637      -- 事务大小
       trx_mysql_thread_id: 4          -- 事务关联的 MySQL 线程id
                 trx_query: NULL
       trx_operation_state: NULL       -- 事务操作状态
         trx_tables_in_use: 0          -- 事务使用的表数量
         trx_tables_locked: 1          -- 事务锁表数量
          trx_lock_structs: 24637      -- 事务锁定结构体
     trx_lock_memory_bytes: 2597072    -- 事务锁定内存大小,单位字节
           trx_rows_locked: 4598010    -- 事务锁定行数
         trx_rows_modified: 0          -- 事务修改行数
   trx_concurrency_tickets: 0
       trx_isolation_level: REPEATABLE READ   -- 当前事务隔离级别
         trx_unique_checks: 1          -- 事务是否进行唯一约束检查
    trx_foreign_key_checks: 1          -- 事务是否进行外键检查
trx_last_foreign_key_error: NULL
 trx_adaptive_hash_latched: 0          -- 事务自适应哈希锁
 trx_adaptive_hash_timeout: 0
          trx_is_read_only: 0
trx_autocommit_non_locking: 0
1 row in set (0.00 sec)

可以看到当前获取到了一个事务的信息,对应的 trx_mysql_thread_id 表示这个事务所在的连接线程,当前为 4.

使用 start transaction WITH CONSISTENT SNAPSHOT 创建一个事务

使用 BEGIN 并不会立即创建一个事务,此时如果要验证事务我们可以使用START TRANSACTION WITH CONSISTENT SNAPSHOT强制启动一个事务。

mysql> START TRANSACTION WITH CONSISTENT SNAPSHOT;
Query OK, 0 rows affected (0.00 sec)

查看 binlog 日志

如果 MySQL开启了 binlog,可以使用 MySQL服务器携带的 mysqlbinlog 命令查看binlog日志:

# 后面指定 binlog 文件
mysqlbinlog ./mysql-bin.000001
# 查看指定 binlong 文件
# -vv 表示详细输出 v 越多输出内容越详细
# --start-position 表示输出内容的起始位置
mysqlbinlog -vv data/master.000001 --start-position=8900;

在 mysql 命令行中查看:

-- 查看第一个binlog文件内容
show binlog events;
-- 查看指定 binlog 文件的内容
show binlog events in 'mysql-bin.000002';
-- 查看当前正在写入的 binlog 文件
show master status \G
-- 获取 binlog 文件列表
show binary logs;

查看存储引擎 InnoDB 状态

可以使用命令查看存储引擎状态,如果数据库最近发生过死锁,该命令可以看到发生死锁时系统的状态。

SHOW ENGINE InnoDB STATUS\G

示例

mysql> SHOW ENGINE InnoDB STATUS\G
*************************** 1. row ***************************
  Type: InnoDB -- 引擎类型
  Name:
Status:
=====================================
2022-10-24 10:33:35 0x428c INNODB MONITOR OUTPUT -- 存储引擎监视器输出
=====================================
Per second averages calculated from the last 52 seconds
-----------------
BACKGROUND THREAD    -- 后台线程
-----------------
srv_master_thread loops: 16 srv_active, 0 srv_shutdown, 219547 srv_idle   -- 伺服线程循环状态
srv_master_thread log flush and writes: 219563   -- 伺服线程日志刷盘状态
----------
SEMAPHORES -- 信号量
----------
OS WAIT ARRAY INFO: reservation count 41     -- 系统等待队列
OS WAIT ARRAY INFO: signal count 41
RW-shared spins 0, rounds 40, OS waits 20
RW-excl spins 0, rounds 54, OS waits 1
RW-sx spins 0, rounds 0, OS waits 0
Spin rounds per wait: 40.00 RW-shared, 54.00 RW-excl, 0.00 RW-sx
------------
TRANSACTIONS
------------
Trx id counter 6276938
Purge done for trxs n:o < 6276935 undo n:o < 0 state: running but idle
History list length 0
LIST OF TRANSACTIONS FOR EACH SESSION:
---TRANSACTION 283738590640792, not started
0 lock struct(s), heap size 1136, 0 row lock(s)
---TRANSACTION 6276935, ACTIVE 903 sec
2 lock struct(s), heap size 1136, 7 row lock(s)
MySQL thread id 5, OS thread handle 5260, query id 87 localhost 127.0.0.1 root
--------
FILE I/O
--------
I/O thread 0 state: wait Windows aio (insert buffer thread)
I/O thread 1 state: wait Windows aio (log thread)
I/O thread 2 state: wait Windows aio (read thread)
I/O thread 3 state: wait Windows aio (read thread)
I/O thread 4 state: wait Windows aio (read thread)
I/O thread 5 state: wait Windows aio (read thread)
I/O thread 6 state: wait Windows aio (write thread)
I/O thread 7 state: wait Windows aio (write thread)
I/O thread 8 state: wait Windows aio (write thread)
I/O thread 9 state: wait Windows aio (write thread)
Pending normal aio reads: [0, 0, 0, 0] , aio writes: [0, 0, 0, 0] ,
 ibuf aio reads:, log i/o_s:, sync i/o_s:
Pending flushes (fsync) log: 0; buffer pool: 0
612 OS file reads, 359 OS file writes, 152 OS fsyncs
0.00 reads/s, 0 avg bytes/read, 0.00 writes/s, 0.00 fsyncs/s
-------------------------------------
INSERT BUFFER AND ADAPTIVE HASH INDEX
-------------------------------------
Ibuf: size 1, free list len 0, seg size 2, 0 merges
merged operations:
 insert 0, delete mark 0, delete 0
discarded operations:
 insert 0, delete mark 0, delete 0
Hash table size 34679, node heap has 0 buffer(s)
Hash table size 34679, node heap has 0 buffer(s)
Hash table size 34679, node heap has 0 buffer(s)
Hash table size 34679, node heap has 0 buffer(s)
Hash table size 34679, node heap has 0 buffer(s)
Hash table size 34679, node heap has 0 buffer(s)
Hash table size 34679, node heap has 0 buffer(s)
Hash table size 34679, node heap has 0 buffer(s)
0.00 hash searches/s, 0.00 non-hash searches/s
---
LOG
---
Log sequence number 3153058970
Log flushed up to   3153058970
Pages flushed up to 3153058970
Last checkpoint at  3153058961
0 pending log flushes, 0 pending chkp writes
99 log i/o_s done, 0.00 log i/o_s/second
----------------------
BUFFER POOL AND MEMORY
----------------------
Total large memory allocated 137297920
Dictionary memory allocated 191079
Buffer pool size   8192    -- 内存中缓存池大小 单位为页
Free buffers       7566    -- 空闲缓存数量
Database pages     626     -- 已缓存数据页数量 8192 = 7566 + 626
Old database pages 248
Modified db pages  0       -- 已修改脏页数量
Pending reads      0
Pending writes: LRU 0, flush list 0, single page 0
Pages made young 0, not young 0
0.00 youngs/s, 0.00 non-youngs/s
Pages read 573, created 53, written 228
0.00 reads/s, 0.00 creates/s, 0.00 writes/s
No buffer pool page gets since the last printout
Pages read ahead 0.00/s, evicted without access 0.00/s, Random read ahead 0.00/s
LRU len: 626, unzip_LRU len: 0
I/O sum[0]:cur[0], unzip sum[0]:cur[0]
--------------
ROW OPERATIONS
--------------
0 queries inside InnoDB, 0 queries in queue
0 read views open inside InnoDB
Process ID=5952, Main thread ID=8412, state: sleeping
Number of rows inserted 27, updated 0, deleted 0, read 203
0.00 inserts/s, 0.00 updates/s, 0.00 deletes/s, 0.00 reads/s
----------------------------
END OF INNODB MONITOR OUTPUT
============================

1 row in set (0.00 sec)
  • 2
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值