MySQL权限授权认证详解

                                   作者:尹正杰

版权声明:原创作品,谢绝转载!否则将追究法律责任。  

一.MySQL权限系统介绍
1>.权限系统的作用是授予来自某个主机的某个用户可以查询、插入、修改、删除等数据库操作的权限
2>.不能明确的指定拒绝某个用户的连接
3>.权限控制(授权与回收)的执行语句包括create user, grant, revoke
4>.授权后的权限都会存放在MySQL的内部数据库中(数据库名叫mysql),并在数据库启动之后把权限信息复制到内存中
5>.MySQL用户的认证信息不光包括用户名,还要包含连接发起的主机名(以下两个yinzhengjie被认为不是同一个用户,因为它们的主机名不同)

  1. >>>SHOW GRANTS FOR yinzhengjie’@‘node101.yinzhengjie.org.cn’;
  2. >>>SHOW GRANTS FOR 'yinzhengjie’@‘node102.yinzhengjie.org.cn’;

二.MySQL权限级别介绍

1>.MySQL权限级别

  1. 全局性的管理权限,作用于整个MySQL实例级别;
  2. 数据库级别的权限,作用于某个指定的数据库上或者所有的数据库上;
  3. 数据库对象级别的权限,作用于指定的数据库对象上(表、视图等)或者所有的数据库对象上;

2>.权限存储在mysql库的user, db, tables_priv, columns_priv, and procs_priv这几个系统表中,待MySQL实例启动后就加载到内存中

3>.查看mysql实例默认root用户的权限(来自localhost)

  1. mysql> SHOW GRANTS FOR root@localhost\G
  2. *************************** . row ***************************
  3. Grants for root@localhost: GRANT SELECT, INSERT, UPDATE, DELETE, CREATE, DROP, RELOAD, SHUTDOWN, PROCESS, FILE, REFERENCES, INDEX, ALTER, SHOW DATABASES, SUPER, CREATE TEMPORARY TABLES, LOCK TABLES, EXECUTE, REPLICATION SLAVE, REPLICATION CLIENT, CREATE VIEW, SHOW VIEW, CREATE ROUTINE, ALTER ROUTINE, CREATE USER, EVENT, TRIGGER, CREATE TABLESPACE, CREATE ROLE, DROP ROLE ON *.* TO `root`@`localhost` WITH GRANT OPTION
  4. *************************** . row ***************************
  5. Grants for root@localhost: GRANT APPLICATION_PASSWORD_ADMIN,BACKUP_ADMIN,BINLOG_ADMIN,BINLOG_ENCRYPTION_ADMIN,CONNECTION_ADMIN,ENCRYPTION_KEY_ADMIN,GROUP_REPLICATION_ADMIN,PERSIST_RO_VARIABLES_ADMIN,REPLICATION_SLAVE_ADMIN,RESOURCE_GROUP_ADMIN,RESOURCE_GROUP_USER,ROLE_ADMIN,SERVICE_CONNECTION_ADMIN,SESSION_VARIABLES_ADMIN,SET_USER_ID,SYSTEM_VARIABLES_ADMIN,XA_RECOVER_ADMIN ON *.* TO `root`@`localhost` WITH GRANT OPTION
  6. *************************** . row ***************************
  7. Grants for root@localhost: GRANT PROXY ON ''@'' TO 'root'@'localhost' WITH GRANT OPTION
  8. rows in set (0.01 sec)
  9.  
  10. mysql>

mysql> SHOW GRANTS FOR root@localhost\G

4>.对比root用户在几个权限系统表中的数据

  1. mysql> SELECT * FROM user WHERE user='root' AND host='localhost'\G
  2. *************************** . row ***************************
  3. Host: localhost
  4. User: root
  5. Select_priv: Y
  6. Insert_priv: Y
  7. Update_priv: Y
  8. Delete_priv: Y
  9. Create_priv: Y
  10. Drop_priv: Y
  11. Reload_priv: Y
  12. Shutdown_priv: Y
  13. Process_priv: Y
  14. File_priv: Y
  15. Grant_priv: Y
  16. References_priv: Y
  17. Index_priv: Y
  18. Alter_priv: Y
  19. Show_db_priv: Y
  20. Super_priv: Y
  21. Create_tmp_table_priv: Y
  22. Lock_tables_priv: Y
  23. Execute_priv: Y
  24. Repl_slave_priv: Y
  25. Repl_client_priv: Y
  26. Create_view_priv: Y
  27. Show_view_priv: Y
  28. Create_routine_priv: Y
  29. Alter_routine_priv: Y
  30. Create_user_priv: Y
  31. Event_priv: Y
  32. Trigger_priv: Y
  33. Create_tablespace_priv: Y
  34. ssl_type:
  35. ssl_cipher:
  36. x509_issuer:
  37. x509_subject:
  38. max_questions:
  39. max_updates:
  40. max_connections:
  41. max_user_connections:
  42. plugin: caching_sha2_password
  43. authentication_string: $A$$_DHTgn}dT9t%>5eMM4wjrUWB.UY3A60WfUlqsZAVP0HhJ3Xxp1bFRs76g9B
  44. password_expired: N
  45. password_last_changed: -- ::
  46. password_lifetime: NULL
  47. account_locked: N
  48. Create_role_priv: Y
  49. Drop_role_priv: Y
  50. Password_reuse_history: NULL
  51. Password_reuse_time: NULL
  52. Password_require_current: NULL
  53. User_attributes: NULL
  54. row in set (0.00 sec)
  55.  
  56. mysql>

观察“root@localhost”在user表中的增删改查权限,基本上都是“Y”(mysql> SELECT * FROM user WHERE user='root' AND host='localhost'\G)

  1. mysql> SELECT * FROM db WHERE user='root' AND host='localhost'\G
  2. Empty set (0.00 sec)
  3.  
  4. mysql>

观察“root@localhost”在db表中是无记录的(mysql> SELECT * FROM db WHERE user='root' AND host='localhost'\G)

  1. mysql> SELECT * FROM tables_priv WHERE host='localhost' AND user = 'root'\G
  2. Empty set (0.00 sec)
  3.  
  4. mysql>

观察“root@localhost”在tables_priv表中是无记录的(mysql> SELECT * FROM tables_priv WHERE host='localhost' AND user = 'root'\G )

  1. mysql> SELECT * FROM columns_priv WHERE host='localhost' AND user = 'root'\G
  2. Empty set (0.00 sec)
  3.  
  4. mysql>

观察“root@localhost”在columns_priv表中是无记录的(mysql> SELECT * FROM columns_priv WHERE host='localhost' AND user = 'root'\G )

  1. mysql> SELECT * FROM procs_priv WHERE host='localhost' AND user = 'root'\G
  2. Empty set (0.00 sec)
  3.  
  4. mysql>

观察“root@localhost”在procs_priv表中是无记录的(mysql> SELECT * FROM procs_priv WHERE host='localhost' AND user = 'root'\G)

5>.查看mysql实例默认mysql.sys用户的权限(来自localhost)

  1. mysql> SHOW GRANTS FOR 'mysql.sys'@localhost;
  2. +---------------------------------------------------------------+
  3. | Grants for mysql.sys@localhost |
  4. +---------------------------------------------------------------+
  5. | GRANT USAGE ON *.* TO `mysql.sys`@`localhost` |
  6. | GRANT TRIGGER ON `sys`.* TO `mysql.sys`@`localhost` |
  7. | GRANT SELECT ON `sys`.`sys_config` TO `mysql.sys`@`localhost` |
  8. +---------------------------------------------------------------+
  9. rows in set (0.00 sec)
  10.  
  11. mysql>
  12. mysql> SHOW GRANTS FOR 'mysql.sys'@localhost\G
  13. *************************** . row ***************************
  14. Grants for mysql.sys@localhost: GRANT USAGE ON *.* TO `mysql.sys`@`localhost`
  15. *************************** . row ***************************
  16. Grants for mysql.sys@localhost: GRANT TRIGGER ON `sys`.* TO `mysql.sys`@`localhost`
  17. *************************** . row ***************************
  18. Grants for mysql.sys@localhost: GRANT SELECT ON `sys`.`sys_config` TO `mysql.sys`@`localhost`
  19. rows in set (0.00 sec)
  20.  
  21. mysql>

mysql> SHOW GRANTS FOR 'mysql.sys'@localhost;

6>.对比mysql.sys用户在几个权限系统表中的数据

  1. mysql> SELECT * FROM user WHERE user='mysql.sys' AND host='localhost'\G
  2. *************************** . row ***************************
  3. Host: localhost
  4. User: mysql.sys
  5. Select_priv: N
  6. Insert_priv: N
  7. Update_priv: N
  8. Delete_priv: N
  9. Create_priv: N
  10. Drop_priv: N
  11. Reload_priv: N
  12. Shutdown_priv: N
  13. Process_priv: N
  14. File_priv: N
  15. Grant_priv: N
  16. References_priv: N
  17. Index_priv: N
  18. Alter_priv: N
  19. Show_db_priv: N
  20. Super_priv: N
  21. Create_tmp_table_priv: N
  22. Lock_tables_priv: N
  23. Execute_priv: N
  24. Repl_slave_priv: N
  25. Repl_client_priv: N
  26. Create_view_priv: N
  27. Show_view_priv: N
  28. Create_routine_priv: N
  29. Alter_routine_priv: N
  30. Create_user_priv: N
  31. Event_priv: N
  32. Trigger_priv: N
  33. Create_tablespace_priv: N
  34. ssl_type:
  35. ssl_cipher:
  36. x509_issuer:
  37. x509_subject:
  38. max_questions:
  39. max_updates:
  40. max_connections:
  41. max_user_connections:
  42. plugin: caching_sha2_password
  43. authentication_string: $A$$THISISACOMBINATIONOFINVALIDSALTANDPASSWORDTHATMUSTNEVERBRBEUSED
  44. password_expired: N
  45. password_last_changed: -- ::
  46. password_lifetime: NULL
  47. account_locked: Y
  48. Create_role_priv: N
  49. Drop_role_priv: N
  50. Password_reuse_history: NULL
  51. Password_reuse_time: NULL
  52. Password_require_current: NULL
  53. User_attributes: NULL
  54. row in set (0.00 sec)
  55.  
  56. mysql>

