一、创建MySQL数据库

  1. mysql> CREATE DATABASE IF NOT EXISTS testdb DEFAULT CHARSET utf8 COLLATE utf8_general_ci;
  2. Query OK, 1 row affected

二、创建用户

2.1、新建用户

  1. mysql> create user 'test'@'%' identified by 'test';
  2. Query OK, 0 rows affected

2.2、查询用户

  1. mysql> select user,host from mysql.user;
  2. +---------------+-----------+
  3. | user | host |
  4. +---------------+-----------+
  5. | flash_sale | % |
  6. | root | % |
  7. | test | % |
  8. | thoughts-beta | % |
  9. | mysql.session | localhost |
  10. | mysql.sys | localhost |
  11. | root | localhost |
  12. +---------------+-----------+
  13. 7 rows in set

2.3、删除用户

  1. drop user test@'%';

2.4、更新密码

  1. mysql> set password for test = password('123456');
  2. Query OK, 0 rows affected

·

2.5、分配用户权限

  1. mysql> grant all privileges on testdb.* to 'test'@'%' identified by 'test';
  2. Query OK, 0 rows affected

2.6、查看用户权限

  1. mysql> show grants for test;
  2. +--------------------------------------------------+
  3. | Grants for test@% |
  4. +--------------------------------------------------+
  5. | GRANT USAGE ON *.* TO 'test'@'%' |
  6. | GRANT ALL PRIVILEGES ON `testdb`.* TO 'test'@'%' |
  7. +--------------------------------------------------+
  8. 2 rows in set

三、测试

image.png