创建用户

  1. create user 'clown'@'%' identified by mysql_native_password '123456';

授权

  1. grant all privileges on nacos.* to 'clown'@'%' with grant option;
  2. grant select,create,delete,update on nacos.* to 'clown'@'%' with grant option;

收回权限

  1. revoke select,create,delete,grant option on nacos.* from 'clown'@'%';
  2. revoke all privileges,grant option from 'clown'@'%';
  3. -- 或者
  4. revoke all privileges on nacos.* from 'clown'@'%';
  5. revoke grant option on nacos.* from 'clown'@'%';

授予什么权限就回收什么权限

  1. grant select on *.* to 'clown'@'%' with grant option;

授予全局权限,权限信息保存在mysql.user表中

  1. select * from mysql.user where user='clown'\G;
  2. *************************** 1. row ***************************
  3. Host: %
  4. User: clown
  5. Password: *06C0BF5B64ECE2F648B5F048A71903906BA08E5C
  6. Select_priv: Y
  7. Insert_priv: N
  8. Update_priv: N
  9. Delete_priv: N
  10. Create_priv: N
  11. Drop_priv: N
  12. Reload_priv: N
  13. Shutdown_priv: N
  14. Process_priv: N
  15. File_priv: N
  16. Grant_priv: Y
  17. References_priv: N
  18. Index_priv: N
  19. Alter_priv: N
  20. Show_db_priv: N
  21. Super_priv: N
  22. Create_tmp_table_priv: N
  23. Lock_tables_priv: N
  24. Execute_priv: N
  25. Repl_slave_priv: N
  26. Repl_client_priv: N
  27. Create_view_priv: N
  28. Show_view_priv: N
  29. Create_routine_priv: N
  30. Alter_routine_priv: N
  31. Create_user_priv: N
  32. Event_priv: N
  33. Trigger_priv: N
  34. Create_tablespace_priv: N
  35. ssl_type:
  36. ssl_cipher:
  37. x509_issuer:
  38. x509_subject:
  39. max_questions: 0
  40. max_updates: 0
  41. max_connections: 0
  42. max_user_connections: 0
  43. plugin: mysql_native_password
  44. authentication_string:
  45. password_expired: N
  1. grant select on nacos.* to 'clown'@'%' with grant option;

授予数据库权限,权限信息保存在mysql.db表中

  1. select * from mysql.db\G;
  2. *************************** 1. row ***************************
  3. Host: %
  4. Db: nacos
  5. User: clown
  6. Select_priv: Y
  7. Insert_priv: N
  8. Update_priv: N
  9. Delete_priv: N
  10. Create_priv: N
  11. Drop_priv: N
  12. Grant_priv: Y
  13. References_priv: N
  14. Index_priv: N
  15. Alter_priv: N
  16. Create_tmp_table_priv: N
  17. Lock_tables_priv: N
  18. Create_view_priv: N
  19. Show_view_priv: N
  20. Create_routine_priv: N
  21. Alter_routine_priv: N
  22. Execute_priv: N
  23. Event_priv: N
  24. Trigger_priv: N
  1. grant select on nacos.user to 'clown'@'%' with grant option;

授予某张表权限,权限信息保存在mysql.tables_priv表中

  1. select * from mysql.tables_priv;
  2. +-----------+-----+-------+------------+----------------+---------------------+--------------+-------------+
  3. | Host | Db | User | Table_name | Grantor | Timestamp | Table_priv | Column_priv |
  4. +-----------+-----+-------+------------+----------------+---------------------+--------------+-------------+
  5. | % |nacos| clown | user | root@localhost | 0000-00-00 00:00:00 | Select,Grant | |
  6. +-----------+-----+-------+------------+----------------+---------------------+--------------+-------------+
  1. grant select(name) on nacos.user to 'clown'@'%' with grant option;

授予某个字段的权限,权限信息保存在mysql.columns_priv表中

  1. select * from mysql.columns_priv;
  2. +-----------+-----+-------+------------+-------------+---------------------+-------------+
  3. | Host | Db | User | Table_name | Column_name | Timestamp | Column_priv |
  4. +-----------+-----+-------+------------+-------------+---------------------+-------------+
  5. | % |nacos| clown | user | name | 0000-00-00 00:00:00 | Select |
  6. +-----------+-----+-------+------------+-------------+---------------------+-------------+

当使用

  1. revoke all privileges on *.* from 'clown'@'%';

回收的只是全局的权限,clown用户其他的权限,比如对nacos数据库的权限,对user表的权限,对某个字段的权限仍然持有。 所以为了回收用户的所有权限,使用

  1. revoke all privileges,grant option from 'clown'@'%';

这是条固定语法,all privileges和grant option必须都有