mysql 用户/授权管理

一.用户管理

1.查看所有用户
mysql> select user,host from mysql.user;
+---------------+-----------+
| user          | host      |
+---------------+-----------+
| root          | %         |
| mysql.session | localhost |
| mysql.sys     | localhost |
+---------------+-----------+
3 rows in set (0.00 sec)
2.创建用户

语法

create user '用户名'@'主机地址' identified by '密码';

案例

1.创建'test'@'localhost'用户,密码为123456

mysql> create user 'test'@'localhost' identified by '123456';
Query OK, 0 rows affected (0.01 sec)
3.删除用户

语法

drop user '用户名'@'主机地址';

案例
1.删除'test'@'localhost'用户

mysql> drop user 'test'@'localhost';
Query OK, 0 rows affected (0.00 sec)
4.修改密码

语法

  • 方法1

    alter user '用户名'@'主机地址' identified by '密码';
    
  • 方法2

    适用于5.6及以下版本

    update mysql.user set password=password('123456') where user='root' and host='%';
    

    适用于5.7版本

    update mysql.user set authentication_string=password('123456') where user='root' and host='%';
    

案例

1.方式1修改test@'localhost'用户密码为123456

mysql> alter user 'test'@'localhost' identified by '123456';
Query OK, 0 rows affected (0.00 sec)

2.方式2修改test@'localhost'用户密码为123456

mysql> update mysql.user set authentication_string=password('123456') where user='test' and host='localhost';
Query OK, 0 rows affected, 1 warning (0.05 sec)
Rows matched: 1  Changed: 0  Warnings: 1

二.授权管理

1.查看用户权限

命令

show grants for '用户名'@'主机地址'

案例

1.查看当前用户权限

mysql> show grants; 

2.查看指定用户权限

mysql> show grants for 'root'@'%';
2.用户授权

命令

grant 权限 on 库.表 to '用户名'@'主机地址';

权限列表

  • all 所有权限

  • 数据权限
    select update delete insert

  • 结构权限
    drop create alter

案例

1.授权所有库的所有操作

mysql> grant all on *.* to test@'localhost';
mysql> flush privileges;

2.授权单个库的查、改操作

# 如果是多个库,那多次执行语句
mysql> grant select,update on test.* to test@'localhost';
mysql> flush privileges;

3.授权单个表的查操作

# 如果是多个库,那多次执行语句
mysql> grant select on test.c_user to test@'localhost';
mysql> flush privileges;

4.授权单个表某些字段的查操作

mysql> grant select(id, username) on test.c_user to test@'localhost';
mysql> flush privileges;

问题

1.不可一次授权多个库,需要授权多个库,重复执行授权语句
2.不可一次授权单个库的多个表,需要授权多个表,重复执行授权语句

3.撤销授权

和授权一样,把grant改为revoke,to变为from

命令

revoke 权限 on 库.表 from '用户名'@'主机地址';

案例

1.取消’test’@'localhost’用户 对test库c_user表的查看id,username的权限

mysql> revoke select(id, username) on test.c_user from 'test'@'localhost';
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值