MySQL 權限和查詢緩存

MySQL 權限和查詢緩存mysql

================================================================================sql

概述:數據庫

 本章將主要介紹MySQL數據庫中的權限和查詢緩存,具體內容以下:vim

  • MySQL的權限類別;centos

     ·數據類權限:庫級別、表級別、字段級別;緩存

     ·管理類權限;bash

     ·程序類權限;服務器

  • MySQL用戶管理:ide

     ·用戶帳號;函數

     ·建立、重命名、刪除、修改用戶密碼

  • 忘記管理員密碼的解決辦法:

  • 受權:GRANT

     ·建立、查看及取消受權

  • 查詢緩存

     ·查詢緩存相關的服務器變量:

     ·統計緩存數據狀態變量:

================================================================================

MySQL用戶和權限管理 

  1.權限類別

用戶帳號:user@host

  • user:帳戶名稱;

  • host:此帳戶可經過哪些客戶端主機請求建立鏈接線程; 

              %:任意長度的任意字符;

               _:任意單個字符;

MySQL權限類別:

數據類權限:

  • 庫級別:把某個數據庫的全部權限或某些權限受權給指定用戶(數據庫級別的);

  • 表級別:把一個數據庫中的某個表或有限的幾個表受權給指定用戶;

  • 字段級別把一個表上指定的字段受權給指定用戶;

管理類:如改變服務器變量的值等;

程序類:可否調用或執行函數,程序代碼等程序;

管理類:

  • CREATE USER; //建立用戶

  • RELOAD;         //能夠使mysql關閉日誌文件,滾動日誌文件並打開新文件     

  • LOCK TABLES;

  • REPLICATION CLIENT, REPLICATION SLAVE;

  • SHUTDOWN;  //關閉mysql服務器進程的權限

  • FILE;                //從文件中加載數據的權限

  • SHOW DATABASES;

  • PROCESS;       //查看進程

  • SUPER               //不受控制的任何管理功能等權限

程序類:

  • FUNCTION(存儲函數),PROCEDURE(存儲過程),TRIGGER(觸發器)

  • 操做:CREATE,ALTER,DROP,EXECUTE(執行)

庫和表級別:

  • CREATE,ALTER,DROP

  • INDEX

  • CREATE VIEW

  • SHOW VIEW

  • GRANT:受權

  • OPTION:可以把本身得到的權限生成一個副本轉贈給其它用戶;

數據操做權限:

表:

  • INSERT/DELETE/UPDATE/SELECT 

字段:

  • SELECT(col1,col2,...)

  • UPDATE(col1,col2,...)

  • INSERT(col1,col2,...)

全部權限:

  • ALL

  • ALL PRIVILEGES

元數據數據庫(數據字典):mysql

受權相關的表:

  • db, host, user

  • tables_priv, column_priv, procs_priv, proxies_priv 

MariaDB [(none)]> use mysql;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Database changed
MariaDB [mysql]> SHOW TABLES;
+---------------------------+
| Tables_in_mysql           |
+---------------------------+
| columns_priv              | # 字段級別的權限
| db                        | # 庫級別的權限表
| event                     |
| func                      |
| general_log               |
| help_category             |
| help_keyword              |
| help_relation             |
| help_topic                |
| host                      | # 主機鏈接限制
| ndb_binlog_index          |
| plugin                    |
| proc                      |
| procs_priv                | # 程序級別的權限
| proxies_priv              | # 代理權限
| servers                   |
| slow_log                  |
| tables_priv               | # 表級別的權限
| time_zone                 |
| time_zone_leap_second     |
| time_zone_name            |
| time_zone_transition      |
| time_zone_transition_type |
| user                      | # 用戶帳號表
+---------------------------+
24 rows in set (0.00 sec)

 

 2.MySQL用戶管理:

用戶帳號:user@host

  • user:帳戶名稱;

  • host:此帳戶可經過哪些客戶端主機請求建立鏈接線程;(IP,主機名,NETWORK)

            %:任意長度的任意字符;

             _:任意單個字符;

注意:

  • 基於主機名受權和IP地址受權是兩個不一樣的帳號,若是基於主機名受權時會反解主機名到IP地址,通常不建議,而且要跳過主機名解析

skip_name_resolve={ON|OFF}

  • 是否跳過主機名解析

建立用戶:

  • CREATE USER  'user'@'host' [IDENTIFIED BY [PASSWORD] 'password'] [,'user'@'host' [IDENTIFIED BY [PASSWORD] 'password']...]

重命名:RENAME USER

  • RENAME USER old_user TO new_user[, old_user TO new_user] ...

刪除用戶:

  • DROP USER 'user'@'host' [, 'user'@'host'] ...

