zoukankan      html  css  js  c++  java
  • MySQL添加用户、删除用户、授权及撤销权限

    一.创建用户:

    mysql> insert into mysql.user(Host,User,Password) values("localhost","test",password("1234"));

    #这样就创建了一个名为:test 密码为:1234 的用户。

    注意:此处的"localhost",是指该用户只能在本地登录,不能在另外一台机器上远程登录。如果想远程登录的话,将"localhost"改为"%",表示在任何一台电脑上都可以登录。也可以指定某台机器(例如192.168.1.10),或某个网段(例如192.168.1.%)可以远程登录。

    二.为用户授权:

    授权格式:grant 权限 on 数据库.* to 用户名@登录主机 identified by "密码"; 

    2.1 首先为用户创建一个数据库(testDB):

    mysql>create database testDB;

    2.2 授权test用户拥有testDB数据库的所有权限(某个数据库的所有权限):

     mysql>grant all privileges on testDB.* to test@localhost identified by '1234';

    mysql>flush privileges;//刷新系统权限表,即时生效

    2.3 如果想指定某库的部分权限给某用户本地操作,可以这样来写:

    mysql>grant select,update on testDB.* to test@localhost identified by '1234';

    mysql>flush privileges; 

    #常用的权限有select,insert,update,delete,alter,create,drop等。可以查看mysql可授予用户的执行权限了解更多内容。

    2.4  授权test用户拥有所有数据库的某些权限的远程操作:   

    mysql>grant select,delete,update,create,drop on *.* to test@"%" identified by "1234";

     #test用户对所有数据库都有select,delete,update,create,drop 权限。

    2.5 查看用户所授予的权限:

    mysql> show grants for test@localhost;

    三、删除用户:

    mysql>Delete FROM user Where User='test' and Host='localhost';

    mysql>flush privileges;

    删除账户及权限:>drop user 用户名@'%';

            >drop user 用户名@ localhost; 

    四、修改指定用户密码:

    mysql>update mysql.user set password=password('新密码') where User="test" and Host="localhost";

    mysql>flush privileges;

    五、撤销已经赋予用户的权限:

    revoke 跟 grant 的语法差不多,只需要把关键字 “to” 换成 “from” 即可:

    mysql>grant all on *.* to dba@localhost;

    mysql>revoke all on *.* from dba@localhost;

    六、MySQL grant、revoke 用户权限注意事项:

    6.1 grant, revoke 用户权限后,该用户只有重新连接 MySQL 数据库,权限才能生效。

    6.2. 如果想让授权的用户,也可以将这些权限 grant 给其他用户,需要选项 "grant option"

    mysql>grant select on testdb.* to dba@localhost with grant option;

    mysql>grant select on testdb.* to dba@localhost with grant option;

    这个特性一般用不到。实际中,数据库权限最好由 DBA 来统一管理。

    补充:

    mysql授权表共有5个表:user、db、host、tables_priv和columns_priv。

    授权表的内容有如下用途:
    user表
    user表列出可以连接服务器的用户及其口令,并且它指定他们有哪种全局(超级用户)权限。在user表启用的任何权限均是全局权限,并适用于所有数据库。例如,如果你启用了DELETE权限,在这里列出的用户可以从任何表中删除记录,所以在你这样做之前要认真考虑。

    db表
    db表列出数据库,而用户有权限访问它们。在这里指定的权限适用于一个数据库中的所有表。

    host表
    host表与db表结合使用在一个较好层次上控制特定主机对数据库的访问权限,这可能比单独使用db好些。这个表不受GRANT和REVOKE语句的影响,所以,你可能发觉你根本不是用它。

    tables_priv表
    tables_priv表指定表级权限,在这里指定的一个权限适用于一个表的所有列。

    columns_priv表
    columns_priv表指定列级权限。这里指定的权限适用于一个表的特定列

  • 相关阅读:
    HDU 3951 (博弈) Coin Game
    HDU 3863 (博弈) No Gambling
    HDU 3544 (不平等博弈) Alice's Game
    POJ 3225 (线段树 区间更新) Help with Intervals
    POJ 2528 (线段树 离散化) Mayor's posters
    POJ 3468 (线段树 区间增减) A Simple Problem with Integers
    HDU 1698 (线段树 区间更新) Just a Hook
    POJ (线段树) Who Gets the Most Candies?
    POJ 2828 (线段树 单点更新) Buy Tickets
    HDU 2795 (线段树 单点更新) Billboard
  • 原文地址:https://www.cnblogs.com/zst062102/p/11132916.html
Copyright © 2011-2022 走看看