MySQL 权限控制细节分析

吾爱主题 阅读:137 2024-04-02 08:01:06 评论:0

   今天周天,早上懒了一会儿,起的有点儿晚,中午没事儿干,重新看了看mysql里面的权限控制模块,再次回头看,还是有很多收获的细节,这里记录一下,方便自己后续查看。
    关于权限部分的内容,之前3月11号的文章中有写过一些,今天的内容,我们使用一个一个的细节知识点来撰写(本文中所使用的mysql版本是5.7.16),在写这些知识点之前,我们首先介绍一下mysql的权限控制粒度、然后了解一下mysql中客户端发起请求的时候,服务端所做的核实工作,先来看权限控制粒度:

1、全局层级

    全局权限使用于给一个给定服务器中的所有数据库,这些权限存储在mysql.user表中,使用grant all on *.*的方法和revoke all on *.*的方法授予或者回收权限。

2、数据库层级

    数据库权限适用于一个给定数据库中的所有目标,包含表对象和存储过程,这些权限存储在mysql.db表中,使用grant all on db_name.*或者对应的revoke方法可以授予和回收数据库权限

3、表层级

   表权限适用于一个给定表中的所有列,这些权限存储在mysql的tables_priv表中,一般使用grant all on db_name.tbl_name和对应的revoke语句来授予或者撤销权限。

4、列层级的权限

    列层级的权限适用于一个给定表中的指定列,这些权限存储在mysql.columns_priv的表中,由于这个权限不常用,这里给出它的授权方法示例,如下:

首先我们创建一个用户,拥有yeyztest.test1这个表的select权限:

?
1 2 3 4 5 6 7 8 mysql:mysql 19:35:38>>show grants for dba_yeyz@ '192.168.18.%' ; + ------------------------------------------------------------------------------+ | grants for dba_yeyz@192.168.18.%                       | + ------------------------------------------------------------------------------+ | grant usage on *.* to 'dba_yeyz' @ '192.168.18.%'               | | grant select on `yeyztest`.`test1` to 'dba_yeyz' @ '192.168.18.%'        | + ------------------------------------------------------------------------------+ 2 rows in set (0.00 sec)

然后我们多test1中的一个字段id进行update操作,结果如下:

?
1 2 3 4 5 6 7 8 9 10 11 mysql> select * from test1; + ---------------+ | id      | + ---------------+ |      22 | | 3333333333333 | + ---------------+ 2 rows in set (0.00 sec)   mysql> update test1 set id=2 where id=22; error 1142 (42000): update command denied to user 'dba_yeyz' @ '192.168.18.**' for table 'test1'

当然,我们是不能进行update的,这个时候,我们使用root账号给这个dba_yeyz的账号一个id列的权限,然后再看它的结果:

?
1 2 3 4 5 6 7 8 9 mysql:mysql 19:38:38>>show grants for dba_yeyz@ '192.168.18.%' ; + ------------------------------------------------------------------------------+ | grants for dba_yeyz@192.168.18.%                       | + ------------------------------------------------------------------------------+ | grant usage on *.* to 'dba_yeyz' @ '192.168.18.%'               | | grant select on `yeyztest`.`test1` to 'dba_yeyz' @ '192.168.18.%'       | | grant select , update (id) on `yeyztest`.`test1` to 'dba_yeyz' @ '192.168.18.%' | + ------------------------------------------------------------------------------+ 3 rows in set (0.00 sec)

这里需要注意给字段添加权限的语句,也就是:

grant  update (id) on yeyztest.test1 to xxxxxx

也就是在权限后面跟上字段的名称。

这个时候,我们查询一下columns_priv的表,可以看到里面的记录是:

?
1 2 3 4 5 6 7 mysql:mysql 19:39:46>> select * from columns_priv; + --------------+----------+----------+------------+-------------+---------------------+-------------+ | host     | db    | user   | table_name | column_name | timestamp      | column_priv | + --------------+----------+----------+------------+-------------+---------------------+-------------+ | 192.168.18.% | yeyztest | dba_yeyz | test1   | id     | 0000-00-00 00:00:00 | update   | + --------------+----------+----------+------------+-------------+---------------------+-------------+ 1 row in set (0.00 sec)

再次用dba_yeyz进行update操作,可以看到结果:

?
1 2 3 4 5 6 7 8 9 10 11 12 mysql> update test1 set id=2 where id=22; query ok, 1 row affected (0.00 sec) rows matched: 1 changed: 1 warnings: 0   mysql> select * from test1; + ---------------+ | id      | + ---------------+ |       2 | | 3333333333333 | + ---------------+ 2 rows in set (0.00 sec)

   将id=22的列成功改成了id=2。

