大数据兼云计算(王明龙)讲师-MYSQL-DAY13-权限管理

**

权限控制

**

mysql 库控制所有库和表:为超级管理员 

# mysql -u root -p密码

mysql> show databases;      #查看数据所有库
+--------------------+
| Database      |
+--------------------+
| information_schema |
| mysql       |
| test        |
+--------------------+
3 rows in set (0.00 sec)

mysql> use mysql;          #登陆超级数据库

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        |
| servers          |
| slow_log         |
| tables_priv        |
| time_zone         |
| time_zone_leap_second   |
| time_zone_name      |
| time_zone_transition   |
| time_zone_transition_type |
| user           |
+---------------------------+
23 rows in set (0.00 sec)


mysql所有库权限由以下5个表控制(但我们主要管理前三个表)

1. user                 # 控制用户权限,select 为 Y,超级权限
2. db                   # 控制数据库权限,select 为 Y,控制指定数据库
3. host                # 控制访问主机,使用此控制要求db表中select必须为Y,Host字段为空
4. tables_priv      # 控制表权限,select 为 Y,控制本身表
5. columns_priv  # 控制列权限,select 为 Y,控制本身列


1. user表 
------------------------------------------------------------------------------------------------------------------------------
按制用户登陆权限
mysql> use mysql;         
mysql> select * from user \G 或 select * from mysql.user \G            #\G把表竖起查看,有几个row就有几个记录
                                 数据库.表名

*************************** 1. row ***************************
       Host: localhost          #允许主机
       User: root                 #允许用户
     Password:                   #密码
        Select_priv: Y         #超级select,其本身含意检索表中的记录
         Insert_priv: Y         #向表中插入新行 
        Update_priv: Y         #修改现存表记录
        Delete_priv: Y         #删除表中已有的记录
        Create_priv: Y         #创建数据库和表
         Drop_priv: Y         #抛弃(删除)数据库和表 
        Reload_priv: Y         #重载授权表或清空日志、主机缓存或表缓存
      Shutdown_priv: Y         #关闭服务器
        Process_priv: Y         #查看服务器中执行的线程信息或杀死线程
           File_priv: Y         #读或写服务器上的文件
         Grant_priv: Y
     References_priv: Y         #未用
         Index_priv: Y         #创建或抛弃索引
          Alter_priv: Y         #修改表和索引
注意:只要user表中的select的权限是Y,当前表与此库所有表的权限都拥有,无须查看其它表


2. db表
--------------------------------------------------------------------------------------------------------------------------------

mysql> select * from db \G   或  select * from mysql.db \G

*************************** 1. row ***************************
         Host: %        #host字段为空时,host表才可做限制,此%代表所有主机和IP
          Db: test      #数据名
         User:         #用户
     Select_priv: Y        #控制开关


3. host表                       使用此控制要求db表中select必须为Y,Host字段为空
---------------------------------------------------------------------------------------------------------------------------------
mysql> select * from host \G  或  select * from mysql.host \G       
Empty set (0.00 sec)

此表为空,但我是要看它的字段,执行下面操作

mysql> desc host \G 或 desc mysql.host \G          #desc 库名.表名 \G

*************************** 1. row ***************************
  Field: Host
  Type: char(60)
   Null: NO
   Key: PRI
Default: 
  Extra: 
*************************** 2. row ***************************
  Field: Db
  Type: char(64)
   Null: NO
   Key: PRI
Default: 
  Extra: 
*************************** 3. row ***************************
  Field: Select_priv
  Type: enum('N','Y')
   Null: NO
   Key: 
Default: N
  Extra: 



权限设定实操

环境
    mysql-server  192.168.0.100
    mysql-client  192.168.0.103

1. 添加数据库用户     

• mysql-server

mysql> select * from mysql.user \G
*************************** 4. row ***************************
         Host: localhost
         User: 
     Password:

添加用户        #修改Host,User,Password字段

