alter user 'zhangsan'@'%' identified by 'new_password';
set password for 'zhangsan'@'%'='new_password';
2.权限管理
2.1 查看和授予权限
show grant; 查看当前用户权限
show grant for zhangsan; root用户查看其他用户权限
grant all privileges on *.* to 'lisi'@'%';
权限有:all privileges、select、update、delete、insert
on表示指定权限针对哪些数据库和表,数据库.表
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
//root用户下 mysql> show grants for zhangsan; +--------------------------------------+ | Grants for zhangsan@% | +--------------------------------------+ | GRANT USAGE ON *.* TO `zhangsan`@`%` | //登录权限,所有新创建的用户都具有这个权限 +--------------------------------------+ //把test数据库中的所有表的select,update权限都给zhangsan用户 mysql> grant select,update on test.* to 'zhangsan'@'%'; Query OK, 0 rows affected (0.01 sec) mysql> show grants for zhangsan; +----------------------------------------------------+ | Grants for zhangsan@% | +----------------------------------------------------+ | GRANT USAGE ON *.* TO `zhangsan`@`%` | | GRANT SELECT, UPDATE ON `test`.* TO `zhangsan`@`%` | +----------------------------------------------------+ 2 rows in set (0.00 sec)
mysql> create role 'manager'; //等同于create role 'manager'@'%'; Query OK, 0 rows affected (0.00 sec)
删除角色
1 2
mysql> drop role 'manager'; Query OK, 0 rows affected (0.00 sec)
4.2 授予及收回角色权限
授予权限
1 2 3 4 5 6 7 8 9 10 11
//root用户下 mysql> grant select,update on test.tb_user1 to 'manager'; Query OK, 0 rows affected (0.00 sec) mysql> show grants for 'manager'@'%'; +------------------------------------------------------------+ | Grants for manager@% | +------------------------------------------------------------+ | GRANT USAGE ON *.* TO `manager`@`%` | | GRANT SELECT, UPDATE ON `test`.`tb_user1` TO `manager`@`%` | +------------------------------------------------------------+ 2 rows in set (0.00 sec)
取消权限
1 2
mysql> revoke select,update on test.tb_user1 from 'manager'; Query OK, 0 rows affected (0.00 sec)
4.3 给用户赋予角色
1 2 3 4 5 6 7 8 9 10 11 12
mysql> grant 'manager'@'%' to 'zhangsan'@'%'; Query OK, 0 rows affected (0.01 sec)
mysql> show grants for 'zhangsan'@'%'; +--------------------------------------------+ | Grants for zhangsan@% | +--------------------------------------------+ | GRANT USAGE ON *.* TO `zhangsan`@`%` | | GRANT UPDATE ON `test`.* TO `zhangsan`@`%` | | GRANT `manager`@`%` TO `zhangsan`@`%` | +--------------------------------------------+ 3 rows in set (0.00 sec)
查询当前角色是否被激活 mysql> select current_role(); +----------------+ | current_role() | +----------------+ | NONE |//未被激活,因此用户未具备相应的角色 +----------------+ 1 row in set (0.00 sec) //激活角色 mysql> set default role 'manager'@'%' to 'zhangsan'@'%'; Query OK, 0 rows affected (0.00 sec) //退出重新登录 mysql> select current_role(); +----------------+ | current_role() | +----------------+ | `manager`@`%` | +----------------+ 1 row in set (0.00 sec)
还有一种方法可以对所有角色永久激活,新创建的角色都默认激活
1 2 3 4 5 6 7 8 9 10
//ROOT用户下 mysql> show variables like 'activate_all_roles_on_login'; +-----------------------------+-------+ | Variable_name | Value | +-----------------------------+-------+ | activate_all_roles_on_login | OFF | +-----------------------------+-------+ 1 row in set (0.01 sec) mysql> set global activate_all_roles_on_login=on; Query OK, 0 rows affected (0.00 sec)