您的位置:首页 > 数据库 > MySQL

MySQL用户、[表库字段]权限、管理及查询缓存详解

2015-09-11 13:09 921 查看
MySQL用户管理:
mysql> help contents;
You asked for help about help category: "Contents"
For more information, type 'help <item>', where <item> is one of the following
categories:
Account Management
Administration
Compound Statements
Data Definition
Data Manipulation
Data Types
Functions
Functions and Modifiers for Use with GROUP BY
Geographic Features
Help Metadata
Language Structure
Plugins
Procedures
Storage Engines
Table Maintenance
Transactions
User-Defined Functions
Utility
*************
mysql> help Account Management;
You asked for help about help category: "Account Management"
For more information, type 'help <item>', where <item> is one of the following
topics:
CREATE USER
DROP USER
GRANT
RENAME USER
REVOKE
SET PASSWORD

用户帐号:username@hostname, password

用户帐号管理:
CREATE USER
DROP UESER
RENAME USER 修改用户名称
SET PASSWORD

权限管理:
GRANT 如果用户不存在 ,可以同时创建用户
REVOKE

【CREATE USER】

CREATE USER username@hostname
[
IDENTIFIED BY [PASSWORD] 'password'
]

主机也可使用通配符:
%:
_:

testuser@'172.16.100.1__' 这就是下面的地址范围
172.16.100.100-172.16.100.199
【创建testuser 用户,限制在192.168.6.0/24 网段内,密码是testpass】

【查看用户能够使用的权限】:SHOW GRANTS FOR username@'hostname'
mysql> SHOW GRANTS FOR 'testuser'@'192.168.6.%' \G
*************************** 1. row ***************************
Grants for testuser@192.168.6.%: GRANT USAGE ON *.* TO 'testuser'@'192.168.6.%' IDENTIFIED BY PASSWORD <secret>
1 row in set (0.00 sec)
mysql> create user testuser@'192.168.6.%' identified by 'testpass';
Query OK, 0 rows affected (0.00 sec)

【通知重读授权表】
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)


【打开一个终端2,用testuser 远程访问mysql服务器192.168.6.177】
[root@pc0003 ~]# mysql -u testuser -h 192.168.6.177 -p
Enter password:


【终端2此时什么库也看不到】
mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| test               |
+--------------------+
2 rows in set (0.02 sec)


【 information_schema】
mysql> show tables from  information_schema;


mysql共享信息,关机就清空,相当于linux的/proc

【终端2 尝试创建一个库,提示没有权限】
mysql> create database testdb;
ERROR 1044 (42000): Access denied for user 'testuser'@'192.168.6.%' to database 'testdb'


【Ranme User】终端2自己没有权限给自己改名
mysql> rename user 'testuser'@'192.168.6.%' to 'tuser'@'192.168.6.%';
ERROR 1227 (42000): Access denied; you need (at least one of) the CREATE USER privilege(s) for this operation


【给有权限的用户来操作】
mysql> rename user 'testuser'@'192.168.6.%' to 'tuser'@'192.168.6.%';
Query OK, 0 rows affected (0.01 sec)


【!!!!!!!!!】
此时终端2没有任何变化,必须退出重新登录才能或得更改的效果。

【终端2 以新的用户名,旧的密码就可以登录了】
[root@pc0003 ~]# mysql -u tuser -h 192.168.6.177 -p
Enter password:


【修改密码】mysql> help set password;
SET PASSWORD FOR 'bob'@'%.example.org' = PASSWORD('cleartext password');


【授权】mysql> help grant;

【显示服务器当前所有线程】
mysql> show processlist \G
*************************** 1. row ***************************
Id: 1
User: root
Host: localhost
db: NULL
Command: Query
Time: 0
State: NULL
Info: show processlist
*************************** 2. row ***************************
Id: 7
User: tuser
Host: 192.168.6.177:52151
db: NULL
Command: Sleep
Time: 12
State:
Info: NULL
2 rows in set (0.00 sec)
【查看谁在登录我的mysql】
mysql> use hellodb;
Database changed
mysql> lock tables classes read;
Query OK, 0 rows affected (0.01 sec)
mysql> show processlist \G
*************************** 1. row ***************************
Id: 1
User: root
Host: localhost
db: hellodb
Command: Query
Time: 0
State: NULL
Info: show processlist
*************************** 2. row ***************************
Id: 7
User: tuser
Host: 192.168.6.177:52151
db: NULL
Command: Sleep
Time: 165
State:
Info: NULL
2 rows in set (0.00 sec)


【MySQL的权限类别:】
库级别
表级别
字段级别
管理类
程序类