下面操作,在mysql5.1以上版本不能直接使用
mysql> insert into user(host,user,password) values('192.168.0.103','wml',password('123'));         诠释: 向user表3个字段host,user,password分别插入ip,用户名,密码,此ip是远程访问的主机和用户

解决方法:ssl_cipher,x509_issuer,x509_subject 这三列不能为空,给任意值即可,否则报错
insert into user(host,user,password,ssl_cipher,x509_issuer,x509_subject) values('192.168.0.103','wml',password('123'),'blob','blob','blob'); 

insert into user(host,user,password,ssl_cipher,x509_issuer,x509_subject) values('192.168.28.100','wml',password('123'),'1','2','3'); 

最好用的授权方法
grant all privileges on *.* to root@"%" identified by "mysql" WITH grant option;



查看是否添加成功

mysql> select * from mysql.user \G
*************************** 6. row ***************************
         Host: 192.168.0.103
         User: wml
     Password: *23AE809DDACAF96AF0FD78ED04B6A265E05AA257
    Select_priv: N           # 新添加用户,默认权限都是NO

刷新用户权限记录 

mysql> flush privileges;   或退出,到命令行下输入命令 # mysqladmin flush-privilege -u root -p密码
Query OK, 0 rows affected (0.01 sec)


测试准备                 建db1库,下面在建t1表,插入1记录

mysql> create database db1;
Query OK, 1 row affected (0.01 sec)

mysql> use db1;
Database changed

mysql> create table t1(id int);
Query OK, 0 rows affected (0.03 sec)

mysql> insert into t1 set id=1;
Query OK, 1 row affected (0.01 sec)

查看一下,都正常

mysql> show databases;
+--------------------+
| Database      |
+--------------------+
| information_schema |
| db1        |
| mysql       |
| test        |
+--------------------+
4 rows in set (0.00 sec)

mysql> select * from t1;
+------+
| id  |
+------+
|  1 |
+------+
1 row in set (0.00 sec)



远程测试

• mysql-client

# yum -y install mysql   #安装mysql客户端
# mysql -h 192.168.0.100 -u wml -p123     
mysql> show databases;  #查看一下数据库,只能看到没用的,我们建的也没看到,所以要赋予权限
+--------------------+
| Database      |
+--------------------+
| information_schema |
| test        |
+--------------------+
2 rows in set (0.00 sec)


2. 给用户增加所有权限

• mysql-sever

mysql> use mysql;
mysql> select * from user \G

*************************** 6. row ***************************
         Host: 192.168.0.103
         User: wml
     Password: *23AE809DDACAF96AF0FD78ED04B6A265E05AA257
    Select_priv: N   #为N状态

mysql> update user set select_priv="Y";


mysql> select * from user \G

*************************** 6. row ***************************
         Host: 192.168.0.103
         User: wml
     Password: *23AE809DDACAF96AF0FD78ED04B6A265E05AA257
    Select_priv: Y   #为Y状态

mysql> flush privileges;       #刷新用户权限记录 


远程测试

• mysql-client

# mysql -h 192.168.0.100 -u wml -p123     #如查你处于已登陆状态,请退出,重新进入
mysql> show databases;
+--------------------+
| Database      |
+--------------------+
| information_schema |
| db1        |
| mysql       |
| test        |
+--------------------+
4 rows in set (0.01 sec)

mysql> use db1;

mysql> show tables;
+---------------+
| Tables_in_db1 |
+---------------+
| t1      |
+---------------+
1 row in set (0.01 sec)

mysql> select * from t1;
+------+
| id  |
+------+
|  1 |
+------+
1 row in set (0.00 sec)

上面可以看连mysql管理库都能看见,所以我们要改成对指定库有权限


3. 限制对指定数据库有权限

• mysql-sever

关掉超级权限

mysql> use mysql
mysql> update user set select_priv="N" where user="wml";
mysql> flush privileges;



• mysql-client

重新登陆一下

# mysql -h 192.168.0.100 -u wml -p123          或    mysql -uroot -pmysql -A test
mysql> show databases;   #查看不到mysql和db1数据库了
+--------------------+
| Database      |
+--------------------+
| information_schema |
| test        |
+--------------------+
2 rows in set (0.00 sec)