观察“mysql.sys@localhost”在user表中的增删改查权限,基本上都是“N”(mysql> SELECT * FROM user WHERE user='mysql.sys' AND host='localhost'\G)

  1. mysql> SELECT * FROM db WHERE user='mysql.sys' AND host='localhost'\G
  2. *************************** . row ***************************
  3. Host: localhost
  4. Db: sys
  5. User: mysql.sys
  6. Select_priv: N
  7. Insert_priv: N
  8. Update_priv: N
  9. Delete_priv: N
  10. Create_priv: N
  11. Drop_priv: N
  12. Grant_priv: N
  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: Y
  25. row in set (0.00 sec)
  26.  
  27. mysql>

观察“mysql.sys@localhost”在db表中的只有一条记录,在sys数据库上的Trigger_priv字段是‘Y’,其他权限都是‘N’(mysql> SELECT * FROM db WHERE user='mysql.sys' AND host='localhost'\G)

  1. mysql> SELECT * FROM tables_priv WHERE user='mysql.sys' AND host='localhost'\G
  2. *************************** . row ***************************
  3. Host: localhost
  4. Db: sys
  5. User: mysql.sys
  6. Table_name: sys_config
  7. Grantor: root@localhost
  8. Timestamp: -- ::
  9. Table_priv: Select
  10. Column_priv:
  11. row in set (0.00 sec)
  12.  
  13. mysql>
  14. mysql>

观察“mysql.sys@localhost”在tables_priv表中的只有一 条记录,在sys_config表上有SELECT权限(mysql> SELECT * FROM tables_priv WHERE user='mysql.sys' AND host='localhost'\G)

  1. mysql> SELECT * FROM columns_priv WHERE user='mysql.sys' AND host='localhost'\G
  2. Empty set (0.00 sec)
  3.  
  4. mysql>

观察“mysql.sys@localhost”在columns_priv表中的是无记录的(mysql> SELECT * FROM columns_priv WHERE user='mysql.sys' AND host='localhost'\G)

  1. mysql> SELECT * FROM procs_priv WHERE user='mysql.sys' AND host='localhost'\G
  2. Empty set (0.00 sec)
  3.  
  4. mysql>
  5. mysql>

观察“mysql.sys@localhost”在procs_priv表中是无记录的(mysql> SELECT * FROM procs_priv WHERE user='mysql.sys' AND host='localhost'\G)

三.MySQL权限详解

1>.ALL/ALL PRIVILEGES权限

  代表全局或者全数据库对象级别的所有权限。

2>.ALTER权限

  代表允许修改表结构的权限,但必须要求有CREATE和INSERT权限配合。如果是RENAME表名,则必须要求有ALTER和DROP原表,CREATE和INSERT新表的权限。

3>.ALTER ROUTINE权限

  代表允许修改或者删除存储过程,函数的权限。

4>.CREATE权限

  CREATE权限代表允许创建新的数据库和表的权限。

5>.CREATE ROUTINE权限

  代表允许创建存储过程,函数的权限。

6>.CREATE TABLESPACE权限

  代表允许创建,修改,删除表空间和日志组的权限。

7>.CRATE TEMPOARY TABLES权限

   代表允许创建临时表的权限。

8>.CREATE USER权限

  代表允许创建,修改,删除,重命名USRER的权限。

9>.CREATE VIEW权限

  代表允许创建视图的权限。

10>.DELETE权限

  代表允许删除行数据的权限。

11>.DROP权限

  代表允许删除数据库,表,视图的权限,包括TRUNCATE TABLE命令。

12>.EVENT权限

  代表允许查询,创建,修改,删除MySQL事件。

13>.Execute权限

  代表允许执行存储过程和函数的权限。

14>.FILE权限

  代表允许在MySQL可以访问的目录进行读写磁盘文件操作,可使用的命令包括LOAD DATA INFILE,SELECT ... INTO OUTFILE,LOAD FILE()函数。

15>.GRANT OPTION权限

  代表是否允许此用户授权或者收回其他用户你给予的权限。

16>.INDEX权限

  代表是否允许创建和删除索引。

17>.INSERT权限

  代表是否允许在表里插入数据,同时在执行ANALYZE TABLE,OPTIMIZE TABLE,REPAIR TABLE语句的时候也需要INSERT权限。

18>.LOCK权限

  代表允许对拥有SELECT权限的表进行锁定,以防止其他链接对此表的读或写。

19>.PROCESS权限

  代表允许查看MySQL中的进程信息,比如执行SHOW PROCESSLIST,mysqladmin processlist(命令行),SHOW ENGINES等命令。

  1. mysql> SHOW PROCESSLIST\G
  2. *************************** . row ***************************
  3. Id:
  4. User: event_scheduler
  5. Host: localhost
  6. db: NULL
  7. Command: Daemon
  8. Time:
  9. State: Waiting on empty queue
  10. Info: NULL
  11. *************************** . row ***************************
  12. Id:
  13. User: root
  14. Host: localhost
  15. db: mysql
  16. Command: Query
  17. Time:
  18. State: starting
  19. Info: SHOW PROCESSLIST
  20. rows in set (0.00 sec)
  21.  
  22. mysql>

mysql> SHOW PROCESSLIST\G

  1. mysql> SHOW ENGINES\G
  2. *************************** . row ***************************
  3. Engine: FEDERATED
  4. Support: NO
  5. Comment: Federated MySQL storage engine
  6. Transactions: NULL
  7. XA: NULL
  8. Savepoints: NULL
  9. *************************** . row ***************************
  10. Engine: InnoDB
  11. Support: DEFAULT
  12. Comment: Supports transactions, row-level locking, and foreign keys
  13. Transactions: YES
  14. XA: YES
  15. Savepoints: YES
  16. *************************** . row ***************************
  17. Engine: PERFORMANCE_SCHEMA
  18. Support: YES
  19. Comment: Performance Schema
  20. Transactions: NO
  21. XA: NO
  22. Savepoints: NO
  23. *************************** . row ***************************
  24. Engine: MyISAM
  25. Support: YES
  26. Comment: MyISAM storage engine
  27. Transactions: NO
  28. XA: NO
  29. Savepoints: NO
  30. *************************** . row ***************************
  31. Engine: MRG_MYISAM
  32. Support: YES
  33. Comment: Collection of identical MyISAM tables
  34. Transactions: NO
  35. XA: NO
  36. Savepoints: NO
  37. *************************** . row ***************************
  38. Engine: BLACKHOLE
  39. Support: YES
  40. Comment: /dev/null storage engine (anything you write to it disappears)
  41. Transactions: NO
  42. XA: NO
  43. Savepoints: NO
  44. *************************** . row ***************************
  45. Engine: MEMORY
  46. Support: YES
  47. Comment: Hash based, stored in memory, useful for temporary tables
  48. Transactions: NO
  49. XA: NO
  50. Savepoints: NO
  51. *************************** . row ***************************
  52. Engine: CSV
  53. Support: YES
  54. Comment: CSV storage engine
  55. Transactions: NO
  56. XA: NO
  57. Savepoints: NO
  58. *************************** . row ***************************
  59. Engine: ARCHIVE
  60. Support: YES
  61. Comment: Archive storage engine
  62. Transactions: NO
  63. XA: NO
  64. Savepoints: NO
  65. rows in set (0.00 sec)
  66.  
  67. mysql>

mysql> SHOW ENGINES\G

  1. [root@node105 ~]# mysqladmin processlist -uroot -pyinzhengjie
  2. mysqladmin: [Warning] Using a password on the command line interface can be insecure.
  3. +----+-----------------+-----------+----+---------+------+------------------------+------------------+
  4. | Id | User | Host | db | Command | Time | State | Info |
  5. +----+-----------------+-----------+----+---------+------+------------------------+------------------+
  6. | | event_scheduler | localhost | | Daemon | | Waiting on empty queue | |
  7. | | root | localhost | | Query | | starting | show processlist |
  8. +----+-----------------+-----------+----+---------+------+------------------------+------------------+
  9. [root@node105 ~]#
  10. [root@node105 ~]#

[root@node105 ~]# mysqladmin processlist -uroot -pyinzhengjie

20>.REFERENCE权限

  是在5.7.6版本之后引入,代表是否允许创建外键。

21>.RELOAD权限

  代表允许执行FLUSH命令,指明重新家在权限表到系统内存中,REFRESH命令代表关闭和重新开启日志文件并刷新所有到表。

22>.REPLICATION CLIENT权限

  代表允许执行SHOW MASTER STATUS,SHOW SLAVE STATUS,SHOW BINARY LOGS命令。

  1. mysql> SHOW MASTER STATUS;
  2. +---------------+----------+--------------+------------------+-------------------+
  3. | File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
  4. +---------------+----------+--------------+------------------+-------------------+
  5. | binlog.000003 | 155 | | | |
  6. +---------------+----------+--------------+------------------+-------------------+
  7. 1 row in set (0.00 sec)
  8.  
  9. mysql>

mysql> SHOW MASTER STATUS;

  1. mysql> SHOW SLAVE STATUS;
  2. Empty set (0.00 sec)
  3.  
  4. mysql>

mysql> SHOW SLAVE STATUS;

  1. mysql>
  2. mysql> SHOW BINARY LOGS;
  3. +---------------+-----------+-----------+
  4. | Log_name | File_size | Encrypted |
  5. +---------------+-----------+-----------+
  6. | binlog.000001 | 513 | No |
  7. | binlog.000002 | 178 | No |
  8. | binlog.000003 | 155 | No |
  9. +---------------+-----------+-----------+
  10. 3 rows in set (0.00 sec)
  11.  
  12. mysql>
  13. mysql>

mysql> SHOW BINARY LOGS;

23>.REPLICATION SLAVE权限

  代表允许SLAVE主机通过此用户连接MASTER以便建立主从复制关系。

24>.SELECT权限

  代表允许从表中查看数据,某些不查询表数据的SELECT执行则不需要此权限,如SELECT 1+1,SELECT PI() +5 等等;而且SELECT权限在执行UPDATA/DELETE语句中含有WHERE条件的情况下也是需要的。

  1. mysql> SELECT PI()+5;
  2. +----------+
  3. | PI()+5 |
  4. +----------+
  5. | 8.141593 |
  6. +----------+
  7. 1 row in set (0.00 sec)
  8.  
  9. mysql>

mysql> SELECT PI()+5;

25>.SHOW DATABASES权限

  代表通过执行SHOW DATABASES名称查看所有的数据库名。

26>.SHOW VIEW权限

  代表通过执行SHOW CREATE VIEW命令查看视图创建的语句。

