用户授权管理

1.用户管理

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

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| day26              |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
10 rows in set (0.00 sec)

mysql> select user,authentication_string,host from  mysql.user;
+----------------------------------+-------------------------------------------+-------------------------------+
| user                             | authentication_string                     | host                          |
+----------------------------------+-------------------------------------------+-------------------------------+
| root                             | *FAAFFE644E901CFAFAEC7562415E5FAEC243B8B2 | localhost                     |
| mysql.session                    | *THISISNOTAVALIDPASSWORDTHATCANBEUSEDHERE | localhost                     |
| mysql.sys                        | *THISISNOTAVALIDPASSWORDTHATCANBEUSEDHERE | localhost                     |
+----------------------------------+-------------------------------------------+-------------------------------+
3 rows in set (0.00 sec)
  • 创建和删除用户
    create user '用户名'@'连接者的IP地址' identified by '密码';
    --创建一个用户名为“wupeiqil”的账户,链接地址为“127.0.0.1” 密码为“root123”
    create user 'wupeiqi1'@'127.0.0.1' identified by 'root123'; --表示只能在这个ip地址下用这个账号和密码进行登陆该数据库
    drop user wupeiqi1@127.0.0.1;	--删除账户
    
    --“%”代表任意
    create user 'wupeiqi2'@'127.0.0.%' identified by 'root123';
    drop user 'wupeiqi2'@'127.0.0.%';	--删除账户
    
    -- ip地址直接写“%”,表示在任何ip地址下都可以用这个账户和米面登陆该数据库
    create user 'wupeiqi3'@'%' identified by 'root123';
    drop user 'wupeiqi3'@'%';	  --删除帐户
  •  修改用户
    rename user 用户名'@'IP地址' to '新用户名'@'IP地址';
    rename user 'wupeiqi1'@'127.0.0.1' to 'wupeiqi1'@'localhost';
  • 修改密码
    set password for '用户名'@'IP地址' = Password('新密码')
    set password for 'wupeiqi4'@'%' = Password('123123');

2.授权管理

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

  • 授权
    grant 权限 on 数据库.表 to   '用户'@'IP地址'
    grant all privileges on *.* TO 'wupeiqi'@'localhost';         -- 用户wupeiqi拥有所有数据库的所有权限
    grant all privileges on day26.* TO 'wupeiqi'@'localhost';     -- 用户wupeiqi拥有数据库day26的所有权限
    grant all privileges on day26.info TO 'wupeiqi'@'localhost';  -- 用户wupeiqi拥有数据库day26中info表的所有权限
    
    grant select on day26.info TO 'wupeiqi'@'localhost';          -- 用户wupeiqi拥有数据库day26中info表的查询权限
    grant select,insert on day26.* TO 'wupeiqi'@'localhost';      -- 用户wupeiqi拥有数据库day26所有表的查询和插入权限
    
    
    注意:flush privileges;   -- 在授权完成后,执行该语句,将数据读取到内存中,从而立即生效。

     

    · 对于权限
    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       由复制从属使用
    · 对于数据库和表
    数据库名.*            数据库中的所有
    数据库名.表名          指定数据库中的某张表
    数据库名.存储过程名     指定数据库中的存储过程
    *.*                  所有数据库
  • 查看授权
    show grants for '用户'@'IP地址'
    show grants for 'wupeiqi'@'localhost';
    show grants for 'wupeiqi4'@'%';
  • 取消授权
    revoke 权限 on 数据库.表 from '用户'@'IP地址'
    revoke ALL PRIVILEGES on day26.* from 'wupeiqi'@'localhost';
    
    revoke ALL PRIVILEGES on day26db.* from 'wupeiqi4'@'%';
    
    注意:flush privileges;   -- 将数据读取到内存中,从而立即生效。
posted @ 2021-11-21 22:49  A熙  阅读(39)  评论(0编辑  收藏  举报