5、子程序层级

    create routine、alter routine、execute和grant权限适用于已经存储的子程序,这些权限可以被授予为全局层级和数据库层级,可以被存储在mysql.procs_priv中。

客户端发起请求的时候,mysql服务器核实请求时候的流程图如下:

  我将今天看到的一些细节的知识点一个一个罗列出来,希望对大家有点儿帮助:

1、在mysql5.7.16版本中,mysql系统库中已经没有host表了,跟权限控制相关的表只有5个,分别是user、db、table_priv、proc_priv、column_priv。

2、mysql.user表的主键是用user和host联合起来组成的,且看表结构:

?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 mysql --dba_admin@127.0.0.1:mysql 19:44:56>>show create table mysql.user\g *************************** 1. row ***************************      table : user create table : create table ` user ` (   `host` char (60) collate utf8_bin not null default '' ,   ` user ` char (32) collate utf8_bin not null default '' , ---------------权限字段(29个)--------------   `select_priv` enum( 'n' , 'y' ) character set utf8 not null default 'n' ,   ......此处省略   ---------------安全字段(4个)---------------   `ssl_type` enum( '' , 'any' , 'x509' , 'specified' ) character set utf8 not null default '' ,   `ssl_cipher` blob not null ,   `x509_issuer` blob not null ,   `x509_subject` blob not null , ---------------资源控制字段(4个)--------------   `max_questions` int (11) unsigned not null default '0' ,   `max_updates` int (11) unsigned not null default '0' ,   `max_connections` int (11) unsigned not null default '0' ,   `max_user_connections` int (11) unsigned not null default '0' , --------------插件字段(1个)---------------   `plugin` char (64) collate utf8_bin not null default 'mysql_native_password' , --------------密码字段(5个)--------------   `authentication_string` text collate utf8_bin,   `password_expired` enum( 'n' , 'y' ) character set utf8 not null default 'n' ,   `password_last_changed` timestamp null default null ,   `password_lifetime` smallint (5) unsigned default null ,   `account_locked` enum( 'n' , 'y' ) character set utf8 not null default 'n' ,   primary key (`host`,` user `) -------------联合主键,host在前----------- ) engine=myisam default charset=utf8 collate =utf8_bin comment= 'users and global privileges' 1 row in set (0.00 sec)

3、tables_priv表中只有以下权限,也就是关于表的权限:

select、insert、update、delete、drop、create、alter、grant、references、index、create view、show view、trigger;

columns_priv表中的权限只有下面四个:select、insert、update,references

4、修改一个用户的密码,一般有以下几种方式:

?
1 2 3 4 set password for user @host = password ( 'newpassword' ); update mysql. user set authentication_string= password ( 'pwd' ) where user = 'username' and host= 'hostname' ; alter user user @host identified by 'newpassword' ; mysqladmin -u username -h hostname -p password "new password" ;

   最好的方式是alter user的方法,事实上,在新的版本8.0中,set password的方法已经不能使用了,所以建议使用alter user的方法设置新的密码。

   除此之外,还有一种方法,不太常用,就是使用grant的方法覆盖掉之前的密码,这里我们简单实验一般,看看效果:

?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 mysql:mysql 20:01:05>> grant select on `yeyztest`.`test` to 'dba_yeyz' @ '192.168.18.%' identified by '111111' ; query ok, 0 rows affected, 1 warning (0.00 sec) mysql:mysql 20:01:29>> select user ,host,concat( user , '@' , '\'' ,host, '\'' ),authentication_string from mysql. user ; + ------------------+----------------+-----------------------------------+-------------------------------------------+ | user       | host      | concat( user , '@' , '\'' ,host, '\'' )  | authentication_string           | + ------------------+----------------+-----------------------------------+-------------------------------------------+ | dba_yeyz     | 192.168.18.%  | dba_yeyz@ '192.168.18.%'      | *fd571203974ba9afe270fe62151ae967eca5e0aa | + ------------------+----------------+-----------------------------------+-------------------------------------------+ 11 rows in set (0.00 sec)   mysql:mysql 20:01:31>> grant select on `yeyztest`.`test` to 'dba_yeyz' @ '192.168.18.%' identified by '123456' ; query ok, 0 rows affected, 1 warning (0.00 sec)   mysql:mysql 20:01:57>> select user ,host,concat( user , '@' , '\'' ,host, '\'' ),authentication_string from mysql. user ; + ------------------+----------------+-----------------------------------+-------------------------------------------+ | user       | host      | concat( user , '@' , '\'' ,host, '\'' )  | authentication_string           | + ------------------+----------------+-----------------------------------+-------------------------------------------+ | dba_yeyz     | 192.168.18.%  | dba_yeyz@ '192.168.18.%'      | *6bb4837eb74329105ee4568dda7dc67ed2ca2ad9 | + ------------------+----------------+-----------------------------------+-------------------------------------------+ 1 rows in set (0.00 sec)

   上面的测试可以看到,当我们使用grant的方法给一个指定的用户重新设置密码的时候,之前的老密码会被覆盖,所以这个操作在线上应该慎用,在每次grant的时候看看有没有已经存在的账号,确认没有之后,再进行grant操作