27>.SHUTDOWN权限

  代表允许关闭数据库实例,执行语句包括mysqladmin shutdown。

  1. [root@node105 ~]# ss -ntl
  2. State Recv-Q Send-Q Local Address:Port Peer Address:Port
  3. LISTEN 0 128 *:22 *:*
  4. LISTEN 0 128 :::3306 :::*
  5. LISTEN 0 128 :::22 :::*
  6. LISTEN 0 70 :::33060 :::*
  7. [root@node105 ~]#
  8. [root@node105 ~]#
  9. [root@node105 ~]# mysqladmin -uroot -pyinzhengjie shutdown
  10. mysqladmin: [Warning] Using a password on the command line interface can be insecure.
  11. [root@node105 ~]#
  12. [root@node105 ~]# ss -ntl
  13. State Recv-Q Send-Q Local Address:Port Peer Address:Port
  14. LISTEN 0 128 *:22 *:*
  15. LISTEN 0 128 :::22 :::*
  16. [root@node105 ~]#
  17. [root@node105 ~]#

[root@node105 ~]# mysqladmin -uroot -pyinzhengjie shutdown

28>.SUPER权限

  代表允许执行一系列数据库管理命令,包括kill强制关闭某个连接命令,CHANGE MASTER TO 创建复制关系命令,以及CRETE/ALTER/DROP SERVER等命令。

29>.TRIGGER权限

  代表允许创建,删除,执行,现实触发器等权限。

30>.UPADTE权限

  代表允许修改表中等数据等权限。

31>.USAGE权限

  它是创建一个用户之后等默认权限,其本身代表连接登陆权限。

  1. mysql> CREATE USER yinzhengjie@node105.yinzhengjie.org.cn;
  2. Query OK, 0 rows affected (0.01 sec)
  3.  
  4. mysql> SHOW GRANTS FOR yinzhengjie@node105.yinzhengjie.org.cn;
  5. +------------------------------------------------------------------+
  6. | Grants for yinzhengjie@node105.yinzhengjie.org.cn |
  7. +------------------------------------------------------------------+
  8. | GRANT USAGE ON *.* TO `yinzhengjie`@`node105.yinzhengjie.org.cn` |
  9. +------------------------------------------------------------------+
  10. 1 row in set (0.00 sec)
  11.  
  12. mysql>

mysql> SHOW GRANTS FOR yinzhengjie@node105.yinzhengjie.org.cn;

四.系统权限表

1>.权限存储在mysql库的user,db,tables_priv,columns_priv和procs_priv这5个系统表中。待MySQL实力启动成功后就家在到内存中。

  1. User表:
  2. 存放用户账户信息以及全局级别(所有数据库)权限,决定了 来自哪些主机的哪些用户可以访问数据库实例,如果有全局权限则意味着对所有数据库都有此权限。
  3.  
  4. Db表:
  5. 存放数据库级别的权限,决定了来自哪些主机的哪些用户可以访 问此数据库。
  6.  
  7. Tables_priv表:
  8. 存放表级别的权限,决定了来自哪些主机的哪些用户可以 访问数据库的这个表。
  9.  
  10. Columns_priv表:
  11. 存放列(字段)级别的权限,决定了来自哪些主机的哪些用户可 以访问数据库表的这个字段。
  12.  
  13. Procs_priv表:
  14. 存放存储过程和函数级别的权限。

2>.user和db权限表结构

  1. mysql> desc mysql.user\G
  2. *************************** 1. row ***************************
  3. Field: Host
  4. Type: char(60)
  5. Null: NO
  6. Key: PRI
  7. Default:
  8. Extra:
  9. *************************** 2. row ***************************
  10. Field: User
  11. Type: char(32)
  12. Null: NO
  13. Key: PRI
  14. Default:
  15. Extra:
  16. *************************** 3. row ***************************
  17. Field: Select_priv
  18. Type: enum('N','Y')
  19. Null: NO
  20. Key:
  21. Default: N
  22. Extra:
  23. *************************** 4. row ***************************
  24. Field: Insert_priv
  25. Type: enum('N','Y')
  26. Null: NO
  27. Key:
  28. Default: N
  29. Extra:
  30. *************************** 5. row ***************************
  31. Field: Update_priv
  32. Type: enum('N','Y')
  33. Null: NO
  34. Key:
  35. Default: N
  36. Extra:
  37. *************************** 6. row ***************************
  38. Field: Delete_priv
  39. Type: enum('N','Y')
  40. Null: NO
  41. Key:
  42. Default: N
  43. Extra:
  44. *************************** 7. row ***************************
  45. Field: Create_priv
  46. Type: enum('N','Y')
  47. Null: NO
  48. Key:
  49. Default: N
  50. Extra:
  51. *************************** 8. row ***************************
  52. Field: Drop_priv
  53. Type: enum('N','Y')
  54. Null: NO
  55. Key:
  56. Default: N
  57. Extra:
  58. *************************** 9. row ***************************
  59. Field: Reload_priv
  60. Type: enum('N','Y')
  61. Null: NO
  62. Key:
  63. Default: N
  64. Extra:
  65. *************************** 10. row ***************************
  66. Field: Shutdown_priv
  67. Type: enum('N','Y')
  68. Null: NO
  69. Key:
  70. Default: N
  71. Extra:
  72. *************************** 11. row ***************************
  73. Field: Process_priv
  74. Type: enum('N','Y')
  75. Null: NO
  76. Key:
  77. Default: N
  78. Extra:
  79. *************************** 12. row ***************************
  80. Field: File_priv
  81. Type: enum('N','Y')
  82. Null: NO
  83. Key:
  84. Default: N
  85. Extra:
  86. *************************** 13. row ***************************
  87. Field: Grant_priv
  88. Type: enum('N','Y')
  89. Null: NO
  90. Key:
  91. Default: N
  92. Extra:
  93. *************************** 14. row ***************************
  94. Field: References_priv
  95. Type: enum('N','Y')
  96. Null: NO
  97. Key:
  98. Default: N
  99. Extra:
  100. *************************** 15. row ***************************
  101. Field: Index_priv
  102. Type: enum('N','Y')
  103. Null: NO
  104. Key:
  105. Default: N
  106. Extra:
  107. *************************** 16. row ***************************
  108. Field: Alter_priv
  109. Type: enum('N','Y')
  110. Null: NO
  111. Key:
  112. Default: N
  113. Extra:
  114. *************************** 17. row ***************************
  115. Field: Show_db_priv
  116. Type: enum('N','Y')
  117. Null: NO
  118. Key:
  119. Default: N
  120. Extra:
  121. *************************** 18. row ***************************
  122. Field: Super_priv
  123. Type: enum('N','Y')
  124. Null: NO
  125. Key:
  126. Default: N
  127. Extra:
  128. *************************** 19. row ***************************
  129. Field: Create_tmp_table_priv
  130. Type: enum('N','Y')
  131. Null: NO
  132. Key:
  133. Default: N
  134. Extra:
  135. *************************** 20. row ***************************
  136. Field: Lock_tables_priv
  137. Type: enum('N','Y')
  138. Null: NO
  139. Key:
  140. Default: N
  141. Extra:
  142. *************************** 21. row ***************************
  143. Field: Execute_priv
  144. Type: enum('N','Y')
  145. Null: NO
  146. Key:
  147. Default: N
  148. Extra:
  149. *************************** 22. row ***************************
  150. Field: Repl_slave_priv
  151. Type: enum('N','Y')
  152. Null: NO
  153. Key:
  154. Default: N
  155. Extra:
  156. *************************** 23. row ***************************
  157. Field: Repl_client_priv
  158. Type: enum('N','Y')
  159. Null: NO
  160. Key:
  161. Default: N
  162. Extra:
  163. *************************** 24. row ***************************
  164. Field: Create_view_priv
  165. Type: enum('N','Y')
  166. Null: NO
  167. Key:
  168. Default: N
  169. Extra:
  170. *************************** 25. row ***************************
  171. Field: Show_view_priv
  172. Type: enum('N','Y')
  173. Null: NO
  174. Key:
  175. Default: N
  176. Extra:
  177. *************************** 26. row ***************************
  178. Field: Create_routine_priv
  179. Type: enum('N','Y')
  180. Null: NO
  181. Key:
  182. Default: N
  183. Extra:
  184. *************************** 27. row ***************************
  185. Field: Alter_routine_priv
  186. Type: enum('N','Y')
  187. Null: NO
  188. Key:
  189. Default: N
  190. Extra:
  191. *************************** 28. row ***************************
  192. Field: Create_user_priv
  193. Type: enum('N','Y')
  194. Null: NO
  195. Key:
  196. Default: N
  197. Extra:
  198. *************************** 29. row ***************************
  199. Field: Event_priv
  200. Type: enum('N','Y')
  201. Null: NO
  202. Key:
  203. Default: N
  204. Extra:
  205. *************************** 30. row ***************************
  206. Field: Trigger_priv
  207. Type: enum('N','Y')
  208. Null: NO
  209. Key:
  210. Default: N
  211. Extra:
  212. *************************** 31. row ***************************
  213. Field: Create_tablespace_priv
  214. Type: enum('N','Y')
  215. Null: NO
  216. Key:
  217. Default: N
  218. Extra:
  219. *************************** 32. row ***************************
  220. Field: ssl_type
  221. Type: enum('','ANY','X509','SPECIFIED')
  222. Null: NO
  223. Key:
  224. Default:
  225. Extra:
  226. *************************** 33. row ***************************
  227. Field: ssl_cipher
  228. Type: blob
  229. Null: NO
  230. Key:
  231. Default: NULL
  232. Extra:
  233. *************************** 34. row ***************************
  234. Field: x509_issuer
  235. Type: blob
  236. Null: NO
  237. Key:
  238. Default: NULL
  239. Extra:
  240. *************************** 35. row ***************************
  241. Field: x509_subject
  242. Type: blob
  243. Null: NO
  244. Key:
  245. Default: NULL
  246. Extra:
  247. *************************** 36. row ***************************
  248. Field: max_questions
  249. Type: int(11) unsigned
  250. Null: NO
  251. Key:
  252. Default: 0
  253. Extra:
  254. *************************** 37. row ***************************
  255. Field: max_updates
  256. Type: int(11) unsigned
  257. Null: NO
  258. Key:
  259. Default: 0
  260. Extra:
  261. *************************** 38. row ***************************
  262. Field: max_connections
  263. Type: int(11) unsigned
  264. Null: NO
  265. Key:
  266. Default: 0
  267. Extra:
  268. *************************** 39. row ***************************
  269. Field: max_user_connections
  270. Type: int(11) unsigned
  271. Null: NO
  272. Key:
  273. Default: 0
  274. Extra:
  275. *************************** 40. row ***************************
  276. Field: plugin
  277. Type: char(64)
  278. Null: NO
  279. Key:
  280. Default: caching_sha2_password
  281. Extra:
  282. *************************** 41. row ***************************
  283. Field: authentication_string
  284. Type: text
  285. Null: YES
  286. Key:
  287. Default: NULL
  288. Extra:
  289. *************************** 42. row ***************************
  290. Field: password_expired
  291. Type: enum('N','Y')
  292. Null: NO
  293. Key:
  294. Default: N
  295. Extra:
  296. *************************** 43. row ***************************
  297. Field: password_last_changed
  298. Type: timestamp
  299. Null: YES
  300. Key:
  301. Default: NULL
  302. Extra:
  303. *************************** 44. row ***************************
  304. Field: password_lifetime
  305. Type: smallint(5) unsigned
  306. Null: YES
  307. Key:
  308. Default: NULL
  309. Extra:
  310. *************************** 45. row ***************************
  311. Field: account_locked
  312. Type: enum('N','Y')
  313. Null: NO
  314. Key:
  315. Default: N
  316. Extra:
  317. *************************** 46. row ***************************
  318. Field: Create_role_priv
  319. Type: enum('N','Y')
  320. Null: NO
  321. Key:
  322. Default: N
  323. Extra:
  324. *************************** 47. row ***************************
  325. Field: Drop_role_priv
  326. Type: enum('N','Y')
  327. Null: NO
  328. Key:
  329. Default: N
  330. Extra:
  331. *************************** 48. row ***************************
  332. Field: Password_reuse_history
  333. Type: smallint(5) unsigned
  334. Null: YES
  335. Key:
  336. Default: NULL
  337. Extra:
  338. *************************** 49. row ***************************
  339. Field: Password_reuse_time
  340. Type: smallint(5) unsigned
  341. Null: YES
  342. Key:
  343. Default: NULL
  344. Extra:
  345. *************************** 50. row ***************************
  346. Field: Password_require_current
  347. Type: enum('N','Y')
  348. Null: YES
  349. Key:
  350. Default: NULL
  351. Extra:
  352. *************************** 51. row ***************************
  353. Field: User_attributes
  354. Type: json
  355. Null: YES
  356. Key:
  357. Default: NULL
  358. Extra:
  359. 51 rows in set (0.00 sec)
  360.  
  361. mysql>

