不同角色,分配不同权限,保证数据安全。

用户管理

在MySQL的默认数据库 mysql 中的 user 表中存储着所有的账户信息(含账户、权限等)。

  1. mysql> show databases;
  2. +--------------------+
  3. | Database |
  4. +--------------------+
  5. | information_schema |
  6. | db1 |
  7. | mysql |
  8. | performance_schema |
  9. | sys |
  10. +--------------------+
  11. 10 rows in set (0.00 sec)
  12. mysql> select user,authentication_string,host from mysql.user;
  13. +----------------------------------+-------------------------------------------+-------------------------------+
  14. | user | authentication_string | host |
  15. +----------------------------------+-------------------------------------------+-------------------------------+
  16. | root | *FAAFFE644E901CFAFAEC7562415E5FAEC243B8B2 | localhost |
  17. | mysql.session | *THISISNOTAVALIDPASSWORDTHATCANBEUSEDHERE | localhost |
  18. | mysql.sys | *THISISNOTAVALIDPASSWORDTHATCANBEUSEDHERE | localhost |
  19. +----------------------------------+-------------------------------------------+-------------------------------+
  20. 3 rows in set (0.00 sec)
  • 创建和删除用户
    1. create user '用户名'@'连接者的IP地址' identified by '密码';
    ```sql create user mufeng@127.0.0.1 identified by ‘root123’; drop user mufeng@127.0.0.1;

create user mufeng@’127.0.0.%’ identified by ‘root123’; drop user mufeng@’127.0.0.%’;

create user ‘mufeng2’@’%’ identified by ‘root123’; drop user ‘mufeng2’@’%’;

  1. - 修改用户

rename user ‘用户名’@’IP地址’ to ‘新用户名’@’IP地址’;

  1. ```sql
  2. rename user mufeng@127.0.0.1 to mufeng@localhost;
  3. rename user 'mufeng'@'127.0.0.1' to 'mufeng'@'localhost';
  • 修改密码

    1. set password for '用户名'@'IP地址' = Password('新密码')
    1. set password for 'mufeng2'@'%' = Password('123123');

    授权管理

    创建好用户之后,就可以为用户进行授权了。

  • 授权

    1. grant 权限 on 数据库.表 to '用户'@'IP地址'

    ```sql grant all privileges on . to ‘mufeng’@’localhost’; — 用户mufeng拥有所有数据库的所有权限 grant all privileges on db1.* to ‘mufeng’@’localhost’; — 用户mufeng拥有数据库db1的所有权限 grant all privileges on db1.info to ‘mufeng’@’localhost’; — 用户mufeng拥有数据库db1中info表的所有权限

grant select on day26.info to ‘mufeng’@’localhost’; — 用户mufeng拥有数据库db1中info表的查询权限 grant select,insert on db1. to ‘mufeng’@’localhost’; — 用户mufeng拥有数据库db1所有表的查询和插入权限 grant all privileges on db1. to ‘mufeng2’@’%’;

注意:flush privileges; — 将数据读取到内存中,从而立即生效。

  1. - 对于权限

all privileges 除grant外的所有权限 select 仅查权限 select,insert 查和插入权限 … usage 无访问权限 alter 使用alter table alter routine 使用alter procedure和drop procedure create 使用create table create routine 使用create procedure create temporary tables 使用create temporary tables create user 使用create user、drop user、rename user和revoke all privileges create view 使用create view delete 使用delete drop 使用drop table execute 使用call和存储过程 file 使用select into outfile 和 load data infile grant option 使用grant 和 revoke index 使用index insert 使用insert lock tables 使用lock table process 使用show full processlist select 使用select show databases 使用show databases show view 使用show view update 使用update reload 使用flush shutdown 使用mysqladmin shutdown(关闭MySQL) super 􏱂􏰈使用change master、kill、logs、purge、master和set global。还允许mysqladmin􏵗􏵘􏲊􏲋调试登陆 replication client 服务器位置的访问 replication slave 由复制从属使用

  1. - 对于数据库和表

数据库名. 数据库中的所有 数据库名.表名 指定数据库中的某张表 数据库名.存储过程名 指定数据库中的存储过程 .* 所有数据库

  1. - 查看授权

show grants for ‘用户’@’IP地址’

  1. ```sql
  2. show grants for 'mufeng'@'localhost';
  3. show grants for 'mufeng2'@'%';
  • 取消授权
    1. revoke 权限 on 数据库.表 from '用户'@'IP地址'
    ```sql revoke ALL PRIVILEGES on day26.* from ‘mufeng’@’localhost’;

revoke ALL PRIVILEGES on day26db.* from ‘mufeng2’@’%’; 注意:flush privileges; — 将数据读取到内存中,从而立即生效。 ```

一般情况下,在很多的 正规 公司,数据库都是由 DBA 来统一进行管理,DBA为每个项目的数据库创建用户,并赋予相关的权限。