修改用戶密碼

  • SET PASSWORD [FOR 'user'@'host'] = PASSWORD('cleartext password');

  • UPDATE mysql.user SET Password=PASSWORD('cleartext password')  WHERE User='USERNAME' AND Host='HOST';

  • mysqladmin -uUSERNAME -hHOST -p  password 'NEW_PASS'

FLUSH PRIVILEGES  

  • 通知mysql進程重讀受權表

演示:

  1.能夠使用SHOW PROCESSLIST 查看當前線程列表,以下:

MariaDB [hellodb]> SHOW PROCESSLIST;
+----+---------+-----------------+---------+---------+------+-------+------------------+----------+
| Id | User    | Host            | db      | Command | Time | State | Info             | Progress |
+----+---------+-----------------+---------+---------+------+-------+------------------+----------+
|  3 | root    | localhost       | hellodb | Query   |    0 | NULL  | SHOW PROCESSLIST |    0.000 |
|  4 | rsyslog | 127.0.0.1:38101 | Syslog  | Sleep   |  123 |       | NULL             |    0.000 |
+----+---------+-----------------+---------+---------+------+-------+------------------+----------+
2 rows in set (0.00 sec)

 2.建立用戶,修改密碼,修改用戶名,並查看剛建立用戶的權限

# 建立用戶帳號及密碼
MariaDB [(none)]> CREATE USER 'tao'@'192.168.1.%' IDENTIFIED BY '134296'; 
Query OK, 0 rows affected (0.00 sec)
# 修改用戶帳戶密碼,並重讀受權表
MariaDB [(none)]> SET PASSWORD FOR 'tao'@'192.168.1.%' = PASSWORD('123456'); 
Query OK, 0 rows affected (0.00 sec)

MariaDB [(none)]> FLUSH PRIVILEGES;
Query OK, 0 rows affected (0.01 sec)
# 修改用戶名
MariaDB [(none)]> RENAME USER 'tao'@'192.168.1.%' TO 'xiu'@'192.168.1.%';
Query OK, 0 rows affected (0.02 sec)
# 並查看剛建立用戶的權限,發現爲usage
MariaDB [(none)]> SHOW GRANTS FOR 'xiu'@'192.168.1.%';
+--------------------------------------------------------------------------------------------------------------+
| Grants for xiu@192.168.1.%                                                                                   |
+--------------------------------------------------------------------------------------------------------------+
| GRANT USAGE ON *.* TO 'xiu'@'192.168.1.%' IDENTIFIED BY PASSWORD '*6BB4837EB74329105EE4568DDA7DC67ED2CA2AD9' |
+--------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)

 3.登陸建立的用戶帳戶,建立數據庫,能夠發現權限不被容許,可見新建立的用戶幾乎是沒有任何權限的;要想有權限就須要給用戶授予相關的管理、程序、數據類的權限等

[root@centos7 ~]# mysql -uxiu -h192.168.1.107 -p123456
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 6
Server version: 5.5.44-MariaDB MariaDB Server

Copyright (c) 2000, 2015, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

MariaDB [(none)]> SHOW DATABASES;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| test               |
+--------------------+
2 rows in set (0.05 sec)

MariaDB [test]> CREATE DATABASE mydb;
ERROR 1044 (42000): Access denied for user 'xiu'@'192.168.1.%' to database 'mydb'

 4.要想有權限就須要給用戶授予相關的管理、程序、數據類的權限等,授予xiu用戶相關的權限以下:

MariaDB [(none)]> GRANT ALL ON mydb.* TO 'xiu'@'192.168.1.%'; # 授予全部的權限
Query OK, 0 rows affected (0.00 sec)

MariaDB [(none)]> SHOW GRANTS FOR 'xiu'@'192.168.1.%';
+--------------------------------------------------------------------------------------------------------------+
| Grants for xiu@192.168.1.%                                                                                   |
+--------------------------------------------------------------------------------------------------------------+
| GRANT USAGE ON *.* TO 'xiu'@'192.168.1.%' IDENTIFIED BY PASSWORD '*6BB4837EB74329105EE4568DDA7DC67ED2CA2AD9' |
| GRANT ALL PRIVILEGES ON `mydb`.* TO 'xiu'@'192.168.1.%'                                                      |
+--------------------------------------------------------------------------------------------------------------+
2 rows in set (0.00 sec)

MariaDB [(none)]> REVOKE UPDATE ON mydb.* FROM 'xiu'@'192.168.1.%'; # 回收UPDATE權限
Query OK, 0 rows affected (0.00 sec)

