在上一期《日志记录等混杂表|全方位认识 mysql 系统库》中,我们详细介绍了mysql系统库中的日志记录表,本期我们将为大家带来系列第九篇《应用示例荟萃|全方位认识 mysql 系统库》,也是"全方位认识 mysql 系统库"的最后一篇,下面请跟随我们一起开始 mysql 系统库的系统学习之旅吧
root@localhost : (none) 12:51:46> use mysql
Database changed
root@localhost : mysql 12:52:58> grant select on *.* to test_global@'%' identified by 'test';
Query OK, 0 rows affected, 1 warning (0.00 sec)
root@localhost : mysql 12:53:35> select * from user where user='test_global'\G
*************************** 1. row ***************************
Host: %
User: test_global
Select_priv: Y # 从这里可以看到,mysql.user表中只有select权限标记列为Y,其余的权限列为N
Insert_priv: N
Update_priv: N
Delete_priv: N
Create_priv: N
Drop_priv: N
Reload_priv: N
Shutdown_priv: N
Process_priv: N
File_priv: N
Grant_priv: N
References_priv: N
Index_priv: N
Alter_priv: N
Show_db_priv: N
Super_priv: N
Create_tmp_table_priv: N
Lock_tables_priv: N
Execute_priv: N
Repl_slave_priv: N
Repl_client_priv: N
Create_view_priv: N
Show_view_priv: N
Create_routine_priv: N
Alter_routine_priv: N
Create_user_priv: N
Event_priv: N
Trigger_priv: N
Create_tablespace_priv: N
ssl_type:
ssl_cipher:
x509_issuer:
x509_subject:
max_questions: 0
max_updates: 0
max_connections: 0
max_user_connections: 0
plugin: mysql_native_password
authentication_string: *94BDCEBE19083CE2A1F959FD02F964C7AF4CFC29
password_expired: N
password_last_changed: 2018-08-19 12:53:35
password_lifetime: NULL
account_locked: N
1 row in set (0.00 sec)
root@localhost : mysql 12:53:58> select * from db where user='test_global'\G
Empty set (0.02 sec)
root@localhost : mysql 12:54:13> select * from tables_priv where user='test_global'\G
Empty set (0.00 sec)
root@localhost : mysql 12:54:22> select * from columns_priv where user='test_global'\G
Empty set (0.00 sec)
1.2. 查看库级别权限
root@localhost : mysql 12:55:30> create database test_grant;
Query OK, 1 row affected (0.01 sec)
root@localhost : mysql 12:55:42> grant select on test_grant.* to test_db@'%' identified by 'test';
Query OK, 0 rows affected, 1 warning (0.00 sec)
root@localhost : mysql 12:56:09> select * from user where user='test_db'\G
*************************** 1. row ***************************
Host: %
User: test_db
Select_priv: N
Insert_priv: N
Update_priv: N
Delete_priv: N
Create_priv: N
Drop_priv: N
Reload_priv: N
Shutdown_priv: N
Process_priv: N
File_priv: N
Grant_priv: N
References_priv: N
Index_priv: N
Alter_priv: N
Show_db_priv: N
Super_priv: N
Create_tmp_table_priv: N
Lock_tables_priv: N
Execute_priv: N
Repl_slave_priv: N
Repl_client_priv: N
Create_view_priv: N
Show_view_priv: N
Create_routine_priv: N
Alter_routine_priv: N
Create_user_priv: N
Event_priv: N
Trigger_priv: N
Create_tablespace_priv: N
ssl_type:
ssl_cipher:
x509_issuer:
x509_subject:
max_questions: 0
max_updates: 0
max_connections: 0
max_user_connections: 0
plugin: mysql_native_password
authentication_string: *94BDCEBE19083CE2A1F959FD02F964C7AF4CFC29
password_expired: N
password_last_changed: 2018-08-19 12:56:09
password_lifetime: NULL
account_locked: N
1 row in set (0.00 sec)
root@localhost : mysql 12:56:29> select * from db where user='test_db'\G
*************************** 1. row ***************************
Host: %
Db: test_grant
User: test_db
Select_priv: Y # 库级别select权限列为Y,其余权限标记列为N
Insert_priv: N
Update_priv: N
Delete_priv: N
Create_priv: N
Drop_priv: N
Grant_priv: N
References_priv: N
Index_priv: N
Alter_priv: N
Create_tmp_table_priv: N
Lock_tables_priv: N
Create_view_priv: N
Show_view_priv: N
Create_routine_priv: N
Alter_routine_priv: N
Execute_priv: N
Event_priv: N
Trigger_priv: N
1 row in set (0.00 sec)
root@localhost : mysql 12:56:41> select * from tables_priv where user='test_db'\G
Empty set (0.00 sec)
root@localhost : mysql 12:56:53> select * from columns_priv where user='test_db'\G
Empty set (0.00 sec)
root@localhost : mysql 12:57:00> use test_grant
Database changed
root@localhost : test_grant 12:57:27> create table test_table_grant(id int);
Query OK, 0 rows affected (0.03 sec)
root@localhost : test_grant 12:57:52> grant select on test_grant.test_table_grant to test_table@'%' identified by 'test';
Query OK, 0 rows affected, 1 warning (0.01 sec)
root@localhost : test_grant 12:59:21> select * from mysql.user where user='test_table'\G
*************************** 1. row ***************************
Host: %
User: test_table
Select_priv: N
Insert_priv: N
Update_priv: N
Delete_priv: N
Create_priv: N
Drop_priv: N
Reload_priv: N
Shutdown_priv: N
Process_priv: N
File_priv: N
Grant_priv: N
References_priv: N
Index_priv: N
Alter_priv: N
Show_db_priv: N
Super_priv: N
Create_tmp_table_priv: N
Lock_tables_priv: N
Execute_priv: N
Repl_slave_priv: N
Repl_client_priv: N
Create_view_priv: N
Show_view_priv: N
Create_routine_priv: N
Alter_routine_priv: N
Create_user_priv: N
Event_priv: N
Trigger_priv: N
Create_tablespace_priv: N
ssl_type:
ssl_cipher:
x509_issuer:
x509_subject:
max_questions: 0
max_updates: 0
max_connections: 0
max_user_connections: 0
plugin: mysql_native_password
authentication_string: *94BDCEBE19083CE2A1F959FD02F964C7AF4CFC29
password_expired: N
password_last_changed: 2018-08-19 12:58:13
password_lifetime: NULL
account_locked: N
1 row in set (0.00 sec)
root@localhost : test_grant 12:59:52> select * from mysql.db where user='test_table'\G
Empty set (0.00 sec)
root@localhost : test_grant 01:00:02> select * from mysql.tables_priv where user='test_table'\G
*************************** 1. row ***************************
Host: %
Db: test_grant
User: test_table
Table_name: test_table_grant
Grantor: root@localhost
Timestamp: 0000-00-00 00:00:00
Table_priv: Select # Table_priv列为select表示用户test_table对test_table_grant表具有表级别select权限
Column_priv: # Column_priv列为空表示用户test_table对test_table_grant表不具有列级别权限
1 rows in set (0.00 sec)
root@localhost : test_grant 01:00:17> select * from mysql.columns_priv where user='test_table'\G
Empty set (0.00 sec)
1.4. 查看列级别权限
root@localhost : test_grant 01:00:24> grant select(id) on test_grant.test_table_grant to test_column@'%' identified by 'test';
Query OK, 0 rows affected, 1 warning (0.02 sec)
root@localhost : test_grant 01:02:38> select * from user where user='test_column'\G
ERROR 1146 (42S02): Table 'test_grant.user' doesn't exist
root@localhost : test_grant 01:02:52> select * from mysql.user where user='test_column'\G
*************************** 1. row ***************************
Host: %
User: test_column
Select_priv: N
Insert_priv: N
Update_priv: N
Delete_priv: N
Create_priv: N
Drop_priv: N
Reload_priv: N
Shutdown_priv: N
Process_priv: N
File_priv: N
Grant_priv: N
References_priv: N
Index_priv: N
Alter_priv: N
Show_db_priv: N
Super_priv: N
Create_tmp_table_priv: N
Lock_tables_priv: N
Execute_priv: N
Repl_slave_priv: N
Repl_client_priv: N
Create_view_priv: N
Show_view_priv: N
Create_routine_priv: N
Alter_routine_priv: N
Create_user_priv: N
Event_priv: N
Trigger_priv: N
Create_tablespace_priv: N
ssl_type:
ssl_cipher:
x509_issuer:
x509_subject:
max_questions: 0
max_updates: 0
max_connections: 0
max_user_connections: 0
plugin: mysql_native_password
authentication_string: *94BDCEBE19083CE2A1F959FD02F964C7AF4CFC29
password_expired: N
password_last_changed: 2018-08-19 13:02:38
password_lifetime: NULL
account_locked: N
1 row in set (0.00 sec)
root@localhost : test_grant 01:03:02> select * from mysql.db where user='test_column'\G
Empty set (0.00 sec)
root@localhost : test_grant 01:03:08> select * from mysql.tables_priv where user='test_column'\G
*************************** 1. row ***************************
Host: %
Db: test_grant
User: test_column
Table_name: test_table_grant
Grantor: root@localhost
Timestamp: 0000-00-00 00:00:00
Table_priv: # Table_priv列为空表示test_column用户对test_table_grant表不具有表级别权限
Column_priv: Select # Column_priv列为Select表示用户test_column对test_table_grant表具有列级别select权限
1 row in set (0.00 sec)
# 通过对mysql.tables_priv表的查询发现test_column用户对test_table_grant表具有列级别的select权限,由于列级别权限是针对具体的列授予的,这些信息需要通过mysql.columns_priv来查询,如下
root@localhost : test_grant 01:03:23> select * from mysql.columns_priv where user='test_column'\G
*************************** 1. row ***************************
Host: %
Db: test_grant
User: test_column
Table_name: test_table_grant
Column_name: id #对id列有列级别权限
Timestamp: 0000-00-00 00:00:00
Column_priv: Select # 列级别权限为select
1 row in set (0.00 sec)
root@localhost : (none) 01:41:09> use test
Database changed
root@localhost : test 01:41:13> create table test_stat(id int not null primary key auto_increment);
Query OK, 0 rows affected (0.01 sec)
root@localhost : test 01:44:16> select * from mysql.innodb_table_stats where table_name='test_stat'\G
*************************** 1. row ***************************
database_name: test
table_name: test_stat
last_update: 2018-08-19 13:41:46
n_rows: 0
clustered_index_size: 1
sum_of_other_index_sizes: 0
1 row in set (0.00 sec)
root@localhost : test 01:44:32> select * from mysql.innodb_index_stats where table_name='test_stat';
+---------------+------------+------------+---------------------+--------------+------------+-------------+-----------------------------------+
| database_name | table_name | index_name | last_update | stat_name | stat_value | sample_size | stat_description |
+---------------+------------+------------+---------------------+--------------+------------+-------------+-----------------------------------+
| test | test_stat | PRIMARY | 2018-08-19 13:41:46 | n_diff_pfx01 | 0 | 1 | id |
| test | test_stat | PRIMARY | 2018-08-19 13:41:46 | n_leaf_pages | 1 | NULL | Number of leaf pages in the index |
| test | test_stat | PRIMARY | 2018-08-19 13:41:46 | size | 1 | NULL | Number of pages in the index |
+---------------+------------+------------+---------------------+--------------+------------+-------------+-----------------------------------+
3 rows in set (0.04 sec)
root@localhost : test 01:47:55> alter table test_stat add column test1 int,add unique index i_test1(test1);
Query OK, 0 rows affected (0.03 sec)
Records: 0 Duplicates: 0 Warnings: 0
root@localhost : test 02:03:18> alter table test_stat add column test2 int,add index i_test2(test2);
Query OK, 0 rows affected (0.03 sec)
Records: 0 Duplicates: 0 Warnings: 0
root@localhost : test 02:03:33> select * from mysql.innodb_index_stats where table_name='test_stat';
+---------------+------------+------------+---------------------+--------------+------------+-------------+-----------------------------------+
| database_name | table_name | index_name | last_update | stat_name | stat_value | sample_size | stat_description |
+---------------+------------+------------+---------------------+--------------+------------+-------------+-----------------------------------+
| test | test_stat | PRIMARY | 2018-08-19 14:03:33 | n_diff_pfx01 | 0 | 1 | id |
| test | test_stat | PRIMARY | 2018-08-19 14:03:33 | n_leaf_pages | 1 | NULL | Number of leaf pages in the index |
| test | test_stat | PRIMARY | 2018-08-19 14:03:33 | size | 1 | NULL | Number of pages in the index |
| test | test_stat | i_test1 | 2018-08-19 14:03:33 | n_diff_pfx01 | 0 | 1 | test1 |
| test | test_stat | i_test1 | 2018-08-19 14:03:33 | n_leaf_pages | 1 | NULL | Number of leaf pages in the index |
| test | test_stat | i_test1 | 2018-08-19 14:03:33 | size | 1 | NULL | Number of pages in the index |
| test | test_stat | i_test2 | 2018-08-19 14:03:33 | n_diff_pfx01 | 0 | 1 | test2 |
| test | test_stat | i_test2 | 2018-08-19 14:03:33 | n_diff_pfx02 | 0 | 1 | test2,id |
| test | test_stat | i_test2 | 2018-08-19 14:03:33 | n_leaf_pages | 1 | NULL | Number of leaf pages in the index |
| test | test_stat | i_test2 | 2018-08-19 14:03:33 | size | 1 | NULL | Number of pages in the index |
+---------------+------------+------------+---------------------+--------------+------------+-------------+-----------------------------------+
10 rows in set (0.00 sec)
* size:当stat_name为size值时,stat_value列值表示索引中的总页数量
* n_leaf_pages:当stat_name为n_leaf_pages值时,stat_value列值显示索引叶子页的数量
* n_diff_pfxNN:NN代表数字(例如:01,02等),当stat_name为n_diff_pfxNN时,stat_value列值显示索引的first column(即索引的最前索引列,从索引定义顺序的第一个列开始)列的唯一值数量,例如:当NN为01时,stat_value列值就表示索引的第一个列的唯一值数量,当NN为02时,stat_value列值就表示索引的第一和第二个列的组合唯一值数量,以此类推。此外,在stat_name = n_diff_pfxNN的情况下,stat_description列显示一个以逗号分隔的计算索引统计信息列的列表
root@localhost : mysql 02:47:28> set global log_output='TABLE';
Query OK, 0 rows affected (0.00 sec)
root@localhost : mysql 02:47:37> set global general_log=1;
Query OK, 0 rows affected (0.00 sec)
root@localhost : mysql 02:47:41> set global slow_query_log=1;
Query OK, 0 rows affected (0.00 sec)
root@localhost : mysql 02:47:45> set global long_query_time=0;
Query OK, 0 rows affected (0.00 sec)
root@localhost : mysql 02:47:50> select connection_id();
+-----------------+
| connection_id() |
+-----------------+
| 4 |
+-----------------+
1 row in set (0.00 sec)
root@localhost : (none) 02:48:22> use test
Database changed
root@localhost : test 02:48:23> lock table test_stat read;
Query OK, 0 rows affected (0.00 sec)
root@localhost : test 02:48:27> select connection_id();
+-----------------+
| connection_id() |
+-----------------+
| 9 |
+-----------------+
1 row in set (0.00 sec)
root@localhost : (none) 02:48:55> select connection_id();
+-----------------+
| connection_id() |
+-----------------+
| 10 |
+-----------------+
1 row in set (0.00 sec)
root@localhost : (none) 02:49:01> use test
Database changed
root@localhost : test 02:49:05> begin;# 显式开启一个事务
Query OK, 0 rows affected (0.00 sec)
root@localhost : test 02:49:06> insert into test_stat values(1,1,1);# 被阻塞
# general_log表
root@localhost : mysql 02:48:03> select * from mysql.general_log;
+----------------------------+---------------------------+-----------+-----------+--------------+-------------------------------------+
| event_time | user_host | thread_id | server_id | command_type | argument |
+----------------------------+---------------------------+-----------+-----------+--------------+-------------------------------------+
# 下面是第一个会话设置修改变量的语句
| 2018-08-19 14:47:45.857168 | root[root] @ localhost [] | 4 | 3306111 | Query | set global slow_query_log=1 |
| 2018-08-19 14:47:50.250382 | root[root] @ localhost [] | 4 | 3306111 | Query | set global long_query_time=0 |
| 2018-08-19 14:48:03.434208 | root[root] @ localhost [] | 4 | 3306111 | Query | select connection_id() |
# 下面是第二个会话对test_stat表加锁的语句
| 2018-08-19 14:48:22.284294 | [root] @ localhost [] | 9 | 3306111 | Connect | root@localhost on using Socket |
| 2018-08-19 14:48:22.284637 | root[root] @ localhost [] | 9 | 3306111 | Query | select @@version_comment limit 1 |
| 2018-08-19 14:48:22.289570 | root[root] @ localhost [] | 9 | 3306111 | Query | select USER() |
| 2018-08-19 14:48:23.744586 | root[root] @ localhost [] | 9 | 3306111 | Query | SELECT DATABASE() |
| 2018-08-19 14:48:23.744747 | root[root] @ localhost [] | 9 | 3306111 | Init DB | test |
| 2018-08-19 14:48:27.529909 | root[root] @ localhost [] | 9 | 3306111 | Query | lock table test_stat read |
| 2018-08-19 14:48:39.194290 | root[root] @ localhost [] | 9 | 3306111 | Query | select connection_id() |
| 2018-08-19 14:48:55.444310 | [root] @ localhost [] | 10 | 3306111 | Connect | root@localhost on using Socket |
# 下面是第三个会话显式开启一个事务插入一行数据的语句
| 2018-08-19 14:48:55.444554 | root[root] @ localhost [] | 10 | 3306111 | Query | select @@version_comment limit 1 |
| 2018-08-19 14:48:55.448847 | root[root] @ localhost [] | 10 | 3306111 | Query | select USER() |
| 2018-08-19 14:49:01.473291 | root[root] @ localhost [] | 10 | 3306111 | Query | select connection_id() |
| 2018-08-19 14:49:05.088319 | root[root] @ localhost [] | 10 | 3306111 | Query | SELECT DATABASE() |
| 2018-08-19 14:49:05.088481 | root[root] @ localhost [] | 10 | 3306111 | Init DB | test |
| 2018-08-19 14:49:06.920451 | root[root] @ localhost [] | 10 | 3306111 | Query | begin |
| 2018-08-19 14:49:23.457191 | root[root] @ localhost [] | 10 | 3306111 | Query | insert into test_stat values(1,1,1) |
# 下面是回到会话1查询general_log的语句
| 2018-08-19 14:49:47.280187 | root[root] @ localhost [] | 4 | 3306111 | Query | select * from mysql.general_log |
+----------------------------+---------------------------+-----------+-----------+--------------+-------------------------------------+
19 rows in set (0.00 sec)
# 通过以上数据表明,general_log表中记录的数据是在MySQL Server接收到语句之后一开始执行就立即记录的。现在我们查询一下slow_log表
root@localhost : mysql 02:49:47> select * from mysql.slow_log;
+----------------------------+---------------------------+-----------------+-----------------+-----------+---------------+------+----------------+-----------+-----------+----------------------------------+-----------+
| start_time | user_host | query_time | lock_time | rows_sent | rows_examined | db | last_insert_id | insert_id | server_id | sql_text | thread_id |
+----------------------------+---------------------------+-----------------+-----------------+-----------+---------------+------+----------------+-----------+-----------+----------------------------------+-----------+
| 2018-08-19 14:48:22.284687 | root[root] @ localhost [] | 00:00:00.000083 | 00:00:00.000000 | 1 | 0 | | 0 | 0 | 3306111 | select @@version_comment limit 1 | 9 |
| 2018-08-19 14:48:22.289881 | root[root] @ localhost [] | 00:00:00.000379 | 00:00:00.000000 | 1 | 0 | | 0 | 0 | 3306111 | select USER() | 9 |
| 2018-08-19 14:48:23.744671 | root[root] @ localhost [] | 00:00:00.000112 | 00:00:00.000000 | 1 | 0 | | 0 | 0 | 3306111 | SELECT DATABASE() | 9 |
| 2018-08-19 14:48:23.744769 | root[root] @ localhost [] | 00:00:00.000035 | 00:00:00.000000 | 1 | 0 | test | 0 | 0 | 3306111 | Init DB | 9 |
| 2018-08-19 14:48:27.530057 | root[root] @ localhost [] | 00:00:00.000172 | 00:00:00.000160 | 0 | 0 | test | 0 | 0 | 3306111 | lock table test_stat read | 9 |
| 2018-08-19 14:48:39.194382 | root[root] @ localhost [] | 00:00:00.000126 | 00:00:00.000000 | 1 | 0 | test | 0 | 0 | 3306111 | select connection_id() | 9 |
| 2018-08-19 14:48:55.444605 | root[root] @ localhost [] | 00:00:00.000087 | 00:00:00.000000 | 1 | 0 | | 0 | 0 | 3306111 | select @@version_comment limit 1 | 10 |
| 2018-08-19 14:48:55.449065 | root[root] @ localhost [] | 00:00:00.000276 | 00:00:00.000000 | 1 | 0 | | 0 | 0 | 3306111 | select USER() | 10 |
| 2018-08-19 14:49:01.473380 | root[root] @ localhost [] | 00:00:00.000122 | 00:00:00.000000 | 1 | 0 | | 0 | 0 | 3306111 | select connection_id() | 10 |
| 2018-08-19 14:49:05.088409 | root[root] @ localhost [] | 00:00:00.000117 | 00:00:00.000000 | 1 | 0 | | 0 | 0 | 3306111 | SELECT DATABASE() | 10 |
| 2018-08-19 14:49:05.088507 | root[root] @ localhost [] | 00:00:00.000041 | 00:00:00.000000 | 1 | 0 | test | 0 | 0 | 3306111 | Init DB | 10 |
| 2018-08-19 14:49:06.920526 | root[root] @ localhost [] | 00:00:00.000091 | 00:00:00.000000 | 0 | 0 | test | 0 | 0 | 3306111 | begin | 10 |
+----------------------------+---------------------------+-----------------+-----------------+-----------+---------------+------+----------------+-----------+-----------+----------------------------------+-----------+
12 rows in set (0.00 sec)
# 发现slow_log表中此时没有INSERT语句的慢查询记录
root@localhost : test 02:48:39> unlock tables;
Query OK, 0 rows affected (0.00 sec)
root@localhost : test 02:49:06> insert into test_stat values(1,1,1);
Query OK, 1 row affected (2 min 12.87 sec)
# general_log表
root@localhost : mysql 02:50:34> select * from mysql.general_log;
+----------------------------+---------------------------+-----------+-----------+--------------+-------------------------------------+
| event_time | user_host | thread_id | server_id | command_type | argument |
+----------------------------+---------------------------+-----------+-----------+--------------+-------------------------------------+
| 2018-08-19 14:47:45.857168 | root[root] @ localhost [] | 4 | 3306111 | Query | set global slow_query_log=1 |
| 2018-08-19 14:47:50.250382 | root[root] @ localhost [] | 4 | 3306111 | Query | set global long_query_time=0 |
| 2018-08-19 14:48:03.434208 | root[root] @ localhost [] | 4 | 3306111 | Query | select connection_id() |
| 2018-08-19 14:48:22.284294 | [root] @ localhost [] | 9 | 3306111 | Connect | root@localhost on using Socket |
| 2018-08-19 14:48:22.284637 | root[root] @ localhost [] | 9 | 3306111 | Query | select @@version_comment limit 1 |
| 2018-08-19 14:48:22.289570 | root[root] @ localhost [] | 9 | 3306111 | Query | select USER() |
| 2018-08-19 14:48:23.744586 | root[root] @ localhost [] | 9 | 3306111 | Query | SELECT DATABASE() |
| 2018-08-19 14:48:23.744747 | root[root] @ localhost [] | 9 | 3306111 | Init DB | test |
| 2018-08-19 14:48:27.529909 | root[root] @ localhost [] | 9 | 3306111 | Query | lock table test_stat read |
| 2018-08-19 14:48:39.194290 | root[root] @ localhost [] | 9 | 3306111 | Query | select connection_id() |
| 2018-08-19 14:48:55.444310 | [root] @ localhost [] | 10 | 3306111 | Connect | root@localhost on using Socket |
| 2018-08-19 14:48:55.444554 | root[root] @ localhost [] | 10 | 3306111 | Query | select @@version_comment limit 1 |
| 2018-08-19 14:48:55.448847 | root[root] @ localhost [] | 10 | 3306111 | Query | select USER() |
| 2018-08-19 14:49:01.473291 | root[root] @ localhost [] | 10 | 3306111 | Query | select connection_id() |
| 2018-08-19 14:49:05.088319 | root[root] @ localhost [] | 10 | 3306111 | Query | SELECT DATABASE() |
| 2018-08-19 14:49:05.088481 | root[root] @ localhost [] | 10 | 3306111 | Init DB | test |
| 2018-08-19 14:49:06.920451 | root[root] @ localhost [] | 10 | 3306111 | Query | begin |
| 2018-08-19 14:49:23.457191 | root[root] @ localhost [] | 10 | 3306111 | Query | insert into test_stat values(1,1,1) |
| 2018-08-19 14:49:47.280187 | root[root] @ localhost [] | 4 | 3306111 | Query | select * from mysql.general_log |
| 2018-08-19 14:50:34.591172 | root[root] @ localhost [] | 4 | 3306111 | Query | select * from mysql.slow_log |
# 下面是第二个会话解锁的语句
| 2018-08-19 14:51:36.350705 | root[root] @ localhost [] | 9 | 3306111 | Query | unlock tables |
# 下面是第一个会话查询general_log的语句
| 2018-08-19 14:51:58.373666 | root[root] @ localhost [] | 4 | 3306111 | Query | select * from mysql.general_log |
+----------------------------+---------------------------+-----------+-----------+--------------+-------------------------------------+
22 rows in set (0.00 sec)
# slow_log表
root@localhost : mysql 02:51:58> select * from mysql.slow_log;
+----------------------------+---------------------------+-----------------+-----------------+-----------+---------------+------+----------------+-----------+-----------+-------------------------------------+-----------+
| start_time | user_host | query_time | lock_time | rows_sent | rows_examined | db | last_insert_id | insert_id | server_id | sql_text | thread_id |
+----------------------------+---------------------------+-----------------+-----------------+-----------+---------------+------+----------------+-----------+-----------+-------------------------------------+-----------+
| 2018-08-19 14:48:22.284687 | root[root] @ localhost [] | 00:00:00.000083 | 00:00:00.000000 | 1 | 0 | | 0 | 0 | 3306111 | select @@version_comment limit 1 | 9 |
| 2018-08-19 14:48:22.289881 | root[root] @ localhost [] | 00:00:00.000379 | 00:00:00.000000 | 1 | 0 | | 0 | 0 | 3306111 | select USER() | 9 |
| 2018-08-19 14:48:23.744671 | root[root] @ localhost [] | 00:00:00.000112 | 00:00:00.000000 | 1 | 0 | | 0 | 0 | 3306111 | SELECT DATABASE() | 9 |
| 2018-08-19 14:48:23.744769 | root[root] @ localhost [] | 00:00:00.000035 | 00:00:00.000000 | 1 | 0 | test | 0 | 0 | 3306111 | Init DB | 9 |
| 2018-08-19 14:48:27.530057 | root[root] @ localhost [] | 00:00:00.000172 | 00:00:00.000160 | 0 | 0 | test | 0 | 0 | 3306111 | lock table test_stat read | 9 |
| 2018-08-19 14:48:39.194382 | root[root] @ localhost [] | 00:00:00.000126 | 00:00:00.000000 | 1 | 0 | test | 0 | 0 | 3306111 | select connection_id() | 9 |
| 2018-08-19 14:48:55.444605 | root[root] @ localhost [] | 00:00:00.000087 | 00:00:00.000000 | 1 | 0 | | 0 | 0 | 3306111 | select @@version_comment limit 1 | 10 |
| 2018-08-19 14:48:55.449065 | root[root] @ localhost [] | 00:00:00.000276 | 00:00:00.000000 | 1 | 0 | | 0 | 0 | 3306111 | select USER() | 10 |
| 2018-08-19 14:49:01.473380 | root[root] @ localhost [] | 00:00:00.000122 | 00:00:00.000000 | 1 | 0 | | 0 | 0 | 3306111 | select connection_id() | 10 |
| 2018-08-19 14:49:05.088409 | root[root] @ localhost [] | 00:00:00.000117 | 00:00:00.000000 | 1 | 0 | | 0 | 0 | 3306111 | SELECT DATABASE() | 10 |
| 2018-08-19 14:49:05.088507 | root[root] @ localhost [] | 00:00:00.000041 | 00:00:00.000000 | 1 | 0 | test | 0 | 0 | 3306111 | Init DB | 10 |
| 2018-08-19 14:49:06.920526 | root[root] @ localhost [] | 00:00:00.000091 | 00:00:00.000000 | 0 | 0 | test | 0 | 0 | 3306111 | begin | 10 |
# 下面是会话2解锁的语句
| 2018-08-19 14:51:36.350840 | root[root] @ localhost [] | 00:00:00.000153 | 00:00:00.000000 | 0 | 0 | test | 0 | 0 | 3306111 | unlock tables | 9 |
# 下面是会话3执行的INSERT慢查询语句
| 2018-08-19 14:51:36.351046 | root[root] @ localhost [] | 00:02:12.893889 | 00:02:12.893718 | 0 | 0 | test | 0 | 0 | 3306111 | insert into test_stat values(1,1,1) | 10 |
+----------------------------+---------------------------+-----------------+-----------------+-----------+---------------+------+----------------+-----------+-----------+-------------------------------------+-----------+
14 rows in set (0.00 sec)
# 注意:此时会话3的事务仍未提交,所以,慢查询日志表中记录的语句与事务无关,只需要语句执行完成即会进行记录
root@localhost : test 02:51:36> commit;
Query OK, 0 rows affected (0.01 sec)
# general_log表
root@localhost : mysql 02:52:26> select * from mysql.general_log;
+----------------------------+---------------------------+-----------+-----------+--------------+-------------------------------------+
| event_time | user_host | thread_id | server_id | command_type | argument |
+----------------------------+---------------------------+-----------+-----------+--------------+-------------------------------------+
| 2018-08-19 14:47:45.857168 | root[root] @ localhost [] | 4 | 3306111 | Query | set global slow_query_log=1 |
| 2018-08-19 14:47:50.250382 | root[root] @ localhost [] | 4 | 3306111 | Query | set global long_query_time=0 |
| 2018-08-19 14:48:03.434208 | root[root] @ localhost [] | 4 | 3306111 | Query | select connection_id() |
| 2018-08-19 14:48:22.284294 | [root] @ localhost [] | 9 | 3306111 | Connect | root@localhost on using Socket |
| 2018-08-19 14:48:22.284637 | root[root] @ localhost [] | 9 | 3306111 | Query | select @@version_comment limit 1 |
| 2018-08-19 14:48:22.289570 | root[root] @ localhost [] | 9 | 3306111 | Query | select USER() |
| 2018-08-19 14:48:23.744586 | root[root] @ localhost [] | 9 | 3306111 | Query | SELECT DATABASE() |
| 2018-08-19 14:48:23.744747 | root[root] @ localhost [] | 9 | 3306111 | Init DB | test |
| 2018-08-19 14:48:27.529909 | root[root] @ localhost [] | 9 | 3306111 | Query | lock table test_stat read |
| 2018-08-19 14:48:39.194290 | root[root] @ localhost [] | 9 | 3306111 | Query | select connection_id() |
| 2018-08-19 14:48:55.444310 | [root] @ localhost [] | 10 | 3306111 | Connect | root@localhost on using Socket |
| 2018-08-19 14:48:55.444554 | root[root] @ localhost [] | 10 | 3306111 | Query | select @@version_comment limit 1 |
| 2018-08-19 14:48:55.448847 | root[root] @ localhost [] | 10 | 3306111 | Query | select USER() |
| 2018-08-19 14:49:01.473291 | root[root] @ localhost [] | 10 | 3306111 | Query | select connection_id() |
| 2018-08-19 14:49:05.088319 | root[root] @ localhost [] | 10 | 3306111 | Query | SELECT DATABASE() |
| 2018-08-19 14:49:05.088481 | root[root] @ localhost [] | 10 | 3306111 | Init DB | test |
| 2018-08-19 14:49:06.920451 | root[root] @ localhost [] | 10 | 3306111 | Query | begin |
| 2018-08-19 14:49:23.457191 | root[root] @ localhost [] | 10 | 3306111 | Query | insert into test_stat values(1,1,1) |
| 2018-08-19 14:49:47.280187 | root[root] @ localhost [] | 4 | 3306111 | Query | select * from mysql.general_log |
| 2018-08-19 14:50:34.591172 | root[root] @ localhost [] | 4 | 3306111 | Query | select * from mysql.slow_log |
| 2018-08-19 14:51:36.350705 | root[root] @ localhost [] | 9 | 3306111 | Query | unlock tables |
| 2018-08-19 14:51:58.373666 | root[root] @ localhost [] | 4 | 3306111 | Query | select * from mysql.general_log |
| 2018-08-19 14:52:26.182895 | root[root] @ localhost [] | 4 | 3306111 | Query | select * from mysql.slow_log |
# 下面是会话3执行的显式commit语句
| 2018-08-19 14:54:14.339269 | root[root] @ localhost [] | 10 | 3306111 | Query | commit |
# 下面是会话1查询general_log表的语句
| 2018-08-19 14:54:23.637580 | root[root] @ localhost [] | 4 | 3306111 | Query | select * from mysql.general_log |
+----------------------------+---------------------------+-----------+-----------+--------------+-------------------------------------+
25 rows in set (0.00 sec)
# slow_log表
root@localhost : mysql 02:54:23> select * from mysql.slow_log;
+----------------------------+---------------------------+-----------------+-----------------+-----------+---------------+------+----------------+-----------+-----------+-------------------------------------+-----------+
| start_time | user_host | query_time | lock_time | rows_sent | rows_examined | db | last_insert_id | insert_id | server_id | sql_text | thread_id |
+----------------------------+---------------------------+-----------------+-----------------+-----------+---------------+------+----------------+-----------+-----------+-------------------------------------+-----------+
| 2018-08-19 14:48:22.284687 | root[root] @ localhost [] | 00:00:00.000083 | 00:00:00.000000 | 1 | 0 | | 0 | 0 | 3306111 | select @@version_comment limit 1 | 9 |
| 2018-08-19 14:48:22.289881 | root[root] @ localhost [] | 00:00:00.000379 | 00:00:00.000000 | 1 | 0 | | 0 | 0 | 3306111 | select USER() | 9 |
| 2018-08-19 14:48:23.744671 | root[root] @ localhost [] | 00:00:00.000112 | 00:00:00.000000 | 1 | 0 | | 0 | 0 | 3306111 | SELECT DATABASE() | 9 |
| 2018-08-19 14:48:23.744769 | root[root] @ localhost [] | 00:00:00.000035 | 00:00:00.000000 | 1 | 0 | test | 0 | 0 | 3306111 | Init DB | 9 |
| 2018-08-19 14:48:27.530057 | root[root] @ localhost [] | 00:00:00.000172 | 00:00:00.000160 | 0 | 0 | test | 0 | 0 | 3306111 | lock table test_stat read | 9 |
| 2018-08-19 14:48:39.194382 | root[root] @ localhost [] | 00:00:00.000126 | 00:00:00.000000 | 1 | 0 | test | 0 | 0 | 3306111 | select connection_id() | 9 |
| 2018-08-19 14:48:55.444605 | root[root] @ localhost [] | 00:00:00.000087 | 00:00:00.000000 | 1 | 0 | | 0 | 0 | 3306111 | select @@version_comment limit 1 | 10 |
| 2018-08-19 14:48:55.449065 | root[root] @ localhost [] | 00:00:00.000276 | 00:00:00.000000 | 1 | 0 | | 0 | 0 | 3306111 | select USER() | 10 |
| 2018-08-19 14:49:01.473380 | root[root] @ localhost [] | 00:00:00.000122 | 00:00:00.000000 | 1 | 0 | | 0 | 0 | 3306111 | select connection_id() | 10 |
| 2018-08-19 14:49:05.088409 | root[root] @ localhost [] | 00:00:00.000117 | 00:00:00.000000 | 1 | 0 | | 0 | 0 | 3306111 | SELECT DATABASE() | 10 |
| 2018-08-19 14:49:05.088507 | root[root] @ localhost [] | 00:00:00.000041 | 00:00:00.000000 | 1 | 0 | test | 0 | 0 | 3306111 | Init DB | 10 |
| 2018-08-19 14:49:06.920526 | root[root] @ localhost [] | 00:00:00.000091 | 00:00:00.000000 | 0 | 0 | test | 0 | 0 | 3306111 | begin | 10 |
| 2018-08-19 14:51:36.350840 | root[root] @ localhost [] | 00:00:00.000153 | 00:00:00.000000 | 0 | 0 | test | 0 | 0 | 3306111 | unlock tables | 9 |
| 2018-08-19 14:51:36.351046 | root[root] @ localhost [] | 00:02:12.893889 | 00:02:12.893718 | 0 | 0 | test | 0 | 0 | 3306111 | insert into test_stat values(1,1,1) | 10 |
# 下面是会话3执行的COMMIT语句
| 2018-08-19 14:54:14.341319 | root[root] @ localhost [] | 00:00:00.002071 | 00:00:00.000000 | 0 | 0 | test | 0 | 0 | 3306111 | commit | 10 |
+----------------------------+---------------------------+-----------------+-----------------+-----------+---------------+------+----------------+-----------+-----------+-------------------------------------+-----------+
15 rows in set (0.00 sec)
* 举个例子:就拿我们上面示例来说,如果把long_query_time变量设置为1(表示执行时间超过1S就记录为慢查询),你就会发现无论INSERT语句被其他语句的锁阻塞多久,都不会记录到慢查询日志中(因为INSERT语句插入一行记录的执行时间排除了锁等待时间之外,真正的执行时间超过1S的情况微乎其微)
坚持阅读我们的"全方位认识 mysql 系统库"系列文章分享,你就可以系统地学完它。截止目前为止,MySQL的4个系统库"performance_schema"、"information_schema"、"sys"、"mysql"我们已经通过4个系列文章为大家介绍完毕。如果你觉得对你有帮助或者你认为有学习与传播的价值,欢迎分享给他人。后续我们将择期推出MySQL优化与复制板块的系列文章。谢谢你的阅读,我们下期不见不散!
| 作者简介
《千金良方——MySQL性能优化金字塔法则》、《数据生态:MySQL复制技术与生产实践》作者之一。
熟悉MySQL体系结构,擅长数据库的整体调优,喜好专研开源技术,并热衷于开源技术的推广,在线上线下做过多次公开的数据库专题分享,发表过近100篇数据库相关的研究文章。
全文完。