【MySQL】数据库用户与权限

素颜马尾好姑娘i 2022-01-17 11:39 351阅读 0赞
  1. 创建用户
  2. mysql> create user 'tpcc'@'%' identified by 'tpcc';
  3. Query OK, 0 rows affected (0.02 sec)
  4. 修改用户密码
  5. mysql> set password for 'tpcc'@'%'=password('abcd.1234');
  6. Query OK, 0 rows affected (0.03 sec)
  7. 查询用户权限
  8. mysql> show grants for 'tpcc'@'%';
  9. +-----------------------------------------------------------------------------------------------------+
  10. | Grants for tpcc@% |
  11. +-----------------------------------------------------------------------------------------------------+
  12. | GRANT USAGE ON *.* TO 'tpcc'@'%' IDENTIFIED BY PASSWORD '*8E329B15E3C0FF9DDF7597B748CCE9473593BF60' |
  13. +-----------------------------------------------------------------------------------------------------+
  14. 1 row in set (0.00 sec)
  15. 删除用户
  16. mysql> drop user 'tpcc'@'%';
  17. Query OK, 0 rows affected (0.07 sec)
  18. 权限级别分为全局,库,表,列四个层次,在指定级别授予的权限也必须在指定的级别撤销权限
  19. 授予列的权限
  20. mysql> grant all (w_id) on tpcc100.warehouse to 'tpcc'@'%';
  21. Query OK, 0 rows affected (0.02 sec
  22. mysql> grant select (w_name) on tpcc100.warehouse to 'tpcc'@'%';
  23. Query OK, 0 rows affected (0.03 sec)
  24. mysql> grant insert (w_street_1) on tpcc100.warehouse to 'tpcc'@'%';
  25. Query OK, 0 rows affected (0.04 sec)
  26. mysql> select * from mysql.columns_priv;
  27. +------+----------+------+------------+-------------+---------------------+-------------+
  28. | Host | Db | User | Table_name | Column_name | Timestamp | Column_priv |
  29. +------+----------+------+------------+-------------+---------------------+-------------+
  30. | % | tpcc100 | tpcc | warehouse | w_id | 0000-00-00 00:00:00 | Select |
  31. | % | tpcc100 | tpcc | warehouse | w_name | 0000-00-00 00:00:00 | Select |
  32. | % | tpcc100 | tpcc | warehouse | w_street_1 | 0000-00-00 00:00:00 | Insert |
  33. +------+----------+------+------------+-------------+---------------------+-------------+
  34. 3 rows in set (0.00 sec)
  35. mysql> show grants for 'tpcc'@'%';
  36. +-----------------------------------------------------------------------------------------------------+
  37. | Grants for tpcc@% |
  38. +-----------------------------------------------------------------------------------------------------+
  39. | GRANT USAGE ON *.* TO 'tpcc'@'%' IDENTIFIED BY PASSWORD '*D2B995B159B5840C27690FC435C6ACEE7B9E1348' |
  40. | GRANT SELECT (w_id, w_name), INSERT (w_street_1) ON `tpcc1000`.`warehouse` TO 'tpcc'@'%' |
  41. +-----------------------------------------------------------------------------------------------------+
  42. 2 rows in set (0.00 sec)
  43. 撤销授予的列权限
  44. mysql> revoke select (w_id, w_name), insert (w_street_1) on tpcc1000.warehouse from 'tpcc'@'%';
  45. Query OK, 0 rows affected (0.03 sec)
  46. mysql> show grants for 'tpcc'@'%';
  47. +-----------------------------------------------------------------------------------------------------+
  48. | Grants for tpcc@% |
  49. +-----------------------------------------------------------------------------------------------------+
  50. | GRANT USAGE ON *.* TO 'tpcc'@'%' IDENTIFIED BY PASSWORD '*D2B995B159B5840C27690FC435C6ACEE7B9E1348' |
  51. +-----------------------------------------------------------------------------------------------------+
  52. 1 row in set (0.00 sec)
  53. 授予表的权限
  54. mysql> grant all on tpcc100.warehouse to 'tpcc'@'%';
  55. Query OK, 0 rows affected (0.03 sec)
  56. mysql> show grants for 'tpcc'@'%';
  57. +-----------------------------------------------------------------------------------------------------+
  58. | Grants for tpcc@% |
  59. +-----------------------------------------------------------------------------------------------------+
  60. | GRANT USAGE ON *.* TO 'tpcc'@'%' IDENTIFIED BY PASSWORD '*D2B995B159B5840C27690FC435C6ACEE7B9E1348' |
  61. | GRANT ALL PRIVILEGES ON `tpcc100`.`warehouse` TO 'tpcc'@'%' |
  62. +-----------------------------------------------------------------------------------------------------+
  63. 2 rows in set (0.00 sec)
  64. mysql> select * from mysql.tables_priv where user='tpcc'\G;
  65. *************************** 1. row ***************************
  66. Host: %
  67. Db: tpcc100
  68. User: tpcc
  69. Table_name: warehouse
  70. Grantor: root@localhost
  71. Timestamp: 0000-00-00 00:00:00
  72. Table_priv: Select,Insert,Update,Delete,Create,Drop,References,Index,Alter,Create View,Show view,Trigger
  73. Column_priv:
  74. 1 row in set (0.00 sec)
  75. 撤销授予的表权限
  76. mysql> revoke all on tpcc100.warehouse from 'tpcc'@'%';
  77. Query OK, 0 rows affected (0.02 sec)
  78. mysql> show grants for 'tpcc'@'%';
  79. +-----------------------------------------------------------------------------------------------------+
  80. | Grants for tpcc@% |
  81. +-----------------------------------------------------------------------------------------------------+
  82. | GRANT USAGE ON *.* TO 'tpcc'@'%' IDENTIFIED BY PASSWORD '*D2B995B159B5840C27690FC435C6ACEE7B9E1348' |
  83. +-----------------------------------------------------------------------------------------------------+
  84. 1 row in set (0.00 sec)
  85. 授予库的权限
  86. mysql> grant all on tpcc100.* to 'tpcc'@'%';
  87. Query OK, 0 rows affected (0.02 sec)
  88. mysql> show grants for 'tpcc'@'%';
  89. +-----------------------------------------------------------------------------------------------------+
  90. | Grants for tpcc@% |
  91. +-----------------------------------------------------------------------------------------------------+
  92. | GRANT USAGE ON *.* TO 'tpcc'@'%' IDENTIFIED BY PASSWORD '*D2B995B159B5840C27690FC435C6ACEE7B9E1348' |
  93. | GRANT ALL PRIVILEGES ON `tpcc100`.* TO 'tpcc'@'%' |
  94. +-----------------------------------------------------------------------------------------------------+
  95. 2 rows in set (0.00 sec)
  96. mysql> select * from mysql.db where user='tpcc'\G;
  97. *************************** 1. row ***************************
  98. Host: %
  99. Db: tpcc100
  100. User: tpcc
  101. Select_priv: Y
  102. Insert_priv: Y
  103. Update_priv: Y
  104. Delete_priv: Y
  105. Create_priv: Y
  106. Drop_priv: Y
  107. Grant_priv: N
  108. References_priv: Y
  109. Index_priv: Y
  110. Alter_priv: Y
  111. Create_tmp_table_priv: Y
  112. Lock_tables_priv: Y
  113. Create_view_priv: Y
  114. Show_view_priv: Y
  115. Create_routine_priv: Y
  116. Alter_routine_priv: Y
  117. Execute_priv: Y
  118. Event_priv: Y
  119. Trigger_priv: Y
  120. 1 row in set (0.00 sec)
  121. 撤销授予的库权限
  122. mysql> revoke all on tpcc100.* from 'tpcc'@'%';
  123. Query OK, 0 rows affected (0.02 sec)
  124. mysql> show grants for 'tpcc'@'%';
  125. +-----------------------------------------------------------------------------------------------------+
  126. | Grants for tpcc@% |
  127. +-----------------------------------------------------------------------------------------------------+
  128. | GRANT USAGE ON *.* TO 'tpcc'@'%' IDENTIFIED BY PASSWORD '*D2B995B159B5840C27690FC435C6ACEE7B9E1348' |
  129. +-----------------------------------------------------------------------------------------------------+
  130. 1 row in set (0.00 sec)
  131. 授予全局权限
  132. mysql> grant all on *.* to 'tpcc'@'%';
  133. Query OK, 0 rows affected (0.01 sec)
  134. mysql> show grants for 'tpcc'@'%';
  135. +--------------------------------------------------------------------------------------------------------------+
  136. | Grants for tpcc@% |
  137. +--------------------------------------------------------------------------------------------------------------+
  138. | GRANT ALL PRIVILEGES ON *.* TO 'tpcc'@'%' IDENTIFIED BY PASSWORD '*D2B995B159B5840C27690FC435C6ACEE7B9E1348' |
  139. +--------------------------------------------------------------------------------------------------------------+
  140. 1 row in set (0.00 sec)
  141. mysql> select * from mysql.user where user='tpcc'\G;
  142. *************************** 1. row ***************************
  143. Host: %
  144. User: tpcc
  145. Password: *D2B995B159B5840C27690FC435C6ACEE7B9E1348
  146. Select_priv: Y
  147. Insert_priv: Y
  148. Update_priv: Y
  149. Delete_priv: Y
  150. Create_priv: Y
  151. Drop_priv: Y
  152. Reload_priv: Y
  153. Shutdown_priv: Y
  154. Process_priv: Y
  155. File_priv: Y
  156. Grant_priv: N
  157. References_priv: Y
  158. Index_priv: Y
  159. Alter_priv: Y
  160. Show_db_priv: Y
  161. Super_priv: Y
  162. Create_tmp_table_priv: Y
  163. Lock_tables_priv: Y
  164. Execute_priv: Y
  165. Repl_slave_priv: Y
  166. Repl_client_priv: Y
  167. Create_view_priv: Y
  168. Show_view_priv: Y
  169. Create_routine_priv: Y
  170. Alter_routine_priv: Y
  171. Create_user_priv: Y
  172. Event_priv: Y
  173. Trigger_priv: Y
  174. Create_tablespace_priv: Y
  175. ssl_type:
  176. ssl_cipher:
  177. x509_issuer:
  178. x509_subject:
  179. max_questions: 0
  180. max_updates: 0
  181. max_connections: 0
  182. max_user_connections: 0
  183. plugin: mysql_native_password
  184. authentication_string:
  185. password_expired: N
  186. 1 row in set (0.00 sec)
  187. 撤销授予的全局权限
  188. mysql> revoke all on *.* from 'tpcc'@'%';
  189. Query OK, 0 rows affected (0.03 sec)
  190. mysql> show grants for 'tpcc'@'%';
  191. +-----------------------------------------------------------------------------------------------------+
  192. | Grants for tpcc@% |
  193. +-----------------------------------------------------------------------------------------------------+
  194. | GRANT USAGE ON *.* TO 'tpcc'@'%' IDENTIFIED BY PASSWORD '*D2B995B159B5840C27690FC435C6ACEE7B9E1348' |
  195. +-----------------------------------------------------------------------------------------------------+
  196. 1 row in set (0.00 sec)

转载于:https://blog.51cto.com/13598811/2349838

发表评论

表情:
评论列表 (有 0 条评论,351人围观)

还没有评论,来说两句吧...

相关阅读