MariaDB [(none)]> SHOW GRANTS FOR 'xiu'@'192.168.1.%';
+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Grants for xiu@192.168.1.%                                                                                                                                                                                                    |
+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| GRANT USAGE ON *.* TO 'xiu'@'192.168.1.%' IDENTIFIED BY PASSWORD '*6BB4837EB74329105EE4568DDA7DC67ED2CA2AD9'                                                                                                                  |
| GRANT SELECT, INSERT, DELETE, CREATE, DROP, REFERENCES, INDEX, ALTER, CREATE TEMPORARY TABLES, LOCK TABLES, EXECUTE, CREATE VIEW, SHOW VIEW, CREATE ROUTINE, ALTER ROUTINE, EVENT, TRIGGER ON `mydb`.* TO 'xiu'@'192.168.1.%' |
+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
2 rows in set (0.00 sec)



 3.忘記管理員密碼的解決辦法

解決步驟以下:

(1)啓動mysqld進程時,使用--skip-grant-tables和--skip-networking選項;

  • CentOS 7:mariadb.service

  • CentOS 6:/etc/init.d/mysqld

(2)經過UPDATE命令修改管理員密碼;

(3)以正常方式啓動mysqld進程;

演示:

  1.以CentOS 7 爲例,首先查看mysql數據庫user表中有關用戶的帳戶名和密碼以下:

MariaDB [(none)]> SELECT user,host,password FROM mysql.user; 
+------------+-------------+-------------------------------------------+
| user       | host        | password                                  |
+------------+-------------+-------------------------------------------+
| root       | localhost   | *41EE0F8759D5340036B009143E1727DB5787A448 |
| root       | centos7     | *41EE0F8759D5340036B009143E1727DB5787A448 |
| root       | 127.0.0.1   | *41EE0F8759D5340036B009143E1727DB5787A448 |
| root       | ::1         | *41EE0F8759D5340036B009143E1727DB5787A448 |
| ultraxuser | 127.0.0.1   | *41EE0F8759D5340036B009143E1727DB5787A448 |
| ultraxuser | localhost   | *41EE0F8759D5340036B009143E1727DB5787A448 |
| rsyslog    | 127.0.0.1   | *41EE0F8759D5340036B009143E1727DB5787A448 |
| rsyslog    | local       | *41EE0F8759D5340036B009143E1727DB5787A448 |
| zbxuser    | 10.1.%.%    | *24E65C3D3577DA6C2A596788CEAA02923A74B75D |
| zbxuser    | 127.0.0.1   | *24E65C3D3577DA6C2A596788CEAA02923A74B75D |
| xiu        | 192.168.1.% | *6BB4837EB74329105EE4568DDA7DC67ED2CA2AD9 |
+------------+-------------+-------------------------------------------+

 2.假如如今我忘記了root管理員用戶的密碼,執行過程以下:

  1)首先關閉mysql服務,修改啓動mariadb.service的文件,添加 --skip-grant-tables和--skip-networking選項,以下:

[root@centos7 ~]# systemctl stop mariadb.service  # 中止mariadb服務
[root@centos7 ~]# vim /usr/lib/systemd/system/mariadb.service  # 修改mariadb啓動文件
 ExecStart=/usr/bin/mysqld_safe --basedir=/usr --skip-grant-tables --skip-networking //在ExecStart 後添加
 
[root@centos7 ~]# systemctl daemon-reload  # 要想生效須要重載

  2)再次啓動mariadb服務,而後使用mysql直接鏈接,鏈接成功後使用UPDATE修改密碼

[root@centos7 ~]# mysql
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 2
Server version: 5.5.44-MariaDB MariaDB Server

Copyright (c) 2000, 2015, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

MariaDB [(none)]> UPDATE mysql.user SET Password=PASSWORD('taoxiu') WHERE User='root'; # 修改密碼
Query OK, 4 rows affected (0.00 sec)
Rows matched: 4  Changed: 4  Warnings: 0

MariaDB [(none)]> exit
Bye

 3)而後再次關閉mariadb服務,把mariadb啓動文件中添加的--skip-grant-tables和--skip-networking選項刪除便可

[root@centos7 ~]# systemctl stop mariadb.service 
[root@centos7 ~]# vim /usr/lib/systemd/system/mariadb.service 
  ExecStart=/usr/bin/mysqld_safe --basedir=/usr  # 把添加的選項刪除便可

[root@centos7 ~]# systemctl daemon-reload
[root@centos7 ~]# systemctl start mariadb.service 
[root@centos7 ~]# mysql  # 再使用mysql直接登陸,發現已經登陸不上了
ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: NO)

[root@centos7 ~]# mysql -uroot -ptaoxiu # 使用修改的密碼成功登陸
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 3
Server version: 5.5.44-MariaDB MariaDB Server

Copyright (c) 2000, 2015, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

MariaDB [(none)]>



 4.受權:GRANT

