我们可以使用
status
获取当前连接的状态:
mysql>status-- 查看当前连接状态--------------
D:\mysql\bin\mysql.exe Ver 14.14 Distrib 5.7.36,for Win64 (x86_64)-- 当前连接使用的客户端
Connection id: 3-- 当前连接id(线程id)Currentdatabase: test -- 当前连接数据库Currentuser: root@localhost-- 当前连接用户
SSL: Cipher inuseis ECDHE-RSA-AES128-GCM-SHA256
Usingdelimiter: ;-- 当前连接分隔符
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: 1Opentables: 114 Queries per second avg: 0.004--------------
Connection id 表示当前会话的连接线程id,当前线程id为 3
除此之外,还可以使用
show processlist
查看当前MySQL 服务器上存在哪些连接:
mysql>show processlist\G
***************************1.row***************************
Id: 3-- 当前线程idUser: root -- 当前线程登录用户
Host: localhost:65447-- 线程登录主机
db: test -- 线程使用数据库
Command: Query -- 当前线程执行命令类型Time: 0
State: starting-- 当前连接状态
Info: show processlist -- 当前连接执行SQL***************************2.row***************************
Id: 4User: root
Host: localhost:65431
db: test
Command: Sleep
Time: 405
State:
Info: NULL2rowsinset(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-1016: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: REPEATABLEREAD-- 当前事务隔离级别
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: 01rowinset(0.00 sec)
可以看到当前获取到了一个事务的信息,对应的
trx_mysql_thread_id
表示这个事务所在的连接线程,当前为 4.
使用
start transaction WITH CONSISTENT SNAPSHOT
创建一个事务
使用
BEGIN
并不会立即创建一个事务,此时如果要验证事务我们可以使用
START TRANSACTION WITH CONSISTENT SNAPSHOT
强制启动一个事务。
mysql>STARTTRANSACTIONWITHCONSISTENTSNAPSHOT;
Query OK,0rows 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 文件列表showbinary logs;
查看存储引擎 InnoDB 状态
可以使用命令查看存储引擎状态,如果数据库最近发生过死锁,该命令可以看到发生死锁时系统的状态。
SHOWENGINEInnoDBSTATUS\G
示例
mysql>SHOWENGINEInnoDBSTATUS\G
***************************1.row***************************Type: InnoDB-- 引擎类型
Name:
Status:
=====================================2022-10-2410:33:350x428cINNODB MONITOR OUTPUT -- 存储引擎监视器输出=====================================
Per second averages calculated from the last52 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 6276938Purge done for trxs n:o <6276935 undo n:o <0 state: running but idle
History list length 0
LIST OFTRANSACTIONSFOR EACH SESSION:
---TRANSACTION 283738590640792, not started0lock struct(s), heap size 1136,0rowlock(s)---TRANSACTION 6276935, ACTIVE 903 sec2lock struct(s), heap size 1136,7rowlock(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: 0612 OS filereads,359 OS file writes,152 OS fsyncs
0.00reads/s,0 avg bytes/read,0.00 writes/s,0.00 fsyncs/s
-------------------------------------INSERT BUFFER AND ADAPTIVE HASHINDEX-------------------------------------
Ibuf: size 1, free list len 0, seg size 2,0 merges
merged operations:
insert0,delete mark 0,delete0
discarded operations:
insert0,delete mark 0,delete0Hashtable size 34679, node heap has 0 buffer(s)Hashtable size 34679, node heap has 0 buffer(s)Hashtable size 34679, node heap has 0 buffer(s)Hashtable size 34679, node heap has 0 buffer(s)Hashtable size 34679, node heap has 0 buffer(s)Hashtable size 34679, node heap has 0 buffer(s)Hashtable size 34679, node heap has 0 buffer(s)Hashtable size 34679, node heap has 0 buffer(s)0.00hash searches/s,0.00 non-hash searches/s
---
LOG
---
Log sequence number 3153058970
Log flushed up to3153058970
Pages flushed up to3153058970Lastcheckpoint at 31530589610 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 reads0
Pending writes: LRU 0, flush list 0, single page 0
Pages made young 0,not young 00.00 youngs/s,0.00 non-youngs/s
Pages read573, created 53, written 2280.00reads/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
0read views open inside InnoDB
Process ID=5952, Main thread ID=8412, state: sleeping
Number ofrows inserted 27, updated 0, deleted 0,read2030.00 inserts/s,0.00 updates/s,0.00 deletes/s,0.00reads/s
----------------------------ENDOFINNODB MONITOR OUTPUT
============================1rowinset(0.00 sec)
版权归原作者 ghimi 所有, 如有侵权,请联系我们删除。