mysql 5.7.23用户权限_(九)MySQL用户和权限管理

(1)用户管理

1)登录和退出mysql

例:

mysql -h192.168.111.150 -P3306 -uroot -predhat mysql -e 'select user,host,authentication_string from mysql.user'

-h 指定主机名 [默认为localhost]

-P MySQL端口[默认为3306]

-u 指定用户名 [默认为root]

-p 指定登录密码 [默认为空]

此处mysql为指定登录的数据库

-e 接SQL语句

2)创建用户

方法一:create user语句创建

create user user1@'localhost' identified by 'password';

方法二:grant 语句创建

grant all on . to 'user1'@'localhost' identified by 'password';

grant all on blog.* to 'user2'@'%' identified by 'password';

3)删除用户

方法一:drop user

drop user 'user1'@'localhost';

方法二:delete语句删除

delete from mysql.user where user='user1' and host='localhost';

flush privileges;

4)修改用户密码

root 用户修改自己的密码

方法一:#mysqladmin -uroot -p'123' password 'new_password' //123为旧密码

方法二:update mysql.user set authentication_string=password('new_password') where user='root' and host='localhost'; flush privileges

方法三 :set password=password('new_password');

root修改其他用户密码

方法一:set password for user3@'localhost'=password('new_password');

方法二:update mysql.user set authentication_string=password('new_password') where user='root' and host='localhost'; flush privileges;

普通用户修改自己密码

方法:set password=password('new_password');

(2)权限管理

1)权限应用级别

user(全局) ---> db(数据库级别)--->tables_priv(表级别)---->columns_priv(字段级别)

所有用户名和密码信息都是user表中,全局用户的权限在user表中;库级别的用户权限信息在db表中

2)授权语法格式以及授权

grant 权限列表 on 库名.表名 to '用户名'@'客户端主机' [identified by '密码' with option参数];

权限列表:、

all 所有权限

select,update

数据库.表名:

*.* 所有库下的所有表

web.*web库下的所有表

web.stu_info web库下的stu_info表

客户端主机:

% 所有主机

192.168.2.% 192.168.2.0网段的所有主机

192.168.2.18指定主机

localhost指定主机

with_option参数:

grant option 授权选项,其他用户可以授权

max_queries_per_hour :定义每小时允许执行的查询数

max_updates_per_hour :定义每小时允许执行的更新数

max_connections_per_hour :定义每小时可以建立的连接数

max_user_connections :定义单个用户同时可以建立的连接数

示例

grant all on . to admin1@'%' identified by 'new_password' with grant option;

grant all on web.* to admin2@'%' identified by 'new_password';

3)查看权限

show grants\G \用户查看自己的权限

show grants for 'admin1'@'%'\G \管理员查看其他用户的权限

4)revoke回收权限

语法:revoke 权限列表 on 库.表 from 用户名@'客户端主机';

示例

revoke insert,delete on . from admin1@'%'; \回收部分权限

revoke all privileges on . from admin2@'%';\回收所有权限

revoke all privileges,grant option on . from admin2@'%';\回收所有权限

5)删除用户

5.6之前需要先把权限去掉在删除用户:revoke all privilege drop user

5.7之后可以直接删除用户:drop user

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值