-
Notifications
You must be signed in to change notification settings - Fork 55
第17章 mysql系统库应用示例荟萃
xiaoboluo768 edited this page Jun 18, 2020
·
4 revisions
mysql> grant select on *.* to test_global@'%' identified by 'test';
Query OK, 0 rows affected, 1 warning (0.00 sec)
mysql> use mysql
Database changed
mysql> select * from user where user='test_global'\G
......
1 row in set (0.00 sec)
mysql> select * from db where user='test_global'\G
Empty set (0.02 sec)
mysql> select * from tables_priv where user='test_global'\G
Empty set (0.00 sec)
mysql> select * from columns_priv where user='test_global'\G
Empty set (0.00 sec)
mysql> create database test_grant;
Query OK, 1 row affected (0.01 sec)
mysql> grant select on test_grant.* to test_db@'%' identified by 'test';
Query OK, 0 rows affected, 1 warning (0.00 sec)
mysql> select * from user where user='test_db'\G
......
1 row in set (0.00 sec)
mysql> select * from db where user='test_db'\G
......
1 row in set (0.00 sec)
mysql> select * from tables_priv where user='test_db'\G
Empty set (0.00 sec)
mysql> select * from columns_priv where user='test_db'\G
Empty set (0.00 sec)
mysql> use test_grant
Database changed
mysql> create table test_table_grant(id int);
Query OK, 0 rows affected (0.03 sec)
mysql> grant select on test_grant.test_table_grant to test_table@'%' identified by 'test';
Query OK, 0 rows affected, 1 warning (0.01 sec)
mysql> select * from mysql.user where user='test_table'\G
......
1 row in set (0.00 sec)
mysql> select * from mysql.db where user='test_table'\G
Empty set (0.00 sec)
mysql> select * from mysql.tables_priv where user='test_table'\G
......
1 rows in set (0.00 sec)
mysql> select * from mysql.columns_priv where user='test_table'\G
Empty set (0.00 sec)
mysql> 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)
mysql> select * from mysql.user where user='test_column'\G
......
1 row in set (0.00 sec)
mysql> select * from mysql.db where user='test_column'\G
Empty set (0.00 sec)
mysql> select * from mysql.tables_priv where user='test_column'\G
......
1 row in set (0.00 sec)
# 通过对mysql.tables_priv表的查询发现,test_column用户对test_table_grant表具有字段级别的Select权限。由于字段级别权限是针对具体的字段授予的,这些信息需要通过mysql.columns_priv表来查询,如下所示
mysql> select * from mysql.columns_priv where user='test_column'\G
......
1 row in set (0.00 sec)
- 提示:以上示例中未涉及的权限管理请读者自行尝试,这里不再赘述。
mysql> use test
Database changed
mysql> create table test_stat(id int not null primary key auto_increment);
Query OK, 0 rows affected (0.01 sec)
mysql> select * from mysql.innodb_table_stats where table_name='test_stat'\G
......
1 row in set (0.00 sec)
mysql> 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
mysql> 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
mysql> set global log_output='TABLE';
Query OK, 0 rows affected (0.00 sec)
mysql> set global general_log=1;
Query OK, 0 rows affected (0.00 sec)
mysql> set global slow_query_log=1;
Query OK, 0 rows affected (0.00 sec)
mysql> set global long_query_time=0;
Query OK, 0 rows affected (0.00 sec)
mysql> select connection_id();
+-----------------+
| connection_id() |
+-----------------+
| 4 |
+-----------------+
1 row in set (0.00 sec)
mysql> use test
Database changed
mysql> lock table test_stat read;
Query OK, 0 rows affected (0.00 sec)
mysql> select connection_id();
......
1 row in set (0.00 sec)
mysql> select connection_id();
......
1 row in set (0.00 sec)
mysql> use test
Database changed
mysql> begin;# 显式开启一个事务
Query OK, 0 rows affected (0.00 sec)
mysql> insert into test_stat values(1,1,1);# 被阻塞
# general_log表
mysql> select * from mysql.general_log;
......
19 rows in set (0.00 sec)
# 以上数据表明,general_log表中的数据是在MySQL服务器接收到语句之后一开始执行就立即记录的。现在我们查询slow_log表
mysql> select * from mysql.slow_log;
......
12 rows in set (0.00 sec)
# 此时发现slow_log表中没有INSERT语句的慢查询日志记录
mysql> unlock tables;
Query OK, 0 rows affected (0.00 sec)
mysql> insert into test_stat values(1,1,1);
Query OK, 1 row affected (2 min 12.87 sec)
# general_log表
mysql> select * from mysql.general_log;
......
22 rows in set (0.00 sec)
# slow_log表
mysql> select * from mysql.slow_log;
......
14 rows in set (0.00 sec)
# 注意:此时会话3的事务仍未提交,所以在慢查询日志表中记录的语句与事务无关,只需要语句执行完成即会进行记录
mysql> commit;
Query OK, 0 rows affected (0.01 sec)
# general_log表
mysql> select * from mysql.general_log;
......
25 rows in set (0.00 sec)
# slow_log表
mysql> select * from mysql.slow_log;
......
15 rows in set (0.00 sec)
- 温馨提示:关于mysql schema库更详细的内容,可参阅微信公众号“沃趣技术”。或详见开源文档:mysql schema详解
上一篇:第16章 mysql系统库之日志记录表 | 下一篇:第18章 复制技术的演进
-
本 WIKI 包含了《千金良方--MySQL 性能优化金字塔法则》一书的代码段加粗命令行命令和SQL语句文本、以及4个附录内容,其中:
- 代码段和高清图单独整理为一个系列文档,如下:
- 每个附录都各自整理成了一个小系列文档,如下:
-
《千金良方--MySQL 性能优化金字塔法则》 一书的作者信息如下:
- 李春、罗小波、董红禹
-
联系人QQ:309969177
-
提示:
-
郑重声明:本WIKI仓库中的资料为电子工业出版社与本书的三位作者共同授权开源,为了在方便大家的同时,避免不必要的纠葛,任何商业与非商业的引用、转载,麻烦大家注明出处,谢谢配合!