GRANT 

  • priv_type [(column_list)] [, priv_type [(column_list)]] ... //權限類型(字段)

  • ON [object_type] priv_level

  • TO user_specification [, user_specification] ...   指明用戶名、主機、密碼(存在的話就不須要了),一次可受權多個

  • [REQUIRE {NONE | ssl_option [[AND] ssl_option] ...}]

  • [WITH with_option ...]

wKiom1g86sTj8P_CAAEK1E0UvUc164.png


查看受權:SHOW GRANTS 

  • SHOW GRANTS [FOR 'user'@'host']

取消受權:REVOKE

REVOKE

  • priv_type [(column_list)][, priv_type [(column_list)]] ...

  • ON [object_type] priv_level

  • FROM  'user'@'host' [,  'user'@'host'] ...

REVOKE ALL PRIVILEGES, GRANT OPTION

  • FROM user [, user] ...

演示:
  1.建立tao用戶帳戶,並指定密碼;

MariaDB [mysql]> CREATE USER 'tao'@'localhost' IDENTIFIED BY '123456';
Query OK, 0 rows affected (0.01 sec)

MariaDB [mysql]> SHOW GRANTS FOR 'tao'@'localhost';
+------------------------------------------------------------------------------------------------------------+
| Grants for tao@localhost                                                                                   |
+------------------------------------------------------------------------------------------------------------+
| GRANT USAGE ON *.* TO 'tao'@'localhost' IDENTIFIED BY PASSWORD '*6BB4837EB74329105EE4568DDA7DC67ED2CA2AD9' |
+------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)

  2.授予tao用戶全部權限;

MariaDB [(none)]> GRANT ALL ON mydb.* TO 'tao'@'localhost';
Query OK, 0 rows affected (0.01 sec)

MariaDB [(none)]> SHOW GRANTS FOR 'tao'@'localhost';
+------------------------------------------------------------------------------------------------------------+
| Grants for tao@localhost                                                                                   |
+------------------------------------------------------------------------------------------------------------+
| GRANT USAGE ON *.* TO 'tao'@'localhost' IDENTIFIED BY PASSWORD '*6BB4837EB74329105EE4568DDA7DC67ED2CA2AD9' |
| GRANT ALL PRIVILEGES ON `mydb`.* TO 'tao'@'localhost'                                                      |
+------------------------------------------------------------------------------------------------------------+
2 rows in set (0.00 sec)

 3.如今我再建立一個叫'jing'的用戶,看讓tao用戶可否把本身的權限轉增給jing用戶,以下:

MariaDB [(none)]> CREATE USER 'jing'@'localhost';  # 在root管理員上建立jing用戶
Query OK, 0 rows affected (0.00 sec)

[root@centos7 ~]# mysql -utao -hlocalhost -p123456 # 登陸tao用戶帳號
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 10
Server version: 5.5.44-MariaDB MariaDB Server

Copyright (c) 2000, 2015, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

MariaDB [(none)]> show databases;  # 能夠看到受權的mydb數據庫
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mydb               |
| test               |
+--------------------+
3 rows in set (0.01 sec)

# tao用戶把本身的權限轉贈給jing用戶,發現失敗,這是由於要想有轉贈的權限,須要管理員授予WITH GRANT OPTION權限
MariaDB [(none)]> GRANT SELECT,INSERT,DELETE on mydb.* TO 'jing'@'localhost';
ERROR 1044 (42000): Access denied for user 'tao'@'localhost' to database 'mydb'

 4.管理員授予tao用戶容許轉贈的權限,再次測試能夠發現能夠成功轉贈給jing用戶

MariaDB [(none)]> GRANT ALL ON mydb.* TO 'tao'@'localhost' WITH GRANT OPTION;
Query OK, 0 rows affected (0.00 sec)

MariaDB [(none)]> FLUSH PRIVILEGES;
Query OK, 0 rows affected (0.00 sec)

# ---------------------------------------------------------------------------------------

# 登陸tao用戶帳號,查看其權限能夠發現多了一項 WITH GRANT OPTION 的權限,
MariaDB [(none)]> SHOW GRANTs FOR 'tao'@'localhost'; 
+------------------------------------------------------------------------------------------------------------+
| Grants for tao@localhost                                                                                   |
+------------------------------------------------------------------------------------------------------------+
| GRANT USAGE ON *.* TO 'tao'@'localhost' IDENTIFIED BY PASSWORD '*6BB4837EB74329105EE4568DDA7DC67ED2CA2AD9' |
| GRANT ALL PRIVILEGES ON `mydb`.* TO 'tao'@'localhost' WITH GRANT OPTION                                    |
+------------------------------------------------------------------------------------------------------------+
2 rows in set (0.00 sec)

