MySQL用户权限管理

(互联网文档整理)

  MySQL的用户管理,指的是哪个用户可以连接服务器,从哪里连接,连接后能做什么.

  MySQL中grant语句创建mysql用户并指定其权限,而revoke语句删除权限。两条语句实现了mysql数据库的用户管理,并提供与直接操作这些表的内容不同的另一种方法。

  create和revoke语句影响4个表:授权表内容
    user 能连接服务器的用户以及他们拥有的任何全局权限
    db 数据库级权限
    tables_priv 表级权限
    columns_priv 列级权限

  还有第5个授权表(host),但它不受grant和revoke的影响。

  当你对一个用户发出一条grant语句时,在user表中为该用户创建一条记录。

  如果语句指定任何全局权限(管理权限或适用于所有数据库的权限),这些也记录在user表中。如果你指定数据库、表和列级权限,他们被分别记录在db、tables_priv和columns_priv表中。

  本篇将以实例的形式讲解创建删除MySQL用户权限...

1.创建用户并授权

  grant语句的语法:

    grant privileges (columns) on what to 'user'@'%' identified by 'passwordwith grant option 
  要使用该句型,需确定字段有:

  privileges 权限指定符权限允许的操作 
    alter 修改表和索引
    create 创建数据库和表
    delete 删除表中已有的记录
    drop 抛弃(删除)数据库和表
    index 创建或抛弃索引
    insert 向表中插入新行
    reference 未用
    select 检索表中的记录
    update 修改现存表记录
    file 读或写服务器上的文件
    process 查看服务器中执行的线程信息或杀死线程
    reload 重载授权表或清空日志、主机缓存或表缓存。
    shutdown 关闭服务器
    all 所有;all privileges同义词
    usage 特殊的“无权限”权限

  以上权限分三组:

     第一组:适用于数据库、表和列如:alter create delete drop index insert select update

     第二组:数管理权限 它们允许用户影响服务器的操作 需严格地授权 如:file process reload shut*

     第三组:权限特殊 all意味着“所有权限” uasge意味着无权限,即创建用户,但不授予权限

  columns

  权限运用的列(可选)并且你只能设置列特定的权限。如果命令有多于一个列,应该用逗号分开它们。

  what

  权限运用的级别。权限可以是全局,定数据库或特定表.

  user

  权限授予的用户,由一个用户名和主机名组成,许两个同名用户从不同地方连接.缺省:mysql用户 

  password

  赋予用户的口令(可选),如果你对用户没有指定identified by子句,该用户口令不变.

    用identified by时,口令字符串用改用口令的字面含义,grant将为你编码口令.

      注:set password使用password()函数
  with grant option

    用户可以授予权限通过grant语句授权给其它用户(可选)


  实例讲解:

    注:以下连接区域,用户名及密码应加'',如 'huaying'@'koowo.com'

    grant all on db_book.* to 'huaying'@'koowo.com' identified by 'yeelion' 只能在本地连接

    grant all on db_book.* to 'huaying'@'vpn.koowo.com' identified by 'yeeliong' 允许从此域连接 

    grant all on db_book.* to 'huaying'@'%' identified by 'yeelion' 允许从任何主机连接

      注:"%"字符起通配符作用,与like模式匹配的含义相同,yeelion为口令.

    grant all on db_book.* to 'huaying'@%.koowo.com identified by 'yeelion';

      允许huaying从koowo.com域的任何主机连接 

    grant all on db_book.* to 'huaying'@'192.168.1.189' identified by 'yeelion';

    grant all on db_book.* to 'huaying'@'192.168.1.%' identified by 'yeelion'

    grant all on db_book.* to 'huaying'@'192.168.1.0/17' identified by 'yeelion'

      允许从单IP 段IP或一子网IP登陆 

      注:有时 用户@IP 需用引号 如'huaying'@'192.168.1.0/17'

    grant all on *.* to 'huaying'@'localhost' identified by 'yeelion' with grant option

      添加超级用户huaying 可在本地登陆做任何操作.

    grant reload on *.* to 'huaying'@'localhost' identified by 'yeelion' 只赋予reload权限

    grant all on db_book to 'huaying'@'koowo.com' indetified by 'yeelion' 所有权限

    grant select on db_book to 'huaying'@'%' indetified by 'yeelion' 只读权限

    grant select,insert,delete,update on db_book to 'huaying'@'koowo.com' indetified by 'yeelion'

      只有select,insert,delete,update的权限

    grant select on db_book.storybook to 'huaying'@'localhost' indetified by 'yeelion' 只对表

    grant update (name) on db_book.storybook to 'huaying'@'localhost' 只对表的name列 密码不变

    grant update (id,name,author) on db_book.storybook to 'huaying'@'localhost'  只对表的多列

    grant all on book.* to ''@'koowo.com允许koowo.com域中的所有用户使用库book 

    grant all on book.* to 'huaying'@'%.koowo.com' indetified by 'yeelion' with grant option

      允许huaying对库book所有表的管理员授权.

    flush privileges; 重载授权表 操作为更新,使之生效。

 

2.查看撤权并删除用户权限。
  show grants; 查看当前用户(自己)权限
  show grants for db_user@localhost; 查看其他 MySQL 用户权限:

  revoke 跟 grant 的语法差不多,只需要把关键字 “to” 换成 “from” 即可:
    to用from取代,没有indetifed by和with grant option子句. 如下:

    revoke privileges (columns) on what from user

    user:必须匹配原来grant语句的你想撤权的用户的user部分。

    privileges:不需匹配,可以用grant语句授权,然后用revoke语句只撤销部分权限。

    revoke语句只删权限不删用户,撤销了所有权限后user表中用户记录保留,用户仍然可以连接服务器.

    要完全删除一个用户必须用一条delete语句明确从user表中删除用户记录:

      delete from user where user='huaying'

      flush privileges; 重载授权表

        注:使

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章