查看user的表结构(mysql> desc mysql.user\G)

  1. mysql> desc mysql.db\G
  2. *************************** 1. row ***************************
  3. Field: Host
  4. Type: char(60)
  5. Null: NO
  6. Key: PRI
  7. Default:
  8. Extra:
  9. *************************** 2. row ***************************
  10. Field: Db
  11. Type: char(64)
  12. Null: NO
  13. Key: PRI
  14. Default:
  15. Extra:
  16. *************************** 3. row ***************************
  17. Field: User
  18. Type: char(32)
  19. Null: NO
  20. Key: PRI
  21. Default:
  22. Extra:
  23. *************************** 4. row ***************************
  24. Field: Select_priv
  25. Type: enum('N','Y')
  26. Null: NO
  27. Key:
  28. Default: N
  29. Extra:
  30. *************************** 5. row ***************************
  31. Field: Insert_priv
  32. Type: enum('N','Y')
  33. Null: NO
  34. Key:
  35. Default: N
  36. Extra:
  37. *************************** 6. row ***************************
  38. Field: Update_priv
  39. Type: enum('N','Y')
  40. Null: NO
  41. Key:
  42. Default: N
  43. Extra:
  44. *************************** 7. row ***************************
  45. Field: Delete_priv
  46. Type: enum('N','Y')
  47. Null: NO
  48. Key:
  49. Default: N
  50. Extra:
  51. *************************** 8. row ***************************
  52. Field: Create_priv
  53. Type: enum('N','Y')
  54. Null: NO
  55. Key:
  56. Default: N
  57. Extra:
  58. *************************** 9. row ***************************
  59. Field: Drop_priv
  60. Type: enum('N','Y')
  61. Null: NO
  62. Key:
  63. Default: N
  64. Extra:
  65. *************************** 10. row ***************************
  66. Field: Grant_priv
  67. Type: enum('N','Y')
  68. Null: NO
  69. Key:
  70. Default: N
  71. Extra:
  72. *************************** 11. row ***************************
  73. Field: References_priv
  74. Type: enum('N','Y')
  75. Null: NO
  76. Key:
  77. Default: N
  78. Extra:
  79. *************************** 12. row ***************************
  80. Field: Index_priv
  81. Type: enum('N','Y')
  82. Null: NO
  83. Key:
  84. Default: N
  85. Extra:
  86. *************************** 13. row ***************************
  87. Field: Alter_priv
  88. Type: enum('N','Y')
  89. Null: NO
  90. Key:
  91. Default: N
  92. Extra:
  93. *************************** 14. row ***************************
  94. Field: Create_tmp_table_priv
  95. Type: enum('N','Y')
  96. Null: NO
  97. Key:
  98. Default: N
  99. Extra:
  100. *************************** 15. row ***************************
  101. Field: Lock_tables_priv
  102. Type: enum('N','Y')
  103. Null: NO
  104. Key:
  105. Default: N
  106. Extra:
  107. *************************** 16. row ***************************
  108. Field: Create_view_priv
  109. Type: enum('N','Y')
  110. Null: NO
  111. Key:
  112. Default: N
  113. Extra:
  114. *************************** 17. row ***************************
  115. Field: Show_view_priv
  116. Type: enum('N','Y')
  117. Null: NO
  118. Key:
  119. Default: N
  120. Extra:
  121. *************************** 18. row ***************************
  122. Field: Create_routine_priv
  123. Type: enum('N','Y')
  124. Null: NO
  125. Key:
  126. Default: N
  127. Extra:
  128. *************************** 19. row ***************************
  129. Field: Alter_routine_priv
  130. Type: enum('N','Y')
  131. Null: NO
  132. Key:
  133. Default: N
  134. Extra:
  135. *************************** 20. row ***************************
  136. Field: Execute_priv
  137. Type: enum('N','Y')
  138. Null: NO
  139. Key:
  140. Default: N
  141. Extra:
  142. *************************** 21. row ***************************
  143. Field: Event_priv
  144. Type: enum('N','Y')
  145. Null: NO
  146. Key:
  147. Default: N
  148. Extra:
  149. *************************** 22. row ***************************
  150. Field: Trigger_priv
  151. Type: enum('N','Y')
  152. Null: NO
  153. Key:
  154. Default: N
  155. Extra:
  156. 22 rows in set (0.00 sec)
  157.  
  158. mysql>

查看db的表结构(mysql> desc mysql.db\G)

  1. User权限表结构中的特殊字段
  2. Plugin,password,authentication_string三个字段存放用户认证信息
  3. Password_expired设置成’Y’则表明允许DBA将此用户的密码设置成过期而 且过期后要求用户的使用者重置密码(alter user/set password重置密码)
  4. Password_last_changed作为一个时间戳字段代表密码上次修改时间,执 create user/alter user/set password/grant等命令创建用户或修改用户密 码时此数值自动更新
  5. Password_lifetime代表从password_last_changed时间开始此密码过期的天
  6. Account_locked代表此用户被锁住,无法使用

3>.tables_priv和columns_priv权限表结构

  1. mysql> desc mysql.tables_priv\G
  2. *************************** 1. row ***************************
  3. Field: Host
  4. Type: char(60)
  5. Null: NO
  6. Key: PRI
  7. Default:
  8. Extra:
  9. *************************** 2. row ***************************
  10. Field: Db
  11. Type: char(64)
  12. Null: NO
  13. Key: PRI
  14. Default:
  15. Extra:
  16. *************************** 3. row ***************************
  17. Field: User
  18. Type: char(32)
  19. Null: NO
  20. Key: PRI
  21. Default:
  22. Extra:
  23. *************************** 4. row ***************************
  24. Field: Table_name
  25. Type: char(64)
  26. Null: NO
  27. Key: PRI
  28. Default:
  29. Extra:
  30. *************************** 5. row ***************************
  31. Field: Grantor
  32. Type: char(93)
  33. Null: NO
  34. Key: MUL
  35. Default:
  36. Extra:
  37. *************************** 6. row ***************************
  38. Field: Timestamp
  39. Type: timestamp
  40. Null: NO
  41. Key:
  42. Default: CURRENT_TIMESTAMP
  43. Extra: DEFAULT_GENERATED on update CURRENT_TIMESTAMP
  44. *************************** 7. row ***************************
  45. Field: Table_priv
  46. Type: set('Select','Insert','Update','Delete','Create','Drop','Grant','References','Index','Alter','Create View','Show view','Trigger')
  47. Null: NO
  48. Key:
  49. Default:
  50. Extra:
  51. *************************** 8. row ***************************
  52. Field: Column_priv
  53. Type: set('Select','Insert','Update','References')
  54. Null: NO
  55. Key:
  56. Default:
  57. Extra:
  58. 8 rows in set (0.00 sec)
  59.  
  60. mysql>

查看tables_priv表结构,Grantor和Timestamp两个字段暂时没用(mysql> desc mysql.tables_priv\G)

  1. mysql> desc mysql.columns_priv\G
  2. *************************** 1. row ***************************
  3. Field: Host
  4. Type: char(60)
  5. Null: NO
  6. Key: PRI
  7. Default:
  8. Extra:
  9. *************************** 2. row ***************************
  10. Field: Db
  11. Type: char(64)
  12. Null: NO
  13. Key: PRI
  14. Default:
  15. Extra:
  16. *************************** 3. row ***************************
  17. Field: User
  18. Type: char(32)
  19. Null: NO
  20. Key: PRI
  21. Default:
  22. Extra:
  23. *************************** 4. row ***************************
  24. Field: Table_name
  25. Type: char(64)
  26. Null: NO
  27. Key: PRI
  28. Default:
  29. Extra:
  30. *************************** 5. row ***************************
  31. Field: Column_name
  32. Type: char(64)
  33. Null: NO
  34. Key: PRI
  35. Default:
  36. Extra:
  37. *************************** 6. row ***************************
  38. Field: Timestamp
  39. Type: timestamp
  40. Null: NO
  41. Key:
  42. Default: CURRENT_TIMESTAMP
  43. Extra: DEFAULT_GENERATED on update CURRENT_TIMESTAMP
  44. *************************** 7. row ***************************
  45. Field: Column_priv
  46. Type: set('Select','Insert','Update','References')
  47. Null: NO
  48. Key:
  49. Default:
  50. Extra:
  51. 7 rows in set (0.00 sec)
  52.  
  53. mysql>

