文章目录
- 1、如何查看 MySQL 数据库的连接数
- 2、如何查看 MySQL 数据库当前运行的事务与锁
- 3、MySQL 中数据表的监控
- 3.1 查看数据了中当前打开了哪些表:show open tables 。还可以通过 show open tables where In_use > 0 过滤当前已经被锁定的表。
- 3.2 查看数据库中表的状态:show status like "%tables%",需要注意的是,table_locks_waited 指的是不能立即获取表级锁而需要等待的次数。如果等待的次数非常大,则说明可能存在锁争抢的情况;如果是频繁的出现锁争抢,则对应应用程序的并发性能影响很大。
- 3.3 查看数据了中锁的信息:show status like "%lock%",如下所示:
- 3.4 查看数据库中被扫描的情况:show global status like "handler_read%",如下所示:
- 4、性能测试时 MySQL 中其他的常用监控
1、如何查看 MySQL 数据库的连接数
连接数是指用户已经创建多少个连接,也就是 MySQL 中通过执行 show processlist 命令输出数据库中运行着的线程个数的详情,如:
mysql> show processlist;
+----+-----------------+-----------------+-------+---------+-------+------------
------------+------------------+
| Id | User | Host | db | Command | Time | State
| Info |
+----+-----------------+-----------------+-------+---------+-------+------------
------------+------------------+
| 4 | event_scheduler | localhost | NULL | Daemon | 13395 | Waiting on
empty queue | NULL |
| 16 | root | localhost:56236 | NULL | Sleep | 808 |
| NULL |
| 17 | root | localhost:56237 | mysql | Sleep | 806 |
| NULL |
| 18 | root | localhost:56377 | NULL | Sleep | 439 |
| NULL |
| 21 | root | localhost:56400 | mysql | Sleep | 338 |
| NULL |
| 23 | root | localhost:56569 | NULL | Query | 0 | starting
| show processlist |
+----+-----------------+-----------------+-------+---------+-------+------------
------------+------------------+
6 rows in set (0.00 sec)
show processlist 默认情况下只显示100条记录的详情,如果需要显示超过100条的所有记录,可以通过执行 show full processlist 命令来查看,如:
mysql> show full processlist;
+----+-----------------+-----------------+-------+---------+-------+------------
------------+-----------------------+
| Id | User | Host | db | Command | Time | State
| Info |
+----+-----------------+-----------------+-------+---------+-------+------------
------------+-----------------------+
| 4 | event_scheduler | localhost | NULL | Daemon | 13521 | Waiting on
empty queue | NULL |
| 16 | root | localhost:56236 | NULL | Sleep | 934 |
| NULL |
| 17 | root | localhost:56237 | mysql | Sleep | 932 |
| NULL |
| 18 | root | localhost:56377 | NULL | Sleep | 565 |
| NULL |
| 21 | root | localhost:56400 | mysql | Sleep | 464 |
| NULL |
| 23 | root | localhost:56569 | NULL | Query | 0 | starting
| show full processlist |
+----+-----------------+-----------------+-------+---------+-------+------------
------------+-----------------------+
6 rows in set (0.00 sec)
show variable like “max_connections” 命令可以查询数据库中允许支持的最大连接数,如:
mysql> show variables like "max_connections";
+-----------------+-------+
| Variable_name | Value |
+-----------------+-------+
| max_connections | 151 |
+-----------------+-------+
1 row in set, 1 warning (0.01 sec)
show global status like “max_used_connections” 命令可以查询当前已经使用过的最大连接数,如:
mysql> show global status like "max_used_connections";
+----------------------+-------+
| Variable_name | Value |
+----------------------+-------+
| Max_used_connections | 5 |
+----------------------+-------+
1 row in set (0.01 sec)
2、如何查看 MySQL 数据库当前运行的事务与锁
事务是对数据库执行一种带有原子性、一致性、隔离性、持久性的数据操作。在 MySQL中如果需要使用事务,那么数据库存储时必须选用 MySQL 的 innodb 引擎,使用 innodb 引擎后,在 MySQL 系统数据库 information_schema 的 innodb_trx 表中记录 了数据库当前正在运行的事务。
innodb_trx 表中包含的常用字段说明。
| 字段 | 说明 |
|---|---|
| trx_id | 事务ID |
| trx_state | 事务的状态。一般包括 running、lock wait、rolling back 和 committing 等几种不同的状态 |
| trx_started | 事务开始运行的时间 |
| trx_requested_lock_id | 事务需要等待的、但已经被别的程序锁定的资源id。一般可以和innbdb_locks 表关联在一起,获取更多的被锁定的资源的详细信息 |
| trx_wait_started | 事务开始等待时间 |
| trx_mysql_thread_id | 事务对应的MySQL线程id |
| trx_query | 事务正在执行的SQL语句 |
MySQL 系统数据库 information_shcema 的 innodb_lock_waits 表中记录了 innodb 数据库引擎当前运行的数据库事务等待锁的情况。innod表中包含的常用字段说明如下:
| 字段 | 说明 |
|---|---|
| 在数据库出现死锁时,经常需要通过查询 innodb_trx、innodb_locks 和 innodb_lock_waits 这三张表来找出在执行什么事务操作时导致了死锁,例如执行如下 SQL 语句可以列出数据库中所有事务的等待和锁定情况。 |
3、MySQL 中数据表的监控
3.1 查看数据了中当前打开了哪些表:show open tables 。还可以通过 show open tables where In_use > 0 过滤当前已经被锁定的表。
mysql> show open tables;
+--------------------+-------------------------------------+--------+-----------
--+
| Database | Table | In_use | Name_locke
d |
+--------------------+-------------------------------------+--------+-----------
--+
| mysql | slave_master_info | 0 |
0 |
| mysql | columns | 0 |
0 |
| mysql | foreign_keys | 0 |
0 |
| mysql | component | 0 |
0 |
| mysql | column_type_elements | 0 |
0 |
| mysql | server_cost | 0 |
0 |
| performance_schema | memory_summary_global_by_event_name | 0 |
0 |
| mysql | plugin | 0 |
0 |
| mysql | index_column_usage | 0 |
0 |
| mysql | foreign_key_column_usage | 0 |
0 |
| mysql | index_partitions | 0 |
0 |
| mysql | view_table_usage | 0 |
0 |
| mysql | indexes | 0 |
0 |
| performance_schema | table_io_waits_summary_by_table | 0 |
0 |
| mysql | schemata | 0 |
0 |
| mysql | collations | 0 |
0 |
| mysql | table_partition_values | 0 |
0 |
| mysql | table_partitions | 0 |
0 |
| mysql | tables | 0 |
0 |
| information_schema | ROUTINES | 0 |
0 |
| mysql | triggers | 0 |
0 |
| mysql | role_edges | 0 |
0 |
| mysql | column_statistics | 0 |
0 |
| mysql | view_routine_usage | 0 |
0 |
| mysql | func | 0 |
0 |
| mysql | tablespace_files | 0 |
0 |
| mysql | tablespaces | 0 |
0 |
| mysql | character_sets | 0 |
0 |
| performance_schema | data_lock_waits | 0 |
0 |
| performance_schema | data_locks | 0 |
0 |
| performance_schema | setup_instruments | 0 |
0 |
| mysql | parameter_type_elements | 0 |
0 |
| mysql | parameters | 0 |
0 |
| mysql | events | 0 |
0 |
| mysql | time_zone_transition_type | 0 |
0 |
| mysql | routines | 0 |
0 |
| mysql | default_roles | 0 |
0 |
| sys | sys_config | 0 |
0 |
| information_schema | SCHEMATA | 0 |
0 |
| mysql | catalogs | 0 |
0 |
| sys | x$ps_schema_table_statistics_io | 0 |
0 |
| performance_schema | file_summary_by_instance | 0 |
0 |
| mysql | proxies_priv | 0 |
0 |
| performance_schema | global_status | 0 |
0 |
| mysql | resource_groups | 0 |
0 |
| mysql | gtid_executed | 0 |
0 |
| mysql | engine_cost | 0 |
0 |
| mysql | user | 0 |
0 |
| mysql | db | 0 |
0 |
| mysql | tables_priv | 0 |
0 |
| mysql | columns_priv | 0 |
0 |
| mysql | procs_priv | 0 |
0 |
| performance_schema | session_status | 0 |
0 |
| mysql | global_grants | 0 |
0 |
| mysql | password_history | 0 |
0 |
| mysql | servers | 0 |
0 |
| mysql | time_zone_transition | 0 |
0 |
| mysql | time_zone_leap_second | 0 |
0 |
| mysql | table_stats | 0 |
0 |
| mysql | time_zone | 0 |
0 |
| mysql | time_zone_name | 0 |
0 |
| mysql | slave_relay_log_info | 0 |
0 |
| information_schema | TABLES | 0 |
0 |
| mysql | slave_worker_info | 0 |
0 |
| performance_schema | session_variables | 0 |
0 |
+--------------------+-------------------------------------+--------+-----------
--+
65 rows in set (0.00 sec)
mysql> show open tables where In_use > 0;
Empty set (0.00 sec)
3.2 查看数据库中表的状态:show status like “%tables%”,需要注意的是,table_locks_waited 指的是不能立即获取表级锁而需要等待的次数。如果等待的次数非常大,则说明可能存在锁争抢的情况;如果是频繁的出现锁争抢,则对应应用程序的并发性能影响很大。
mysql> show status like "%tables%";
+-------------------------+-------+
| Variable_name | Value |
+-------------------------+-------+
| Com_alter_tablespace | 0 |
| Com_lock_tables | 0 |
| Com_show_open_tables | 4 |
| Com_show_tables | 0 |
| Com_unlock_tables | 0 |
| Created_tmp_disk_tables | 0 |
| Created_tmp_tables | 5 |
| Open_tables | 184 |
| Opened_tables | 1 |
| Slave_open_temp_tables | 0 |
+-------------------------+-------+
10 rows in set (0.00 sec)
3.3 查看数据了中锁的信息:show status like “%lock%”,如下所示:
mysql> show status like "%lock%";
+------------------------------------------+-------+
| Variable_name | Value |
+------------------------------------------+-------+
| Com_lock_instance | 0 |
| Com_lock_tables | 0 |
| Com_unlock_instance | 0 |
| Com_unlock_tables | 0 |
| Handler_external_lock | 2 |
| Innodb_row_lock_current_waits | 0 |
| Innodb_row_lock_time | 0 |
| Innodb_row_lock_time_avg | 0 |
| Innodb_row_lock_time_max | 0 |
| Innodb_row_lock_waits | 0 |
| Key_blocks_not_flushed | 0 |
| Key_blocks_unused | 6698 |
| Key_blocks_used | 0 |
| Locked_connects | 0 |
| Performance_schema_locker_lost | 0 |
| Performance_schema_metadata_lock_lost | 0 |
| Performance_schema_rwlock_classes_lost | 0 |
| Performance_schema_rwlock_instances_lost | 0 |
| Performance_schema_table_lock_stat_lost | 0 |
| Table_locks_immediate | 9 |
| Table_locks_waited | 0 |
+------------------------------------------+-------+
21 rows in set (0.01 sec)
3.4 查看数据库中被扫描的情况:show global status like “handler_read%”,如下所示:
mysql> show global status like "handler_read%";
+-----------------------+-------+
| Variable_name | Value |
+-----------------------+-------+
| Handler_read_first | 63 |
| Handler_read_key | 2408 |
| Handler_read_last | 0 |
| Handler_read_next | 4888 |
| Handler_read_prev | 0 |
| Handler_read_rnd | 165 |
| Handler_read_rnd_next | 9023 |
+-----------------------+-------+
7 rows in set (0.00 sec)
数据库查询的结果数据:
| 查询结果项 | 说明 |
|---|---|
4、性能测试时 MySQL 中其他的常用监控
4.1 查看每秒事务的提交次数:show global status like “com_commit”,如下所示:
mysql> show global status like "com_commit";
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| Com_commit | 0 |
+---------------+-------+
1 row in set (0.00 sec)
4.2 查看每秒事务的回滚数:show global status like “com_rollback”,如下所示:
mysql> show global status like "com_rollback";
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| Com_rollback | 0 |
+---------------+-------+
1 row in set (0.01 sec)
4.3 查看线程的运行情况:show global status like “threads_%”,如下所示:
mysql> show global status like "threads_%";
+-------------------+-------+
| Variable_name | Value |
+-------------------+-------+
| Threads_cached | 0 |
| Threads_connected | 5 |
| Threads_created | 5 |
| Threads_running | 2 |
+-------------------+-------+
4 rows in set (0.00 sec)
查询结果的说明如表所示:
| 查询结果项 | 说明 |
|---|---|
| 4.4 查看数据库建立过的连接总数(包括连接中以及断开的连接):show global status like “Connections”,如下所示: |
mysql> show global status like "Connections";
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| Connections | 24 |
+---------------+-------+
1 row in set (0.00 sec)
4.5 查看 innodb 引擎缓存命中情况:show global status like “innodb_buffer_pool_read%”,如下所示:
mysql> show global status like "innodb_buffer_pool_read%";
+---------------------------------------+-------+
| Variable_name | Value |
+---------------------------------------+-------+
| Innodb_buffer_pool_read_ahead_rnd | 0 |
| Innodb_buffer_pool_read_ahead | 0 |
| Innodb_buffer_pool_read_ahead_evicted | 0 |
| Innodb_buffer_pool_read_requests | 29082 |
| Innodb_buffer_pool_reads | 1103 |
+---------------------------------------+-------+
5 rows in set (0.00 sec)
5.6 查看 join 操作时全表扫描的次数:show global status like “select_full_join”,如下所示。该值一般可以表示 SQL 语句中的 join 操作没有使用索引次数,如果值非常大,那可能是 SQL 语句中的 join 操作存在性能问题。
mysql> show global status like "select_full_join";
+------------------+-------+
| Variable_name | Value |
+------------------+-------+
| Select_full_join | 1 |
+------------------+-------+
1 row in set (0.00 sec)
5.7查看 SQL 中排序使用情况:show global status like “sort%”,如下所示:
mysql> show global status like "sort%";
+-------------------+-------+
| Variable_name | Value |
+-------------------+-------+
| Sort_merge_passes | 0 |
| Sort_range | 0 |
| Sort_rows | 165 |
| Sort_scan | 11 |
+-------------------+-------+
4 rows in set (0.00 sec)
5.8 查看 SQL 查询缓存的命中情况:show global status like “qcache%”,如下所示:
5.9 如果需要查看数据库查询缓存的位置,可以通过 :show variables like “query_cache%”,如下所示:
5.10 如果需要查询数据库更多的状态信息,可以通过show global status 进行查
询。
MySQL 数据库中大部分的运行状态都可以通过 show status 和 show global status 来查看。二者的区别在于前者是查询当前运行状态,后者是查询全局的运行状态,也就是数据库开始运行到现在的为止的状态。
本文介绍了MySQL数据库的性能监控方法,包括查看连接数、运行的事务与锁、数据表状态、性能测试时的常用监控指标,如事务提交、回滚、线程运行、缓存命中、全表扫描和SQL排序等,帮助管理员识别并解决潜在的性能问题。

895

被折叠的 条评论
为什么被折叠?