# 成功將本身的權限轉贈給jing用戶
MariaDB [(none)]> GRANT SELECT,INSERT,DELETE on mydb.* TO 'jing'@'localhost';
Query OK, 0 rows affected (0.00 sec)

 5.收回tao用戶的GRANT OPTION的權限

MariaDB [(none)]> REVOKE GRANT OPTION ON mydb.* FROM 'tao'@'localhost'; # 回收權限
Query OK, 0 rows affected (0.00 sec)

MariaDB [(none)]> SHOW GRANTS FOR 'tao'@'localhost';
+------------------------------------------------------------------------------------------------------------+
| Grants for tao@localhost                                                                                   |
+------------------------------------------------------------------------------------------------------------+
| GRANT USAGE ON *.* TO 'tao'@'localhost' IDENTIFIED BY PASSWORD '*6BB4837EB74329105EE4568DDA7DC67ED2CA2AD9' |
| GRANT ALL PRIVILEGES ON `mydb`.* TO 'tao'@'localhost'                                                      |
+------------------------------------------------------------------------------------------------------------+
2 rows in set (0.00 sec)

 6.如今受權tao用戶hellodb數據庫的stuents表上某個字段的受權,以下:

MariaDB [(none)]> GRANT SELECT(Name,Age),UPDATE(Age) ON hellodb.students TO 'tao'@'localhost';
Query OK, 0 rows affected (0.06 sec)

 登陸tao的用戶帳號,查看權限,執行操做以下:

MariaDB [(none)]> SHOW GRANTS FOR 'tao'@'localhost'; # 查看權限以下:
+------------------------------------------------------------------------------------------------------------+
| Grants for tao@localhost                                                                                   |
+------------------------------------------------------------------------------------------------------------+
| GRANT USAGE ON *.* TO 'tao'@'localhost' IDENTIFIED BY PASSWORD '*6BB4837EB74329105EE4568DDA7DC67ED2CA2AD9' |
| GRANT ALL PRIVILEGES ON `mydb`.* TO 'tao'@'localhost'                                                      |
| GRANT SELECT (Age, Name), UPDATE (Age) ON `hellodb`.`students` TO 'tao'@'localhost'                        |
+------------------------------------------------------------------------------------------------------------+
3 rows in set (0.00 sec)

MariaDB [(none)]> SHOW DATABASES; # 能夠看到數據庫中又多了一個hellodb的數據庫
+--------------------+
| Database           |
+--------------------+
| information_schema |
| hellodb            |
| mydb               |
| test               |
+--------------------+
4 rows in set (0.00 sec)

MariaDB [(none)]> USE hellodb;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Database changed
MariaDB [hellodb]> SHOW TABLES;  # 能夠發現僅能看見咱們受權給的students表
+-------------------+
| Tables_in_hellodb |
+-------------------+
| students          |
+-------------------+
1 row in set (0.05 sec)

MariaDB [hellodb]> DESC students;  # 表結構也只有Name和Age兩個受權的字段,其餘未受權的均不可見
+-------+---------------------+------+-----+---------+-------+
| Field | Type                | Null | Key | Default | Extra |
+-------+---------------------+------+-----+---------+-------+
| Name  | varchar(50)         | NO   |     | NULL    |       |
| Age   | tinyint(3) unsigned | NO   |     | NULL    |       |
+-------+---------------------+------+-----+---------+-------+
2 rows in set (0.08 sec)

MariaDB [hellodb]> SELECT * FROM students; # 查詢全部的的字段發現不容許,由於其餘字段未經受權
ERROR 1143 (42000): SELECT command denied to user 'tao'@'localhost' for column 'StuID' in table 'students'
MariaDB [hellodb]> SELECT Name,Age FROM students; # 只能查看受權的字段
+---------------+-----+
| Name          | Age |
+---------------+-----+
| Shi Zhongyu   |  22 |
| Shi Potian    |  22 |
| Xie Yanke     |  53 |
| Ding Dian     |  32 |
| Yu Yutong     |  26 |
| Shi Qing      |  46 |
| Xi Ren        |  19 |
| Lin Daiyu     |  17 |
| Ren Yingying  |  20 |
| Yue Lingshan  |  19 |
| Yuan Chengzhi |  23 |
| Wen Qingqing  |  19 |
| Tian Boguang  |  33 |
| Lu Wushuang   |  17 |
| Duan Yu       |  19 |
| Xu Zhu        |  21 |
| Lin Chong     |  25 |
| Hua Rong      |  23 |
| Xue Baochai   |  18 |
| Diao Chan     |  19 |
| Huang Yueying |  22 |
| Xiao Qiao     |  20 |
| Ma Chao       |  23 |
| Xu Xian       |  27 |
| Sun Dasheng   | 100 |
+---------------+-----+
25 rows in set (0.00 sec)