查看columns_priv表结构mysql> desc mysql.columns_priv\G

  1. procs_priv权限表结构
  2. Routine_type是枚举类型,代表是存储过程还是函数
  3. Timestampgrantor两个字段暂时没用

4>.系统权限表字段长度限制表

5>.权限认证中的大小写铭感问题

  1. 字段user,password,authencation_string,db,table_name大小写敏感
    字段host,column_name,routine_name大小写不敏感
  1. mysql> CREATE USER yinzhengjie@node110.yinzhengjie.org.cn;
  2. Query OK, 0 rows affected (0.00 sec)
  3.  
  4. mysql>
  5. mysql> CREATE USER Yinzhengjie@node110.yinzhengjie.org.cn;
  6. Query OK, 0 rows affected (0.00 sec)
  7.  
  8. mysql>
  9. mysql> select User,Host from mysql.user where Host='node110.yinzhengjie.org.cn';
  10. +-------------+----------------------------+
  11. | User | Host |
  12. +-------------+----------------------------+
  13. | Yinzhengjie | node110.yinzhengjie.org.cn |
  14. | yinzhengjie | node110.yinzhengjie.org.cn |
  15. +-------------+----------------------------+
  16. 2 rows in set (0.00 sec)
  17.  
  18. mysql>
  19. mysql>

USER用户大小写铭感案例。欢聚话说,就是区分大小写。(mysql> CREATE USER Yinzhengjie@node105.yinzhengjie.org.cn; )

  1. mysql> CREATE USER jason@node110.yinzhengjie.org.cn;
  2. Query OK, 0 rows affected (0.00 sec)
  3.  
  4. mysql>
  5. mysql> CREATE USER jason@NODE110.yinzhengjie.org.cn; #这里报错了,说明MySQL的主机名是不区分大小写的!如果你写成大写他会默认给你转换成小写在user表中进行对比!
  6. ERROR 1396 (HY000): Operation CREATE USER failed for 'jason'@'node110.yinzhengjie.org.cn'
  7. mysql>
  8. mysql>
  9. mysql> select User,Host from mysql.user where Host='node110.yinzhengjie.org.cn';
  10. +-------------+----------------------------+
  11. | User | Host |
  12. +-------------+----------------------------+
  13. | Yinzhengjie | node110.yinzhengjie.org.cn |
  14. | jason | node110.yinzhengjie.org.cn |
  15. | yinzhengjie | node110.yinzhengjie.org.cn |
  16. +-------------+----------------------------+
  17. 3 rows in set (0.00 sec)
  18.  
  19. mysql>
  20. mysql>

Host主机名大小写不铭感。换句话说,就是不区分大小写(mysql> CREATE USER jason@node110.yinzhengjie.org.cn;)

6>.查看用户权限信息

  1. mysql> SHOW GRANTS FOR 'root'@'localhost'\G
  2. *************************** 1. row ***************************
  3. Grants for root@localhost: GRANT SELECT, INSERT, UPDATE, DELETE, CREATE, DROP, RELOAD, SHUTDOWN, PROCESS, FILE, REFERENCES, INDEX, ALTER, SHOW DATABASES, SUPER, CREATE TEMPORARY TABLES, LOCK TABLES, EXECUTE, REPLICATION SLAVE, REPLICATION CLIENT, CREATE VIEW, SHOW VIEW, CREATE ROUTINE, ALTER ROUTINE, CREATE USER, EVENT, TRIGGER, CREATE TABLESPACE, CREATE ROLE, DROP ROLE ON *.* TO `root`@`localhost` WITH GRANT OPTION
  4. *************************** 2. row ***************************
  5. Grants for root@localhost: GRANT APPLICATION_PASSWORD_ADMIN,BACKUP_ADMIN,BINLOG_ADMIN,BINLOG_ENCRYPTION_ADMIN,CONNECTION_ADMIN,ENCRYPTION_KEY_ADMIN,GROUP_REPLICATION_ADMIN,PERSIST_RO_VARIABLES_ADMIN,REPLICATION_SLAVE_ADMIN,RESOURCE_GROUP_ADMIN,RESOURCE_GROUP_USER,ROLE_ADMIN,SERVICE_CONNECTION_ADMIN,SESSION_VARIABLES_ADMIN,SET_USER_ID,SYSTEM_VARIABLES_ADMIN,XA_RECOVER_ADMIN ON *.* TO `root`@`localhost` WITH GRANT OPTION
  6. *************************** 3. row ***************************
  7. Grants for root@localhost: GRANT PROXY ON ''@'' TO 'root'@'localhost' WITH GRANT OPTION
  8. 3 rows in set (0.00 sec)
  9.  
  10. mysql>

查看已经授给用户的权限信息(mysql> SHOW GRANTS FOR 'root'@'localhost'\G)

  1. mysql> SHOW CREATE USER root@localhost\G
  2. *************************** 1. row ***************************
  3. CREATE USER for root@localhost: CREATE USER 'root'@'localhost' IDENTIFIED WITH 'caching_sha2_password' AS '$A$005$_DHTgn}dT9t%1>5eMM4wjrUWB.UY3A60WfUlqsZAVP0HhJ3Xxp1bFRs76g9B' REQUIRE NONE PASSWORD EXPIRE DEFAULT ACCOUNT UNLOCK PASSWORD HISTORY DEFAULT PASSWORD REUSE INTERVAL DEFAULT PASSWORD REQUIRE CURRENT DEFAULT
  4. 1 row in set (0.00 sec)
  5.  
  6. mysql>
  7. mysql>

查看用户的其他非授权信息(mysql> SHOW CREATE USER root@localhost\G)

五.MySQL授权用户

1>.MySQL授权用户的组成部分

  MySQL的授权用户由两部分组成,即用户名和登陆主机名。关于用户名和主机名需要遵循以下几点规则:

  1. 表达用户的语法为‘user_name’@‘host_name
  2. 单引号不是必须,但如果其中包含特殊字符则是必须的
  3. ‘’@‘localhost’代表匿名登录的用户
  4. Host_name可以使主机名或者ipv4/ipv6的地址。Localhost代表本机,127.0.0.1代表ipv4 本机地址,::1代表ipv6的本机地址
  5. Host_name字段允许使用%和_两个匹配字符,比如’%’代表所有主机,’%.mysql.com’代表 来自mysql.com这个域名下的所有主机,‘192.168.1.%’代表所有来自192.168.1网段的主机

2>.MySQL修改权限的生效周期

  1. 执行Grant,revoke,setpassword,renameuser命令修改权限之后,MySQL会自动将修改后的权限信息同步加载到系统内存中
  2. 如果执行insert/update/delete操作上述的系统权限表之后,则必须再执行刷 新权限命令才能同步到系统内存中,刷新权限命令包括:flush privileges/mysqladmin flush-privileges/mysqladmin reload
  3. 如果是修改tablescolumns级别的权限,则客户端的下次操作新权限就会生效
  4. 如果是修改database级别的权限,则新权限在客户端执行use database命令后生效
  5. 如果是修改global级别的权限,则需要重新创建连接新权限才能生效
  6. --skip-grant-tables可以跳过所有系统权限表而允许所有用户登录,只在特殊 情况下暂时使用

3>.MySQL用户连接各种姿势

  1. [root@node105 ~]# mysql --user=root --password mysql
  2. Enter password:
  3. Reading table information for completion of table and column names
  4. You can turn off this feature to get a quicker startup with -A
  5.  
  6. Welcome to the MySQL monitor. Commands end with ; or \g.
  7. Your MySQL connection id is 14
  8. Server version: 8.0.14 MySQL Community Server - GPL
  9.  
  10. Copyright (c) 2000, 2019, Oracle and/or its affiliates. All rights reserved.
  11.  
  12. Oracle is a registered trademark of Oracle Corporation and/or its
  13. affiliates. Other names may be trademarks of their respective
  14. owners.
  15.  
  16. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
  17.  
  18. mysql> select database();
  19. +------------+
  20. | database() |
  21. +------------+
  22. | mysql |
  23. +------------+
  24. 1 row in set (0.00 sec)
  25.  
  26. mysql>
  27. mysql> quit
  28. Bye
  29. [root@node105 ~]#
  30. [root@node105 ~]#

