MySQL-权限管理

1、权限管理

  * mysql 库的 user 表控制mysql的权限。

  如:

  mysql> select user,host from user;
 
  | user                       | host |
    | debian-sys-maint    | localhost |
  | mysql.sys                | localhost |
  | phpmyadmin           | localhost |
  | root                       | localhost |
 

     数据说明,root这个用户只能从localhost这个主机登陆到数据库。

2、 添加一个mysql用户账号,并授权

  使用grant命令可以添加mysql用户

  grant例子:

  grant all privileges on *.* to test@'localhost' identified by '123' with grant option;

  grant命令说明:

  all privileges 表示所有权限,也可以使用select,update等

  on 是固定语法

  *.* 第一个*代表所有的数据库,第二个*代表所有表。也可以写demo.user代表demo库的user表

  to 为固定语法


  test@'localhost' 代表: 用户名@ip, ip可以是具体的ip地址,也可以是%(表示任何地址)

  identified by '密码', identified by 也可以不写,不写就是不修改密码

  with grant option 代表该用户可以将自己所拥有的权限给到别的用户,这个选项一般建议不加


  * grant 实例

    1. 授予jack在tp库的tp_user表上有select和update权限

     grant select,update on tp.tp_user to jack@'localhost' identified by '123';

    2. 在第一题的基础上,再给jack用户对tp库的tp_user表有delete权限

     这个时候,jack就有select,update,delete权限了
     grant delete on tp.tp_user to jack@'localhost';

    3. 授予test1在tp库的tp_user表上的姓名和邮箱字段的select权限

    grant select(name,email) on tp.tp_user to 'test1'@'localhost' identified by '123';

    这个时候,test1只能查询出name与email的数据


3、 修改用户密码

  set password for '用户名'@'ip'=PASSWORD('123456');


4、 删除用户

   drop user '用户名'@'ip'

   例如: drop user test@localhost;

5、 查看用户的权限

  grants for '用户名'@'ip'

原文地址:https://www.cnblogs.com/onlyzc/p/8417038.html