管理类权限:
CREATE TEMPORARY TABLES
CREATE USER
FILE
SUPER
SHOW DATABASES
RELOAD
SHUTDOWN
REPLICATION SLAVE
REPLICATION CLIENT
LOCK TABLES
PROCESS

库级别和表级别:
ALTER
ALTER ROUTINE
CREATE
CREATE ROUTINE
CREATE VIEW
DROP
EXECUTE
GRNAT OPTION
INDEX
SHOW VIEW

数据操作(表级别):
SELECT
INSERT
UPDATE
DELETE

字段级别:
SELECT(col1,...)
UPDATE(col1,...)
INSERT(col1,...)

所有权限:
ALL [PRIVILEGES]

GRANT ALL ON [FUNCTION] *.*
GRANT priv_type [(column_list)]
[, priv_type [(column_list)]] ...
ON [TABLE|FUNCTION|PROCEDURE] priv_level
TO username@hostname [IDENTIFIED BY 'password'], [username@hostname [],...]
[REQUIRE SSL]
[WITH with_option ...]
priv_level:
*
| *.*
| db_name.*
| db_name.tbl_name
| tbl_name
| db_name.routine_name
with_option:
GRANT OPTION
| MAX_QUERIES_PER_HOUR count
| MAX_UPDATES_PER_HOUR count
| MAX_CONNECTIONS_PER_HOUR count
| MAX_USER_CONNECTIONS count


【授予tuser 创建 testdb库的tb1表 】
mysql> grant create on testdb.tb1 to 'tuser'@'192.168.6.%';
Query OK, 0 rows affected (0.04 sec)
【终端2 不用退出,就可以查看自己拥有的权限】
mysql> SHOW GRANTS FOR 'tuser'@'192.168.6.%'\G
*************************** 1. row ***************************
Grants for tuser@192.168.6.%: GRANT USAGE ON *.* TO 'tuser'@'192.168.6.%' IDENTIFIED BY PASSWORD <secret>
*************************** 2. row ***************************
Grants for tuser@192.168.6.%: GRANT CREATE ON `testdb`.`tb1` TO 'tuser'@'192.168.6.%'
2 rows in set (0.00 sec)
【终端2 create database testdb 失败!】
mysql> create database testdb;
ERROR 1044 (42000): Access denied for user 'tuser'@'192.168.6.%' to database 'testdb'
【grant create on testdb.*】
mysql> grant create on testdb.* to 'tuser'@'192.168.6.%';
Query OK, 0 rows affected (0.01 sec)
【终端2 create database testdb 正常!】
查看自己的权限,就多了一行
mysql> SHOW GRANTS FOR 'tuser'@'192.168.6.%'\G
*************************** 1. row ***************************
Grants for tuser@192.168.6.%: GRANT USAGE ON *.* TO 'tuser'@'192.168.6.%' IDENTIFIED BY PASSWORD <secret>
*************************** 2. row ***************************
Grants for tuser@192.168.6.%: GRANT CREATE ON `testdb`.* TO 'tuser'@'192.168.6.%'
*************************** 3. row ***************************
Grants for tuser@192.168.6.%: GRANT CREATE ON `testdb`.`tb1` TO 'tuser'@'192.168.6.%'
3 rows in set (0.00 sec)
【此时终端2 tuser 只有一个创建这个表的权限,没有desc,insert,drop,select 权限】
mysql> create database testdb;
Query OK, 1 row affected (0.03 sec)
mysql> use testdb;
Database changed
mysql> create table t1 (ID tinyint unsigned);
Query OK, 0 rows affected (0.14 sec)
mysql> desc t1;
ERROR 1142 (42000): SELECT command denied to user 'tuser'@'192.168.6.177' for table 't1'
mysql> insert into t1 value(1);
ERROR 1142 (42000): INSERT command denied to user 'tuser'@'192.168.6.177' for table 't1'
mysql> drop table t1;
ERROR 1142 (42000): DROP command denied to user 'tuser'@'192.168.6.177' for table 't1'
mysql> select * from t1;
ERROR 1142 (42000): SELECT command denied to user 'tuser'@'192.168.6.177' for table 't1'


【赋予drop权限】
mysql> grant drop on testdb.* to 'tuser'@'192.168.6.%';
Query OK, 0 rows affected (0.00 sec)
【终端2 查看自己的权限 ,会多出一点,并不是覆盖,而是累加】
需要退出mysql再次登录,重新认证就可以使用这个权限
或者 通知改变授权表也可以
mysql> SHOW GRANTS FOR 'tuser'@'192.168.6.%'\G
Grants for tuser@192.168.6.%: GRANT CREATE, DROP ON `testdb`.* TO 'tuser'@'192.168.6.%'
收回授权:
REVOKE
priv_type [(column_list)]
[, priv_type [(column_list)]] ...
ON [object_type] priv_level
FROM user [, user] ...