姿势一,输入完整的命令参数连接指定的数据库([root@node105 ~]# mysql --user=root --password mysql)

  1. [root@node105 ~]# mysql --user=root -p mysql
  2. Enter password:
  3. Reading table information for completion of table and column names
  4. You can turn off this feature to get a quicker startup with -A
  5.  
  6. Welcome to the MySQL monitor. Commands end with ; or \g.
  7. Your MySQL connection id is 15
  8. Server version: 8.0.14 MySQL Community Server - GPL
  9.  
  10. Copyright (c) 2000, 2019, Oracle and/or its affiliates. All rights reserved.
  11.  
  12. Oracle is a registered trademark of Oracle Corporation and/or its
  13. affiliates. Other names may be trademarks of their respective
  14. owners.
  15.  
  16. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
  17.  
  18. mysql> select database();
  19. +------------+
  20. | database() |
  21. +------------+
  22. | mysql |
  23. +------------+
  24. 1 row in set (0.00 sec)
  25.  
  26. mysql> quit
  27. Bye
  28. [root@node105 ~]#

姿势二,对姿势一对password参数简写形式([root@node105 ~]# mysql --user=root -p mysql )

  1. [root@node105 ~]# mysql --user=root --password=yinzhengjie mysql
  2. mysql: [Warning] Using a password on the command line interface can be insecure.
  3. Reading table information for completion of table and column names
  4. You can turn off this feature to get a quicker startup with -A
  5.  
  6. Welcome to the MySQL monitor. Commands end with ; or \g.
  7. Your MySQL connection id is 16
  8. Server version: 8.0.14 MySQL Community Server - GPL
  9.  
  10. Copyright (c) 2000, 2019, Oracle and/or its affiliates. All rights reserved.
  11.  
  12. Oracle is a registered trademark of Oracle Corporation and/or its
  13. affiliates. Other names may be trademarks of their respective
  14. owners.
  15.  
  16. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
  17.  
  18. mysql> select database();
  19. +------------+
  20. | database() |
  21. +------------+
  22. | mysql |
  23. +------------+
  24. 1 row in set (0.00 sec)
  25.  
  26. mysql> quit
  27. Bye
  28. [root@node105 ~]#
  29. [root@node105 ~]# history | tail -5
  30. 282 mysql --user=yinzhengjie@node105.yinzhengjie.org.cn --password mysql
  31. 283 mysql --user=root --password mysql
  32. 284 mysql --user=root -p mysql
  33. 285 mysql --user=root --password=yinzhengjie mysql #密码被history记录住了
  34. 286 history | tail -5
  35. [root@node105 ~]#

姿势三,输入完整的命令参数连接指定的数据库,容易报漏密码给history!不推荐使用([root@node105 ~]# mysql --user=root --password=yinzhengjie mysql)

  1. [root@node105 ~]# mysql -uroot -pyinzhengjie mysql
  2. mysql: [Warning] Using a password on the command line interface can be insecure.
  3. Reading table information for completion of table and column names
  4. You can turn off this feature to get a quicker startup with -A
  5.  
  6. Welcome to the MySQL monitor. Commands end with ; or \g.
  7. Your MySQL connection id is 17
  8. Server version: 8.0.14 MySQL Community Server - GPL
  9.  
  10. Copyright (c) 2000, 2019, Oracle and/or its affiliates. All rights reserved.
  11.  
  12. Oracle is a registered trademark of Oracle Corporation and/or its
  13. affiliates. Other names may be trademarks of their respective
  14. owners.
  15.  
  16. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
  17.  
  18. mysql> select database();
  19. +------------+
  20. | database() |
  21. +------------+
  22. | mysql |
  23. +------------+
  24. 1 row in set (0.00 sec)
  25.  
  26. mysql> quit;
  27. Bye
  28. [root@node105 ~]# history | tail -2
  29. 289 mysql -uroot -pyinzhengjie mysql
  30. 290 history | tail -2
  31. [root@node105 ~]#

姿势四,对姿势三参数的简写形式,由于容易报漏密码,俺不推荐大家使用这种方式,如果在测试环境下使用倒也无所谓([root@node105 ~]# mysql -uroot -pyinzhengjie mysql)

4>.创建MySQL用户案例展示

  有两种创建MySQL授权用户:

    姿势一 :执行CREATE USER/GRANT命令(博主推荐)

    姿势二 :通过INSERT语句直接操作MySQL系统权限表(不推荐使用)

  1. mysql> SELECT User,Host from mysql.user;
  2. +------------------+-----------+
  3. | User | Host |
  4. +------------------+-----------+
  5. | mysql.infoschema | localhost |
  6. | mysql.session | localhost |
  7. | mysql.sys | localhost |
  8. | root | localhost |
  9. +------------------+-----------+
  10. 4 rows in set (0.00 sec)
  11.  
  12. mysql>
  13. mysql>
  14. mysql> CREATE USER 'jason'@'node110.yinzhengjie.org.cn' IDENTIFIED BY 'yinzhengjie';
  15. Query OK, 0 rows affected (0.00 sec)
  16.  
  17. mysql>
  18. mysql> SELECT User,Host from mysql.user;
  19. +------------------+----------------------------+
  20. | User | Host |
  21. +------------------+----------------------------+
  22. | mysql.infoschema | localhost |
  23. | mysql.session | localhost |
  24. | mysql.sys | localhost |
  25. | root | localhost |
  26. | jason | node110.yinzhengjie.org.cn |
  27. +------------------+----------------------------+
  28. 5 rows in set (0.00 sec)
  29.  
  30. mysql>

创建用户(mysql> CREATE USER 'jason'@'node110.yinzhengjie.org.cn' IDENTIFIED BY 'yinzhengjie';)

  1. mysql> SHOW GRANTS FOR 'jason'@'node110.yinzhengjie.org.cn';
  2. +------------------------------------------------------------+
  3. | Grants for jason@node110.yinzhengjie.org.cn |
  4. +------------------------------------------------------------+
  5. | GRANT USAGE ON *.* TO `jason`@`node110.yinzhengjie.org.cn` |
  6. +------------------------------------------------------------+
  7. 1 row in set (0.00 sec)
  8.  
  9. mysql>

创建用户后,查看该用户的默认权限(mysql> SHOW GRANTS FOR 'jason'@'node110.yinzhengjie.org.cn';)

  1. mysql> SHOW GRANTS FOR 'jason'@'node110.yinzhengjie.org.cn';
  2. +------------------------------------------------------------+
  3. | Grants for jason@node110.yinzhengjie.org.cn |
  4. +------------------------------------------------------------+
  5. | GRANT USAGE ON *.* TO `jason`@`node110.yinzhengjie.org.cn` |
  6. +------------------------------------------------------------+
  7. row in set (0.00 sec)
  8.  
  9. mysql>
  10. mysql> CREATE DATABASE yinzhengjie;
  11. Query OK, 1 row affected (0.00 sec)
  12.  
  13. mysql> show databases;
  14. +--------------------+
  15. | Database |
  16. +--------------------+
  17. | information_schema |
  18. | mysql |
  19. | performance_schema |
  20. | sys |
  21. | yinzhengjie |
  22. +--------------------+
  23. rows in set (0.00 sec)
  24.  
  25. mysql>
  26. mysql> GRANT ALL PRIVILEGES ON yinzhengjie.* TO `jason`@`node110.yinzhengjie.org.cn` WITH GRANT OPTION;
  27. Query OK, 0 rows affected (0.00 sec)
  28.  
  29. mysql>
  30. mysql> SHOW GRANTS FOR 'jason'@'node110.yinzhengjie.org.cn';
  31. +--------------------------------------------------------------------------------------------------+
  32. | Grants for jason@node110.yinzhengjie.org.cn |
  33. +--------------------------------------------------------------------------------------------------+
  34. | GRANT USAGE ON *.* TO `jason`@`node110.yinzhengjie.org.cn` |
  35. | GRANT ALL PRIVILEGES ON `yinzhengjie`.* TO `jason`@`node110.yinzhengjie.org.cn` WITH GRANT OPTION |
  36. +--------------------------------------------------------------------------------------------------+
  37. rows in set (0.00 sec)
  38.  
  39. mysql>

将自定义的yinzhengjie数据库授权给jason@node110.yinzhengjie.org.cn用户,并且该用户还可以把这个权限授权给其他用户哟!(mysql> GRANT ALL PRIVILEGES ON yinzhengjie.* TO `jason`@`node110.yinzhengjie.org.cn` WITH GRANT OPTION;)

  1. [root@node110 ~]# hostname
  2. node110.yinzhengjie.org.cn
  3. [root@node110 ~]#
  4. [root@node110 ~]# hostname -i
  5. 172.30.1.110
  6. [root@node110 ~]#
  7. [root@node110 ~]# cat /etc/hosts | grep yinzhengjie
  8. 172.30.1.101 node101.yinzhengjie.org.cn
  9. 172.30.1.102 node102.yinzhengjie.org.cn
  10. 172.30.1.103 node103.yinzhengjie.org.cn
  11. 172.30.1.105 node105.yinzhengjie.org.cn
  12. 172.30.1.110 node110.yinzhengjie.org.cn
  13. [root@node110 ~]#
  14. [root@node110 ~]#
  15. [root@node110 ~]# mysql -h node105.yinzhengjie.org.cn -ujason -pyinzhengjie
  16. mysql: [Warning] Using a password on the command line interface can be insecure.
  17. Welcome to the MySQL monitor. Commands end with ; or \g.
  18. Your MySQL connection id is 21
  19. Server version: 8.0.14 MySQL Community Server - GPL
  20.  
  21. Copyright (c) 2000, 2019, Oracle and/or its affiliates. All rights reserved.
  22.  
  23. Oracle is a registered trademark of Oracle Corporation and/or its
  24. affiliates. Other names may be trademarks of their respective
  25. owners.
  26.  
  27. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
  28.  
  29. mysql> show databases;
  30. +--------------------+
  31. | Database |
  32. +--------------------+
  33. | information_schema |
  34. | yinzhengjie |
  35. +--------------------+
  36. 2 rows in set (0.00 sec)
  37.  
  38. mysql> use yinzhengjie;
  39. Database changed
  40. mysql>
  41. mysql> SELECT database();
  42. +-------------+
  43. | database() |
  44. +-------------+
  45. | yinzhengjie |
  46. +-------------+
  47. 1 row in set (0.00 sec)
  48.  
  49. mysql> show tables;
  50. Empty set (0.00 sec)
  51.  
  52. mysql> quit
  53. Bye
  54. [root@node110 ~]#
  55. [root@node110 ~]#

使用客户端(node110.yinzhengjie.org.cn)访问数据库服务器(node105.yinzhengjie.org.cn) [root@node110 ~]# mysql -h node105.yinzhengjie.org.cn -ujason -pyinzhengjie

5>.回收MySQL用户权限

  1. mysql> SHOW GRANTS FOR 'jason'@'node110.yinzhengjie.org.cn';
  2. +---------------------------------------------------------------------------------------------------+
  3. | Grants for jason@node110.yinzhengjie.org.cn |
  4. +---------------------------------------------------------------------------------------------------+
  5. | GRANT USAGE ON *.* TO `jason`@`node110.yinzhengjie.org.cn` |
  6. | GRANT ALL PRIVILEGES ON `yinzhengjie`.* TO `jason`@`node110.yinzhengjie.org.cn` WITH GRANT OPTION |
  7. +---------------------------------------------------------------------------------------------------+
  8. 2 rows in set (0.00 sec)
  9.  
  10. mysql>
  11. mysql> REVOKE SELECT,UPDATE,DELETE ON yinzhengjie.* FROM 'jason'@'node110.yinzhengjie.org.cn';
  12. Query OK, 0 rows affected (0.00 sec)
  13.  
  14. mysql> SHOW GRANTS FOR 'jason'@'node110.yinzhengjie.org.cn';
  15. +---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  16. | Grants for jason@node110.yinzhengjie.org.cn |
  17. +---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  18. | GRANT USAGE ON *.* TO `jason`@`node110.yinzhengjie.org.cn` |
  19. | GRANT INSERT, CREATE, DROP, REFERENCES, INDEX, ALTER, CREATE TEMPORARY TABLES, LOCK TABLES, EXECUTE, CREATE VIEW, SHOW VIEW, CREATE ROUTINE, ALTER ROUTINE, EVENT, TRIGGER ON `yinzhengjie`.* TO `jason`@`node110.yinzhengjie.org.cn` WITH GRANT OPTION |
  20. +---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  21. 2 rows in set (0.00 sec)
  22.  
  23. mysql>

通过REVOKE命令收回用户权限(mysql> REVOKE SELECT,UPDATE,DELETE ON yinzhengjie.* FROM 'jason'@'node110.yinzhengjie.org.cn';)

6>.删除MySQL用户

  1. mysql>
  2. mysql> SELECT User,Host from mysql.user;
  3. +------------------+----------------------------+
  4. | User | Host |
  5. +------------------+----------------------------+
  6. | mysql.infoschema | localhost |
  7. | mysql.session | localhost |
  8. | mysql.sys | localhost |
  9. | root | localhost |
  10. | jason | node110.yinzhengjie.org.cn |
  11. +------------------+----------------------------+
  12. 5 rows in set (0.00 sec)
  13.  
  14. mysql>
  15. mysql> DROP USER jason@node110.yinzhengjie.org.cn;
  16. Query OK, 0 rows affected (0.00 sec)
  17.  
  18. mysql>
  19. mysql> SELECT User,Host from mysql.user;
  20. +------------------+-----------+
  21. | User | Host |
  22. +------------------+-----------+
  23. | mysql.infoschema | localhost |
  24. | mysql.session | localhost |
  25. | mysql.sys | localhost |
  26. | root | localhost |
  27. +------------------+-----------+
  28. 4 rows in set (0.00 sec)
  29.  
  30. mysql>

通过执行DROP USER命令删除MySQL用户(mysql> DROP USER jason@node110.yinzhengjie.org.cn;)

7>.设置MySQL用户资源

  1. 通过设置全局变量max_user_connections可以限制所有用户在同一时间连接MySQL实例的数量,但此参数无法对每个用户区别对待,所以MySQL提供了对每个用户的资源限制管理
  2. MAX_QUERIES_PER_HOUR:一个用户在一个小时内可以执行查询的次数(基本包含所有语句)
  3. MAX_UPDATES_PER_HOUR:一个用户在一个小时内可以执行修改的次数(仅包含修改数据库或表的语句)
  4. MAX_CONNECTIONS_PER_HOUR:一个用户在一个小时内可以连接MySQL的时间

  5. MAX_USER_CONNECTIONS:一个用户可以在同一时间连接MySQL实例的数量,注意,当针对某个用户当MAX_USER_CONNECTIONS0时,则忽略全局系统参数MAX_USER_CONNECTIONS,反之则全局系统参数生效!

  6. 5.0.3版本开始,对用户‘user’@‘%.example.com’的资源限制是指所有 通过example.com域名主机连接user用户的连接,而不是分别指从 host1.example.comhost2.example.com主机过来的连接 
  1. mysql> SELECT User,Host from mysql.user;
  2. +------------------+-----------+
  3. | User | Host |
  4. +------------------+-----------+
  5. | mysql.infoschema | localhost |
  6. | mysql.session | localhost |
  7. | mysql.sys | localhost |
  8. | root | localhost |
  9. +------------------+-----------+
  10. 4 rows in set (0.00 sec)
  11.  
  12. mysql>
  13. mysql> CREATE USER 'jason'@'node110.yinzhengjie.org.cn' IDENTIFIED BY 'yinzhengjie'
  14. -> WITH MAX_QUERIES_PER_HOUR 20
  15. -> MAX_UPDATES_PER_HOUR 5
  16. -> MAX_CONNECTIONS_PER_HOUR 3
  17. -> MAX_USER_CONNECTIONS 2;
  18. Query OK, 0 rows affected (0.00 sec)
  19.  
  20. mysql>
  21. mysql> SELECT User,Host from mysql.user;
  22. +------------------+----------------------------+
  23. | User | Host |
  24. +------------------+----------------------------+
  25. | mysql.infoschema | localhost |
  26. | mysql.session | localhost |
  27. | mysql.sys | localhost |
  28. | root | localhost |
  29. | jason | node110.yinzhengjie.org.cn |
  30. +------------------+----------------------------+
  31. 5 rows in set (0.00 sec)
  32.  
  33. mysql>

在创建MySQL用户时指定MySQL资源限制(mysql> CREATE USER 'jason'@'node110.yinzhengjie.org.cn' IDENTIFIED BY 'yinzhengjie' WITH MAX_QUERIES_PER_HOUR 20 MAX_UPDATES_PER_HOUR 5 MAX_CONNECTIONS_PER_HOUR 3 MAX_USER_CONNECTIONS 2;)

  1. mysql> ALTER USER jason@node110.yinzhengjie.org.cn WITH MAX_USER_CONNECTIONS 5;
  2. Query OK, 0 rows affected (0.01 sec)
  3.  
  4. mysql>

对已经创建的用户进行资源限制(mysql> ALTER USER jason@node110.yinzhengjie.org.cn WITH MAX_USER_CONNECTIONS 5;)

  1. mysql> ALTER USER jason@node110.yinzhengjie.org.cn WITH MAX_USER_CONNECTIONS 0;
  2. Query OK, 0 rows affected (0.01 sec)
  3.  
  4. mysql>

取消某项资源限制既是把原先的值修改成0(mysql> ALTER USER jason@node110.yinzhengjie.org.cn WITH MAX_USER_CONNECTIONS 0;)

8>.设置MySQL用户当密码

  1. mysql> SELECT User,Host from mysql.user;
  2. +------------------+----------------------------+
  3. | User | Host |
  4. +------------------+----------------------------+
  5. | mysql.infoschema | localhost |
  6. | mysql.session | localhost |
  7. | mysql.sys | localhost |
  8. | root | localhost |
  9. | jason | node110.yinzhengjie.org.cn |
  10. +------------------+----------------------------+
  11. 5 rows in set (0.00 sec)
  12.  
  13. mysql>
  14. mysql> CREATE USER 'yinzhengjie'@'node110.yinzhengjie.org.cn' IDENTIFIED BY 'yinzhengjie';
  15. Query OK, 0 rows affected (0.00 sec)
  16.  
  17. mysql>
  18. mysql> SELECT User,Host from mysql.user;
  19. +------------------+----------------------------+
  20. | User | Host |
  21. +------------------+----------------------------+
  22. | mysql.infoschema | localhost |
  23. | mysql.session | localhost |
  24. | mysql.sys | localhost |
  25. | root | localhost |
  26. | jason | node110.yinzhengjie.org.cn |
  27. | yinzhengjie | node110.yinzhengjie.org.cn |
  28. +------------------+----------------------------+
  29. 6 rows in set (0.00 sec)
  30.  
  31. mysql>

姿势一,执行CREATE USER创建用户和密码(mysql> CREATE USER 'yinzhengjie'@'node110.yinzhengjie.org.cn' IDENTIFIED BY 'yinzhengjie';)

  1. mysql> ALTER USER jason@node110.yinzhengjie.org.cn IDENTIFIED BY 'yinzhengjie2019';
  2. Query OK, 0 rows affected (0.01 sec)
  3.  
  4. mysql>

姿势二,修改已经存在的MySQL用户的密码(mysql> ALTER USER jason@node110.yinzhengjie.org.cn IDENTIFIED BY 'yinzhengjie2019';)

  1. mysql> SELECT USER();
  2. +----------------+
  3. | USER() |
  4. +----------------+
  5. | root@localhost |
  6. +----------------+
  7. 1 row in set (0.00 sec)
  8.  
  9. mysql>
  10. mysql> ALTER USER USER() IDENTIFIED BY 'yinzhengjie';
  11. Query OK, 0 rows affected (0.01 sec)
  12.  
  13. mysql>

姿势三,修改当前的登陆用户名的密码(mysql> ALTER USER USER() IDENTIFIED BY 'yinzhengjie';)

  注意,MySQL8.0以后的版本,不支持使用 SET PASSWORD FOR jason@node110.yinzhengjie.org.cn = PASSWORD('yinzhengjie'); 这样的语句修改代码了,使用MySQL5.7的小伙伴们得注意一下了哟~当然,如果你通过mysqladmin的方式修改MySQL密码也是一种方式,但是博主不推荐哟~别忘记Linux中又一个history功能哟!

9>.设置MySQL用户密码过期策略

  设置系统参数default_password_lifetime作用于所有的用户账户
  1. default_password_lifetime=180 设置180天过期
  2.  
  3. default_password_lifetime=0 设置密码不过期

  如果为每个用户设置了密码过期策略,则会覆盖上述系统参数

  1. ALTER USER 'jason'@'node101.yinzhengjie.org.cn' PASSWORD EXPIRE INTERVAL 90 DAY;
  2. ALTER USER ‘jason’@‘node102.yinzhengjie.org.cn’ PASSWORD EXPIRE NEVER;        密码不过期

  3. ALTER USER ‘jason’@‘node103.yinzhengjie.org.cn’ PASSWORD EXPIRE DEFAULT;       默认过期策略

  手动强制某个用户密码过期

  1. ALTER  USER  'jason'@'node105.yinzhengjie.org.cn'  PASSWORD  EXPIRE;

10>.MySQL用户lock

  1. mysql> CREATE USER yzj@node110.yinzhengjie.org.cn IDENTIFIED BY 'yinzhengjie' ACCOUNT LOCK;
  2. Query OK, 0 rows affected (0.01 sec)
  3.  
  4. mysql>

通过执行CREATE USER语句默认的用户是unlock状态(mysql> CREATE USER yzj@node110.yinzhengjie.org.cn IDENTIFIED BY 'yinzhengjie' ACCOUNT LOCK;)

  1. mysql> SELECT User,Host from mysql.user;
  2. +------------------+----------------------------+
  3. | User | Host |
  4. +------------------+----------------------------+
  5. | mysql.infoschema | localhost |
  6. | mysql.session | localhost |
  7. | mysql.sys | localhost |
  8. | root | localhost |
  9. | jason | node110.yinzhengjie.org.cn |
  10. | yinzhengjie | node110.yinzhengjie.org.cn |
  11. | yzj | node110.yinzhengjie.org.cn |
  12. +------------------+----------------------------+
  13. 7 rows in set (0.00 sec)
  14.  
  15. mysql>
  16. mysql> ALTER USER yinzhengjie@node110.yinzhengjie.org.cn ACCOUNT LOCK;
  17. Query OK, 0 rows affected (0.01 sec)
  18.  
  19. mysql>

通过ALTER USER语句将已经存在的MySQL用户锁住(mysql> ALTER USER yinzhengjie@node110.yinzhengjie.org.cn ACCOUNT LOCK;)

  我们创建时就将用户锁住,那么其时无法登陆MySQL服务器的哟!连接时会提示该用户已经被锁入住,如下所示:

  1. [root@node110 ~]# mysql -h node105.yinzhengjie.org.cn -uyzj -pyinzhengjie
  2. mysql: [Warning] Using a password on the command line interface can be insecure.
  3. ERROR 3118 (HY000): Access denied for user 'yzj'@'node110.yinzhengjie.org.cn'. Account is locked.
  4. [root@node110 ~]#
  5. [root@node110 ~]#

  如果MySQL用户被锁住后,有人申请要解锁的话,其实也很简单,具体操作如下:

  1. mysql> ALTER USER yinzhengjie@node110.yinzhengjie.org.cn ACCOUNT UNLOCK;
  2. Query OK, 0 rows affected (0.00 sec)
  3.  
  4. mysql>

11>.企业应用中的常规MySQL用户

  MySQL用户的创建通常由DBA统一协调创建,而且按需创建;

  DBA通常直接使用root用户来管理数据库;

  通常会创建指定业务数据库上的增删改查、临时表、执行存储过程的权限给应 用程序来连接数据库;

  通常也会创建指定业务数据库上的只读权限给特定应用程序或某些高级别人员 来查询数据,防止数据被修改;

  在MySQL8.0引入了一个角色的概念,具体的SQL操作如下:

  1. mysql> CREATE ROLE app_readonly;                                    #创建一个app_readonly角色(组)
  2. Query OK, 0 rows affected (0.03 sec)
  3.  
  4. mysql>
  5. mysql> GRANT SELECT ON *.* TO app_readonly;                              #我们为创建的角色授予只读权限
  6. Query OK, 0 rows affected (0.00 sec)
  7.  
  8. mysql>
  9. mysql> CREATE USER apache@node105.yinzhengjie.org.cn IDENTIFIED BY 'yinzhengjie';       #我们创建一个用户
  10. Query OK, 0 rows affected (0.00 sec)
  11.  
  12. mysql>
  13. mysql> CREATE USER nginx@node105.yinzhengjie.org.cn IDENTIFIED BY 'yinzhengjie';
  14. Query OK, 0 rows affected (0.00 sec)
  15.  
  16. mysql>
  17. mysql> GRANT app_readonly TO apache@node105.yinzhengjie.org.cn ;                  #我们将角色的权限授予指定的用户
  18. Query OK, 0 rows affected (0.00 sec)
  19.  
  20. mysql>
  21. mysql> GRANT app_readonly TO nginx@node105.yinzhengjie.org.cn ;
  22. Query OK, 0 rows affected (0.00 sec)
  23.  
  24. mysql>
  25. mysql>
  26. mysql> CREATE ROLE app_readwrite;
  27. Query OK, 0 rows affected (0.00 sec)
  28.  
  29. mysql>
  30. mysql> GRANT SELECT,INSERT,DELETE,UPDATE ON *.* TO app_readwrite;
  31. Query OK, 0 rows affected (0.00 sec)
  32.  
  33. mysql>
  34. mysql>
  35. mysql> CREATE USER django@node105.yinzhengjie.org.cn IDENTIFIED BY 'yinzhengjie';
  36. Query OK, 0 rows affected (0.01 sec)
  37.  
  38. mysql>
  39. mysql> CREATE USER vue@node105.yinzhengjie.org.cn IDENTIFIED BY 'yinzhengjie';
  40. Query OK, 0 rows affected (0.01 sec)
  41.  
  42. mysql>
  43. mysql> GRANT app_readwrite TO django@node105.yinzhengjie.org.cn;
  44. Query OK, 0 rows affected (0.00 sec)
  45.  
  46. mysql>
  47. mysql> GRANT app_readwrite TO vue@node105.yinzhengjie.org.cn;
  48. Query OK, 0 rows affected (0.00 sec)
  49.  
  50. mysql>
  51. mysql> SHOW GRANTS FOR django@node105.yinzhengjie.org.cn;
  52. +--------------------------------------------------------------------+
  53. | Grants for django@node105.yinzhengjie.org.cn |
  54. +--------------------------------------------------------------------+
  55. | GRANT USAGE ON *.* TO `django`@`node105.yinzhengjie.org.cn` |
  56. | GRANT `app_readwrite`@`%` TO `django`@`node105.yinzhengjie.org.cn` |
  57. +--------------------------------------------------------------------+
  58. 2 rows in set (0.00 sec)
  59.  
  60. mysql>
  61. mysql> SHOW GRANTS FOR django@node105.yinzhengjie.org.cn USING app_readwrite;            #使用USING + 角色名称 就可以看到详细的权限信息了,和上面的查看权限的形成了鲜明的对比~
  62. +--------------------------------------------------------------------------------------+
  63. | Grants for django@node105.yinzhengjie.org.cn |
  64. +--------------------------------------------------------------------------------------+
  65. | GRANT SELECT, INSERT, UPDATE, DELETE ON *.* TO `django`@`node105.yinzhengjie.org.cn` |
  66. | GRANT `app_readwrite`@`%` TO `django`@`node105.yinzhengjie.org.cn` |
  67. +--------------------------------------------------------------------------------------+
  68. 2 rows in set (0.00 sec)
  69.  
  70. mysql>
  71. mysql> REVOKE app_readwrite FROM django@node105.yinzhengjie.org.cn;                  #我们可以收回权限
  72. Query OK, 0 rows affected (0.00 sec)
  73.  
  74. mysql>
  75. mysql> SHOW GRANTS FOR django@node105.yinzhengjie.org.cn;                        #当然我们也可以把多个角色赋值给同一个用户哟~
  76. +-------------------------------------------------------------+
  77. | Grants for django@node105.yinzhengjie.org.cn |
  78. +-------------------------------------------------------------+
  79. | GRANT USAGE ON *.* TO `django`@`node105.yinzhengjie.org.cn` |
  80. +-------------------------------------------------------------+
  81. 1 row in set (0.00 sec)
  82.  
  83. mysql>

12>.企业应用中的MySQL用户密码设定

  1. 企业生产系统中MySQL用户的密码设定有严格的规范,通常要有密码复杂度、密码长度等要求

  2. 搜索网上的密码生成器,能按要求生成随机密码
  3.  
  4.    http://suijimimashengcheng.51240.com/

MySQL权限授权认证详解的更多相关文章

  1. OAuth 2.0 授权认证详解

    一.认识 OAuth 2.0 1.1 OAuth 2.0 应用场景 OAuth 2.0 标准目前被广泛应用在第三方登录场景中,以下是虚拟出来的角色,阐述 OAuth2 能帮我们干什么,引用阮一峰这篇理 ...

  2. Shrio授权验证详解

    所谓授权,就是控制你是否能访问某个资源,比如说,你可以方位page文件夹下的jsp页面,但是不可以访问page文件夹下的admin文件夹下的jsp页面. 在授权中,有三个核心元素:权限,角色,用户. ...

  3. mysql学习3:mysql之my.cnf详解

    mysql之my.cnf详解 本文转自:https://www.cnblogs.com/panwenbin-logs/p/8360703.html 以下是 my.cnf 配置文件参数解释: #*** ...

  4. mysql配置文件my.cnf详解

    原文地址:mysql配置文件my.cnf详解 作者:gron basedir = path 使用给定目录作为根目录(安装目录). character-sets-dir = path 给出存放着字符集的 ...

  5. JWT(Json web token)认证详解

    JWT(Json web token)认证详解 什么是JWT Json web token (JWT), 是为了在网络应用环境间传递声明而执行的一种基于JSON的开放标准((RFC 7519).该to ...

  6. 转 OAuth 2.0授权协议详解

    http://www.jb51.net/article/54948.htm 作者:阮一峰 字体:[增加 减小] 类型:转载 时间:2014-09-10我要评论 这篇文章主要介绍了OAuth 2.0授权 ...

  7. SQL Server中通用数据库角色权限的处理详解

    SQL Server中通用数据库角色权限的处理详解 前言 安全性是所有数据库管理系统的一个重要特征.理解安全性问题是理解数据库管理系统安全性机制的前提. 最近和同事在做数据库权限清理的事情,主要是删除 ...

  8. MySQL 语句执行过程详解

    MySQL 原理篇 MySQL 索引机制 MySQL 体系结构及存储引擎 MySQL 语句执行过程详解 MySQL 执行计划详解 MySQL InnoDB 缓冲池 MySQL InnoDB 事务 My ...

  9. Python中操作mysql的pymysql模块详解

    Python中操作mysql的pymysql模块详解 前言 pymsql是Python中操作MySQL的模块,其使用方法和MySQLdb几乎相同.但目前pymysql支持python3.x而后者不支持 ...

随机推荐

  1. 找工作的程序员必懂的Linux

    一.为什么要学习Linux 首先,我想先说一下:“为什么要学习Linux”?Linux 是什么,它是一款操作系统,是一个支持多用户.多任务.支持多线程和多CPU的操作系统:32位和64位的硬件可以在L ...

  2. springmvc使用swagger生成rest api文档

    pom.xml <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-s ...

  3. windows新增/修改/删除系统环境变量bat示例,一键配置JAVA_HOME

    setx JAVA_HOME "C:\Program Files\java\jdk1.6.0_27" /m setx classpath = ".;%JAVA_HOME% ...

  4. int,String转换

    int -> String 第一种方法:s=i+""; //会产生两个String对象 第二种方法:s=String.valueOf(i); //直接使用String类的静态 ...

  5. js弹框的3种方法

    js的三种弹框的方法 1.第一种 :  alert("1"); 2.第二种 :  window.open("Tests2.html"); var r = con ...

  6. 各种反演细节梳理&模板

    炫酷反演魔术课件byVFK stO FDF Orz(证明全有%%%) 莫比乌斯反演 \(F(n)=\sum\limits_{d|n}f(d)\Rightarrow f(n)=\sum\limits_{ ...

  7. mac上安装memcache

    1. 安装 brew (http://brew.sh/) /usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/H ...

  8. 如何在jsp中引入bootstrap

    如何在jsp中引入bootstrap包: 1.首先在http://getbootstrap.com/上下载Bootstrap的最新版. 您会看到两个按钮: Download Bootstrap:下载 ...

  9. BZOJ 1370: [Baltic2003]Gang团伙(luogu 1892)(种类并查集)

    题面: bzoj题面有误,还是看luogu的吧 https://www.luogu.org/problemnew/show/P1892 题解: 种类并查集.. 因为有敌人的敌人是朋友这个条件,所以需要 ...

  10. SpringBoot整合阿里Druid数据源及Spring-Data-Jpa

    SpringBoot整合阿里Druid数据源及Spring-Data-Jpa https://mp.weixin.qq.com/s?__biz=MzU0MDEwMjgwNA==&mid=224 ...