#-----------------------------------------------------------------------------------

MariaDB [hellodb]> UPDATE students SET Age=21 WHERE Name='Duan YU'; # 能夠修改年齡
Query OK, 1 row affected (0.00 sec)
Rows matched: 1  Changed: 1  Warnings: 0

MariaDB [hellodb]> UPDATE students SET Name=GuoJing WHERE Name='Duan YU'; # 修改姓名失敗,由於只受權了UPDATE容許修改Age字段
ERROR 1143 (42000): UPDATE command denied to user 'tao'@'localhost' for column 'Name' in table 'students'
MariaDB [hellodb]> SELECT Name,Age FROM students;
+---------------+-----+
| Name          | Age |
+---------------+-----+
| Shi Zhongyu   |  22 |
| Shi Potian    |  22 |
| Xie Yanke     |  53 |
| Ding Dian     |  32 |
| Yu Yutong     |  26 |
| Shi Qing      |  46 |
| Xi Ren        |  19 |
| Lin Daiyu     |  17 |
| Ren Yingying  |  20 |
| Yue Lingshan  |  19 |
| Yuan Chengzhi |  23 |
| Wen Qingqing  |  19 |
| Tian Boguang  |  33 |
| Lu Wushuang   |  17 |
| Duan Yu       |  21 |
| Xu Zhu        |  21 |
| Lin Chong     |  25 |
| Hua Rong      |  23 |
| Xue Baochai   |  18 |
| Diao Chan     |  19 |
| Huang Yueying |  22 |
| Xiao Qiao     |  20 |
| Ma Chao       |  23 |
| Xu Xian       |  27 |
| Sun Dasheng   | 100 |
+---------------+-----+
25 rows in set (0.00 sec)




查詢緩存:

  1.介紹

緩存:k/v 

  • key:查詢語句的hash值

  • value:查詢語句的執行結果

如何判斷緩存是否命中:

   經過查詢語句的哈希值判斷:哈希值考慮的因素包括

  • 查詢自己;

  • 要查詢數據庫;

  • 客戶端使用的協議版本;

      ...

哪些查詢可能不會被緩存?

  • 查詢語句中包含UDF(用戶自定義函數)

  • 存儲函數 如:CURRENT_TIME()

  • 用戶自定義變量

  • 臨時表

  • mysql系統表或者是包含列級別權限的查詢

  • 有着不肯定結果值的函數(now());

附圖:

  • 查詢執行過程

wKiom1g9KieAwEJNAATdGHGYHpE047.jpg

 2.查詢緩存相關的服務器變量:

query_cache_limit:

  • 可以緩存的最大查詢結果;(單語句結果集大小上限)

  • 有着較大結果集的語句,顯式使用 SQL_NO_CACHE,以免先緩存再移出; 

query_cache_min_res_unit:

  • 內存塊的最小分配單位;緩存太小的查詢結果集會浪費內存空間;

  • 較小的值會減小空間浪費,但會致使更頻繁地內存分配及回收操做; 

  • 較大值的會帶來空間浪費;

query_cache_size:

  • 查詢緩存空間的總共可用的大小;單位是字節,必須是1024的整數倍;

query_cache_strip_comments

query_cache_type:

  • 緩存功能啓用與否;

  • ON:啓用;

  • OFF:禁用;

  • DEMAND:按需緩存,僅緩存SELECT語句中帶SQL_CACHE的查詢結果;

query_cache_wlock_invalidate:

  • 若是某表被其它鏈接鎖定,是否仍然能夠從查詢緩存中返回查詢結果;

  • 默認爲OFF,表示能夠;ON則表示不能夠;

----------------------------------------------------------------------------

注意:

  •  要想讓設定永久有效,要寫在配置文件當中

 3.統計緩存數據狀態變量:

查看命令:

  • mysql> SHOW GLOBAL STATUS LIKE 'Qcache%';

wKiom1g9O37yzIexAACDNZJhToQ348.png


命中率:

  • Qcache_hits/Com_select (命中次數/執行查詢語句的次數) 

查看SELECT語句的執行次數

  • mysql> SHOW STATUS LIKE 'Com_select '

演示:

 1.查詢緩存相關的服務器變量

MariaDB [(none)]> SHOW VARIABLES LIKE '%query_cache%';
+------------------------------+---------+
| Variable_name                | Value   |
+------------------------------+---------+
| have_query_cache             | YES     | //是否擁有緩存功能
| query_cache_limit            | 1048576 | //單語句結果集上限,字節爲單位,默認大小爲1M
| query_cache_min_res_unit     | 4096    | //內存塊的最小分配單位
| query_cache_size             | 0       |
| query_cache_strip_comments   | OFF     |
| query_cache_type             | ON      | //緩存功能是否啓用
| query_cache_wlock_invalidate | OFF     |
+------------------------------+---------+

 2.設定緩存空間大小,並執行SELECT語句,查看緩存命中率等統計參數