• mysql-server

限制操作指定库

mysql> use mysql;
mysql> select * from db \G
*************************** 1. row ***************************
         Host: %
          Db: test
         User: 
    Select_priv: Y

修改Host,Db,User, Select_priv字段

制定wml数据库用户指定访问db1数据库
mysql> insert into db(host,db,user,select_priv) values('192.168.0.103','db1','wml','Y');    诠释:给db表host,db,user,select_priv插入192.168.0.103','db1','wml','Y'值
Query OK, 1 row affected (0.01 sec)

mysql> flush privileges;              #刷新下记录
Query OK, 0 rows affected (0.00 sec)

mysql> select * from db \G
*************************** 3. row ***************************
         Host: 192.168.0.100
          Db: db1
         User: wml
    Select_priv: Y


远程测试

• mysql-client

# mysql -h 192.168.0.100 -u wml -p123
mysql> show databases;    #只能查看db1
+--------------------+
| Database      |
+--------------------+
| information_schema |
| db1        |
| test        |
+--------------------+
3 rows in set (0.00 sec)

mysql> use db1;
mysql> show tables;
+---------------+
| Tables_in_db1 |
+---------------+
| t1      |
+---------------+
1 row in set (0.00 sec)

mysql> select * from t1;
+------+
| id  |
+------+
|  1 |
+------+
1 row in set (0.00 sec)


以下几种表操作基本相同




四. 添加用户

二种方法
1.grant
2.revoke


1.grant
-----------------------------------------------------------------------------------------------------------------------------------------------------------------
手动创建与添加用户和设置权限

语法: 
grant 权限 on 数据库.表名 to 用户名@'可登陆服务器IP' identified by'密码';    #密码不加密,但系统会默认会加密
grant 权限 on 数据库.表名 to 用户名@'可登陆服务器IP' identified by 'password ('密码')';    #密码加密
grant 权限 privileges on 数据库.表名 to 用户名@'可登陆服务器IP' identified by'密码';   #密码不加密,但系统会默认会加密
grant 权限 privileges on 数据库.表名 to 用户名@'可登陆服务器IP' identified by 'password ('密码')';    #密码加密

privileges 就是特权的意思

mysql> grant select,insert on mysql.user to wang@'192.168.0.100' identified by '123';
mysql> grant select,insert on mysql.user to wml@'192.168.0.100' identified by 'password ('123')';
mysql> grant select(host),insert on mysql.user to ming@'192.168.0.100' identified by'123';   #host 是只对host表有权限
mysql> grant all on db.t1 to long@'192.168.0.100' identified by'123';                  #all代表所有权限,不绝对
mysql> grant all on *.* to wt@'192.168.0.100' identified by'123';    #仅有Grant_priv授权权限没有,其它所有权限都有

mysql> select * from user \G 
*************************** 10. row ***************************
         Host: 192.168.0.100
         User: wt
       Password: *23AE809DDACAF96AF0FD78ED04B6A265E05AA257
     Select_priv: Y
     Insert_priv: Y
     Update_priv: Y
     Delete_priv: Y
     Create_priv: Y
      Drop_priv: Y
     Reload_priv: Y
    Shutdown_priv: Y
     Process_priv: Y
      File_priv: Y
      Grant_priv: N     只是授权没有权限
   References_priv: Y
      Index_priv: Y
      Alter_priv: Y
     Show_db_priv: Y
      Super_priv: Y
Create_tmp_table_priv: Y
   Lock_tables_priv: Y
     Execute_priv: Y
   Repl_slave_priv: Y
   Repl_client_priv: Y
   Create_view_priv: Y
    Show_view_priv: Y
 Create_routine_priv: Y
  Alter_routine_priv: Y
   Create_user_priv: Y
      Event_priv: Y
     Trigger_priv: Y
       ssl_type: 
      ssl_cipher: 
     x509_issuer: 
     x509_subject: 
    max_questions: 0
     max_updates: 0
   max_connections: 0
 max_user_connections: 0