5、如果我们不慎忘记了mysql的root密码,可以重新启动mysql服务,加上--skip-grant-tables这个参数来启动mysql服务,这样就可以直接免除了在权限表里面的匹配工作,直接登陆进mysql服务中,从而修改root账号的密码。

6、如果使用update或者insert记录到mysql.user表中的方法创建账户或者修改密码,在执行完语句之后,必须使用flush privileges的操作刷新权限表,否则该操作无法产生效果。

7、有几个权限会影响mysqladmin工具的执行,分别是

reload权限:影响flush操作

shutdown权限:影响shutdown操作

process权限:影响processlist操作

super权限:影响kill操作

8、之前提到了mysql.user表中的资源控制的字段,分别是

max_questions每小时最大请求数、max_updates每小时最大更新数、max_connections每小时最大连接数、max_user_connections单个用户可同时建立的最大连接数。

如果我们想给一个用户设置这个参数,可以使用如下的sql来进行设置:

?
1 2 3 4 5 6 7 8 9 10 mysql:mysql 20:01:58>> grant select on `yeyztest`.`test` to 'dba_yeyz' @ '192.168.18.%' with max_queries_per_hour 1000; query ok, 0 rows affected, 1 warning (0.00 sec)   mysql:mysql 20:13:13>> select user ,host,max_questions from mysql. user where user = 'dba_yeyz' + ----------+--------------+---------------+ | user   | host     | max_questions | + ----------+--------------+---------------+ | dba_yeyz | 192.168.18.% |     1000 | + ----------+--------------+---------------+ 1 row in set (0.00 sec)

注意到,这里的grant语句中使用了with这个选项,with后面可以跟的选项有5个,分别是:

grant option:被授权的用户可以将这些权限赋予别的用户

max_queries_per_hour count:每个小时可以执行count次查询;

max_updates_per_hour count:每个小时可以执行count次更新;

max_connections_per_hour count:每个小时可以建立count个连接;

max_user_connections count:设置单个用户可以同时建立count个连接

9、设置全局变量:

set global default_password_lifetime = 180;

set global default_password_lifetime = 0;

可以设置密码的生命周期为6个月,6个月之后失效,如果设置为0,则一直有效。

当然,还可以在创建用户的时候就指定密码的修改周期或者禁用密码的修改周期:

?
1 2 3 4 5 create user 'jeffrey' @ 'localhost' password expire interval 90 day ; alter user 'jeffrey' @ 'localhost' password expire interval 90 day ;   create user 'jeffrey' @ 'localhost' password expire never; alter user 'jeffrey' @ 'localhost' password expire never;

10、有时候我们似乎已经删除了账号密码,但是却还可以通过账号密码进行访问,这个时候,需要检查一个设置,就是看看user表中是否有空记录:

select user,host from mysql.user where user='';

很有可能是你设置了user为空的记录,这样导致所有的用户都可以直接登陆。如果有,最好直接干掉它,因为它违背了安全的宗旨。

以上就是mysql 权限控制细节分析的详细内容,更多关于mysql 权限控制的资料请关注服务器之家其它相关文章!

原文链接:https://cloud.tencent.com/developer/article/1533810

可以去百度分享获取分享代码输入这里。
声明

1.本站遵循行业规范,任何转载的稿件都会明确标注作者和来源;2.本站的原创文章,请转载时务必注明文章作者和来源,不尊重原创的行为我们将追究责任;3.作者投稿可能会经我们编辑修改或补充。

【腾讯云】云服务器产品特惠热卖中
搜索
标签列表
    关注我们

    了解等多精彩内容