# 設定緩存空間大小爲10M
MariaDB [(none)]> SET GLOBAL query_cache_size=10485760;  
Query OK, 0 rows affected (0.00 sec)

MariaDB [(none)]> SHOW VARIABLES LIKE '%query_cache%';
+------------------------------+----------+
| Variable_name                | Value    |
+------------------------------+----------+
| have_query_cache             | YES      |
| query_cache_limit            | 1048576  |
| query_cache_min_res_unit     | 4096     |
| query_cache_size             | 10485760 | # 設置成功
| query_cache_strip_comments   | OFF      |
| query_cache_type             | ON       |
| query_cache_wlock_invalidate | OFF      |
+------------------------------+----------+
7 rows in set (0.01 sec)

MariaDB [(none)]> USE hellodb;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Database changed
MariaDB [hellodb]> SHOW STATUS LIKE 'Qcache%'; # 查看緩存統計變量
+-------------------------+----------+
| Variable_name           | Value    |
+-------------------------+----------+
| Qcache_free_blocks      | 1        |
| Qcache_free_memory      | 10468296 |
| Qcache_hits             | 0        |
| Qcache_inserts          | 0        |
| Qcache_lowmem_prunes    | 0        |
| Qcache_not_cached       | 1        |
| Qcache_queries_in_cache | 0        |
| Qcache_total_blocks     | 1        |
+-------------------------+----------+
8 rows in set (0.01 sec)

# 執行SELECT語句3次
MariaDB [hellodb]> SELECT Name,Age FROM students WHERE Age >= 20;
+---------------+-----+
| Name          | Age |
+---------------+-----+
| Shi Zhongyu   |  22 |
| Shi Potian    |  22 |
| Xie Yanke     |  53 |
| Ding Dian     |  32 |
| Yu Yutong     |  26 |
| Shi Qing      |  46 |
| Ren Yingying  |  20 |
| Yuan Chengzhi |  23 |
| Tian Boguang  |  33 |
| Duan Yu       |  21 |
| Xu Zhu        |  21 |
| Lin Chong     |  25 |
| Hua Rong      |  23 |
| Huang Yueying |  22 |
| Xiao Qiao     |  20 |
| Ma Chao       |  23 |
| Xu Xian       |  27 |
| Sun Dasheng   | 100 |
+---------------+-----+
18 rows in set (0.00 sec)

MariaDB [hellodb]> SHOW STATUS LIKE 'Qcache%';
+-------------------------+----------+
| Variable_name           | Value    |
+-------------------------+----------+
| Qcache_free_blocks      | 1        |
| Qcache_free_memory      | 10466752 |
| Qcache_hits             | 2        | # 緩存命中2次
| Qcache_inserts          | 1        | # 插入了一個緩存語句
| Qcache_lowmem_prunes    | 0        |
| Qcache_not_cached       | 1        |
| Qcache_queries_in_cache | 1        |
| Qcache_total_blocks     | 4        |
+-------------------------+----------+
8 rows in set (0.00 sec)

# 查詢執行SELECT語句的次數
MariaDB [hellodb]> SHOW STATUS LIKE 'Com_select';  
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| Com_select    | 5     |
+---------------+-------+
1 row in set (0.00 sec)

 3.查看各類語句的執行次數,不能修改,能夠置零