REVOKE ALL PRIVILEGES, GRANT OPTION
FROM user [, user] ...

几个跟用户授权相关的表:
db: 库级别权限;
host: 主机级别权限,已废弃
tables_priv: 表级别权限
colomns_priv:列级别的权限
procs_priv:存储过程和存储函数相关的权限
proxies_priv:代理用户权限

练习:
1、授予testuser能够通过172.16.0.0/16网络内的任意主机访问当前mysql服务器的权限;
2、让此用户能够创建及删除testdb数据库,及库中的表;
3、让此用户能够在testdb库中的t1表中执行查询、删除、更新和插入操作;
4、让此用户能够在testdb库上执行创建和删除索引;
5、让此用户能够在testdb.t2表上查询id和name字段,并允许其将此权限转授予其他用户;

【面试】【面试】【面试】【面试】【面试】
【面试】【面试】【面试】【面试】【面试】
【面试】【面试】【面试】【面试】【面试】

MySQL查询缓存

用于保存MySQL查询语句返回的完整结果。被命中时,MySQL会立即返回结果,省去解析、优化和执行等阶段。

如何检查缓存?
MySQL保存结果于缓存中:
把SELECT语句本身做hash计算,计算的结果作为key,查询结果作为value

什么样的语句不会被缓存?
查询语句中有一些不确定数据时,不会缓存:例如NOW(), CURRENT_TIME();一般来说,如果查询中包含用户自定义函数、存储函数、用户变量、临时表、mysql库中系统表、或者任何包含权限的表,一般都不会缓存;

缓存会带来额外开销:
1、每个查询都得先检查是否命中;
2、查询结果要先缓存;

MariaDB [(none)]> SHOW GLOBAL VARIABLES LIKE 'query_cache%';
+------------------------------+----------+
| Variable_name                | Value    |
+------------------------------+----------+
| query_cache_limit            | 1048576  |
| query_cache_min_res_unit     | 4096     |
| query_cache_size             | 16777216 |
| query_cache_strip_comments   | OFF      |
| query_cache_type             | ON       |
| query_cache_wlock_invalidate | OFF      |
+------------------------------+----------+
query_cache_type: 查询缓存类型;是否开启缓存功能,开启方式有三种{ON|OFF|DEMAND};
DEMAND:意味着SELECT语句明确使用 SQL_CACHE 选项时才会缓存;

query_cache_size: 总空间,单位为字节,大小必须是1024的整数倍。MySQL启动时,会一次分配并立即初始化这里指定大小的内存空间;这意味着,如果修改此大小,会清空缓存并重新初始化的。

query_cache_min_res_unit: 存储缓存的最小内存块;(query_cache_size-Qcache_free_memory)/Qcache_queries_in_cache能够获得一个理想的值。

query_cache_limit: 单个缓存对象的最大值,超出时则不预缓存;手动使用SQL_NO_CACHE可以人为地避免尝试缓存返回结果超出此参数限定值的语句。

query_cache_wlock_invalidate: 如果某个表被其它用户连接锁住了,是否仍然从缓存中返回结果。OFF表示返回。

如何判断命令率:
MariaDB [hellodb]> SHOW GLOBAL STATUS LIKE 'Qcache%';
+-------------------------+----------+
| Variable_name           | Value    |
+-------------------------+----------+
| Qcache_free_blocks      | 1        |
| Qcache_free_memory      | 16757008 |
| Qcache_hits             | 4        |
| Qcache_inserts          | 2        |
| Qcache_lowmem_prunes    | 0        |
| Qcache_not_cached       | 18       |
| Qcache_queries_in_cache | 2        |
| Qcache_total_blocks     | 6        |
+-------------------------+----------+
碎片整理:FLUSH QUERY_CACHE
清空缓存:RESET QUERY_CACHE

计算命中率:
MariaDB [hellodb]> SHOW GLOBAL STATUS WHERE Variable_name='Qcache_hits' OR Variable_name='Com_select';
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| Com_select    | 24    |
| Qcache_hits   | 4     |
+---------------+-------+
Qcache_hits/(Com_select+Qcache_hits)
也应该参考另外一个指标:命中和写入的比率,即Qcache_hits/Qcache_inserts的值,此比值如果能大于3:1,则表明缓存也是有效的。能达到10:1,为比较理想的情况。

缓存优化使用思路:
1、批量写入而非多次单个写入;
2、缓存空间不宜过大,因为大量缓存同时失效时会导致服务器假死;
3、必要时,使用SQL_CACHE和SQL_N0_CACHE手动控制缓存;
4、对写密集型的应用场景来说,禁用缓存反而能提高性能;

【面试】【面试】【面试】【面试】【面试】
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息