10 rows in set (0.00 sec)


用户管理

------------
添加用户
------------
1.create user 语法
create user 用户名 identified by '密码';

2.grant 语法
grant 权限 on 数据库对象 to 用户@'主机范围' identified by "密码";

3.insert into 语法
insert into mysql.user(host,user,password) values('主机范围','用户名',password('密码'));   //其实就是向系统表里插入

------------
查看用户
------------
1.查看数据库中所有用户
select distinct concat('User: ''',user,'''@''',host,''';') as query from mysql.user;

2.查看数据库中某个用户权限
show grants for 用户名;
select * from mysql.user where user='用户名' \G;

------------
删除用户
------------
drop user 用户名@'主机IP';
delete from mysql.user where user = '用户名';



权限管理

------------
授予权限
------------

grant-全语法

GRANT priv_type [(column_list)] [, priv_type [(column_list)]] ...
      ON [object_type] {tbl_name | * | *.* | db_name.*}
      TO user [IDENTIFIED BY [PASSWORD] 'password']
            [, user [IDENTIFIED BY [PASSWORD] 'password']] ...
      [REQUIRE
        NONE |
        [{SSL| X509}]
        [CIPHER 'cipher' [AND]]
        [ISSUER 'issuer' [AND]]
        [SUBJECT 'subject']]
    [WITH with_option [with_option] ...]

object_type=
    TABLE
  | FUNCTION
  | PROCEDURE
with_option=
    GRANT OPTION
  | MAX_QUERIES_PER_HOUR count
  | MAX_UPDATES_PER_HOUR count
  | MAX_CONNECTIONS_PER_HOUR count
  | MAX_USER_CONNECTIONS count


1.全局级
语法:grant 权限 on 数据库 to 用户名@'主机';

举例:
grant all privileges on *.* to 用户名@'%';                            //给用户所有权限,唯 grant 没有
grant all privileges on *.* to 用户名@'%' with grant option;   //给用户所有权限,包括 grant 权限

2.数据库级
语法:grant 权限 on 库名.* to 用户名@'主机范围';

举例:
grant all on test.* to wml@'%';

3.表级
语法:grant 权限 on 库名.表名 to 用户名@'主机范围';

举例:
grant all on db1.tb1 to wml@'%';

4.字段级
语法:grant 权限(字段名) on 库名.表名 to 用户名@'主机范围';

举例
grant insert(name) on db2.tb1 to wml@'%';

5.子程序级
------------------------------------------------------------------------------------------------------
给用户执行存储过程权限
grant execute on procedure 数据库.存储过程名 to 用户名@'主机范围';

给用户执行函数权限
grant execute on function 数据库.函数名 to 用户名@'主机范围';
------------------------------------------------------------------------------------------------------


------------------
查看用户权限
------------------
show grants;
show grants for 用户名;

------------
撤销权限
------------

revoke

1.撤销字段级
revoke 权限(字段名) on 库名.表名 from 用户名@'主机范围';

2.撤销表级
revoke 权限 on 库名.表名 from 用户名@'主机范围';

3.撤销库级
revoke 权限 on 库名.* from 用户名@'主机范围';

4.撤销全局级
revoke all privileges,grant option from 用户名@'主机范围';
revoke all privileges,grant option from 用户1[,用户名2,...];


---------------------------------------
创建用户,同时赋予权限实例
---------------------------------------

给用户所有权限,唯grant权限没有
grant all on *.* to dba1@'192.168.0.10' identified by "dbapasswd";

给用户指定库权限
grant all privileges on testdb.* to dba1@'192.168.0.10' identified by "dbapasswd";
或
grant all on testdb.* to dba1@'192.168.0.10' identified by "dbapasswd";

给用户指定表权限
grant all on testdb.teable1 to dba1@'192.168.0.10' identified by "dbapasswd";

给用户指定列权限
grant select(id, se, rank) on testdb.table1 to ba1@'192.168.0.10' identified by "dbapasswd";

开放管理操作指令
grant select, insert, update, delete on testdb.* to dba1@'192.168.0.10' identified by "dbapasswd";

回收权限
revoke all on *.* from dba1@localhost;

查看用户权限
show grants;
show grants for 用户名;

grant all on *.* to dba1@'localhost' identified by "123";      //允许本地访问
grant all on *.* to dba1@'%' identified by "123";               //这里的%号,代表允许所有对外ip访问,但不包括本地


用户及权限管理:更多更详细实例

1.grant普通数据用户(test1),查询、插入、更新、删除 数据库(test)中所有表数据的权利。
grant select on test.* to test1@'%';
grant insert on test.* to test1@'%';
grant update on test.* to test1@'%';
grant delete on test.* to test1@'%';
或者,用一条 MySQL 命令来替代:
grant select, insert, update, delete on test.* to test1@'%';


2.grant数据库开发人员(duser),创建表、索引、视图、存储过程、函数。。。等权限。

grant创建、修改、删除 MySQL 数据表结构权限。
grant create on testdb.* to duser@'192.168.0.%';
grant alter on testdb.* to duser@'192.168.0.%';
grant drop on testdb.* to duser@'192.168.0.%';

grant 操作 MySQL 外键权限。
grant references on testdb.* to developer@'192.168.0.%';

grant 操作 MySQL 临时表权限。
grant create temporary tables on testdb.* to developer@'192.168.0.%';

grant 操作 MySQL 索引权限。
grant index on testdb.* to developer@'192.168.0.%';

grant 操作 MySQL 视图、查看视图源代码 权限。
grant create view on testdb.* to duser@'192.168.0.%';
grant show view on testdb.* to duser@'192.168.0.%';
grant select on testdb.* to duser@'192.168.0.%';

grant 操作 MySQL 存储过程、函数 权限。
grant create routine on testdb.* to duser@'192.168.0.%';
grant alter routine on testdb.* to duser@'192.168.0.%';
grant execute on testdb.* to duser@'192.168.0.%';

3.grant 普通DBA管理某个MySQL数据库(test)的权限。
grant all privileges on test to dba@'localhost'
其中,关键字 privileges 可以省略。

4.grant 高级 DBA 管理 MySQL 中所有数据库的权限
grant all on *.* to dba@'localhost'


5.MySQL grant 权限,分别可以作用在多个层次

grant 作用在整个 MySQL 服务器上:
grant select on *.* to dba@localhost; -- dba 可以查询 MySQL 中所有数据库中的表。
grant all on *.* to dba@localhost; -- dba 可以管理 MySQL 中的所有数据库

grant 作用在单个数据库上:
grant select on testdb.* to dba@localhost; -- dba 可以查询 testdb 中的表。

grant 作用在单个数据表上:
grant select, insert, update, delete on testdb.orders to dba@localhost;

grant 作用在表中的列上:
grant select(id, se, rank) on testdb.apache_log to dba@localhost;

grant 作用在存储过程、函数上:
grant execute on procedure testdb.pr_add to 'dba'@'localhost'
grant execute on function testdb.fn_add to 'dba'@'localhost'

6.查看用户权限
查看当前用户(自己)权限:
show grants;

查看其他 MySQL 用户权限:
show grants for dba@localhost;

7.撤销已经赋予给 MySQL 用户权限的权限
revoke 跟 grant 的语法差不多,只需要把关键字 to 换成 from 即可:
grant all on *.* todba@localhost;
revoke all on *.* from dba@localhost;

8.grant、revoke 用户权限注意事项
1. grant, revoke 用户权限后,该用户只有重新连接 MySQL 数据库,权限才能生效。
2. 如果想让授权的用户,也可以将这些权限 grant 给其他用户,需要选项 grant option
grant select on testdb.* to dba@localhost with grant option;
这个特性一般用不到。实际中,数据库权限最好由 DBA 来统一管理。

猜你喜欢

转载自blog.csdn.net/wangminglong1989/article/details/81562729
今日推荐