MariaDB [(none)]> SHOW GLOBAL STATUS LIKE 'Com_%';
+----------------------------+-------+
| Variable_name              | Value |
+----------------------------+-------+
| Com_admin_commands         | 0     |
| Com_alter_db               | 0     |
| Com_alter_db_upgrade       | 0     |
| Com_alter_event            | 0     |
| Com_alter_function         | 0     |
| Com_alter_procedure        | 0     |
| Com_alter_server           | 0     |
| Com_alter_table            | 0     |
| Com_alter_tablespace       | 0     |
| Com_analyze                | 0     |
| Com_assign_to_keycache     | 0     |
| Com_begin                  | 157   |
| Com_binlog                 | 0     |
| Com_call_procedure         | 0     |
| Com_change_db              | 5     |
| Com_change_master          | 0     |
| Com_check                  | 0     |
| Com_checksum               | 0     |
| Com_commit                 | 157   |
| Com_create_db              | 0     |
| Com_create_event           | 0     |
| Com_create_function        | 0     |
| Com_create_index           | 0     |
| Com_create_procedure       | 0     |
| Com_create_server          | 0     |
| Com_create_table           | 0     |
| Com_create_trigger         | 0     |
| Com_create_udf             | 0     |
| Com_create_user            | 2     |
| Com_create_view            | 0     |
| Com_dealloc_sql            | 0     |
| Com_delete                 | 0     |
| Com_delete_multi           | 0     |
| Com_do                     | 0     |
| Com_drop_db                | 0     |
| Com_drop_event             | 0     |
| Com_drop_function          | 0     |
| Com_drop_index             | 0     |
| Com_drop_procedure         | 0     |
| Com_drop_server            | 0     |
| Com_drop_table             | 0     |
| Com_drop_trigger           | 0     |
| Com_drop_user              | 0     |
| Com_drop_view              | 0     |
| Com_empty_query            | 0     |
| Com_execute_sql            | 0     |
| Com_flush                  | 1     |
| Com_grant                  | 6     |
| Com_ha_close               | 0     |
| Com_ha_open                | 0     |
| Com_ha_read                | 0     |
| Com_help                   | 0     |
| Com_insert                 | 207   |
| Com_insert_select          | 0     |
| Com_install_plugin         | 0     |
| Com_kill                   | 0     |
| Com_load                   | 0     |
| Com_lock_tables            | 0     |
| Com_optimize               | 0     |
| Com_preload_keys           | 0     |
| Com_prepare_sql            | 0     |
| Com_purge                  | 0     |
| Com_purge_before_date      | 0     |
| Com_release_savepoint      | 0     |
| Com_rename_table           | 0     |
| Com_rename_user            | 0     |
| Com_repair                 | 0     |
| Com_replace                | 0     |
| Com_replace_select         | 0     |
| Com_reset                  | 0     |
| Com_resignal               | 0     |
| Com_revoke                 | 1     |
| Com_revoke_all             | 0     |
| Com_rollback               | 0     |
| Com_rollback_to_savepoint  | 0     |
| Com_savepoint              | 0     |
| Com_select                 | 27    |
| Com_set_option             | 2     |
| Com_show_authors           | 0     |
| Com_show_binlog_events     | 0     |
| Com_show_binlogs           | 0     |
| Com_show_charsets          | 0     |
| Com_show_client_statistics | 0     |
| Com_show_collations        | 0     |
| Com_show_contributors      | 0     |
| Com_show_create_db         | 0     |
| Com_show_create_event      | 0     |
| Com_show_create_func       | 0     |
| Com_show_create_proc       | 0     |
| Com_show_create_table      | 0     |
| Com_show_create_trigger    | 0     |
| Com_show_databases         | 9     |
| Com_show_engine_logs       | 0     |
| Com_show_engine_mutex      | 0     |
| Com_show_engine_status     | 0     |
| Com_show_errors            | 0     |
| Com_show_events            | 0     |
| Com_show_fields            | 37    |
| Com_show_function_status   | 0     |
| Com_show_grants            | 9     |
| Com_show_index_statistics  | 0     |
| Com_show_keys              | 0     |
| Com_show_master_status     | 0     |
| Com_show_open_tables       | 0     |
| Com_show_plugins           | 0     |
| Com_show_privileges        | 0     |
| Com_show_procedure_status  | 0     |
| Com_show_processlist       | 0     |
| Com_show_profile           | 0     |
| Com_show_profiles          | 0     |
| Com_show_relaylog_events   | 0     |
| Com_show_slave_hosts       | 0     |
| Com_show_slave_status      | 0     |
| Com_show_status            | 4     |
| Com_show_storage_engines   | 0     |
| Com_show_table_statistics  | 0     |
| Com_show_table_status      | 0     |
| Com_show_tables            | 7     |
| Com_show_triggers          | 0     |
| Com_show_user_statistics   | 0     |
| Com_show_variables         | 3     |
| Com_show_warnings          | 0     |
| Com_signal                 | 0     |
| Com_slave_start            | 0     |
| Com_slave_stop             | 0     |
| Com_stmt_close             | 0     |
| Com_stmt_execute           | 0     |
| Com_stmt_fetch             | 0     |
| Com_stmt_prepare           | 0     |
| Com_stmt_reprepare         | 0     |
| Com_stmt_reset             | 0     |
| Com_stmt_send_long_data    | 0     |
| Com_truncate               | 0     |
| Com_uninstall_plugin       | 0     |
| Com_unlock_tables          | 0     |
| Com_update                 | 2     |
| Com_update_multi           | 0     |
| Com_xa_commit              | 0     |
| Com_xa_end                 | 0     |
| Com_xa_prepare             | 0     |
| Com_xa_recover             | 0     |
| Com_xa_rollback            | 0     |
| Com_xa_start               | 0     |
| Compression                | OFF   |
+----------------------------+-------+
144 rows in set (0.00 sec)
相關文章
相關標籤/搜索