mysql 的权限控制

一些简单的例子:

root@localhost : (none) 17:34:45> create user diamonduser identified by 'diamonduser';
Query OK, 0 rows affected (0.01 sec)

root@localhost : (none) 17:36:00> grant all on diamond.* to diamonduser@'localhost';
Query OK, 0 rows affected (0.01 sec)

root@localhost : (none) 17:37:04> grant all on diamond.* to diamonduser@'%';
Query OK, 0 rows affected (0.01 sec)

flush privileges


GRANT all privileges on *.* to 'notifyuser'@'localhost' identified by 'notifyuser';
GRANT all privileges on *.* to 'notifyuser'@'%' identified by 'notifyuser';
flush privileges;

 notifyuser:表示notifyuser用户

@'192.168.172.9': 表示开放的ip
IDENTIFIED BY 'notifyuser': 表示密码是notifyuser

MySQL服务器通过MySQL权限表来控制用户对数据库的访问,MySQL权限表存放在mysql数据库里,由mysql_install_db 脚本初始化。这些MySQL权限表分别user,db,table_priv,columns_priv和host。下面分别介绍一下这些表的结构和内 容:

  1. user权限表:记录允许连接到服务器的用户帐号信息,里面的权限是全局级的。
  2. db权限表:记录各个帐号在各个数据库上的操作权限。
  3. table_priv权限表:记录数据表级的操作权限。
  4. columns_priv权限表:记录数据列级的操作权限。
  5. host权限表:配合db权限表对给定主机上数据库级操作权限作更细致的控制。这个权限表不受GRANT和REVOKE语句的影响。

查询user表的信息

mysql> select User,Host from user;
| User | Host        |
| root  | 127.0.0.1 |
| root  | ::1 |
|         | localhost |
| root   | localhost |
|         |  yunpeng-duitang |
| root   |  yunpeng-duitang |

授予MySQL用户权限的实例:
mysql>grant select,insert,update,delete on test.user to mql@localhost identified by ‘123456′;
给 本地的用户mql分配可对数据库test的user表进行select,insert,update,delete操作的权限,并设定口令为 123456。若mql用户不存在,则将自动创建此用户. 具体的权限控制在mysql.db表中可以查看到.也可直接对这个表进行更新操作进行权限的修改.

mysql>grant all privileges on test.* to mql@localhost identified by ‘123456′;
给本地用户mql分配可对数据库test所有表进行所有操作的权限,并设定口令为123456。

mysql>grant all privileges on *.* to mql@localhost identified by ‘123456′;
给本地用户mql分配可对所有数据库的所有表进行所有操作的权限,并设定口令为123456。

mysql>grant all privileges on *.* to [email protected] identified by ‘123456′;
给来自61.127.46.128的用户mql2分配可对所有数据库的所有表进行所有操作的权限,并设定口令为123456。

REVOKE
REVOKE和作用和GRANT相反,语法格式为:
REVOKE privileges ON 数据库名[.表名] FROM user_name

例如:
创建用户Bob,密码为“bob”,但不给他任何权限:
GRANT usage on * to Bob identified by ’bob’;
授予Bob在books数据库中的查询和插入权限:
GRANT select, insert on books.* to   Bob;
取消Bob在books数据库中的所有权限:
REVOKE all on books.* from Bob;
注:需要指出的是,REVOKE all...仅仅是回收用户的权限,并不删除用户。在MySQL中,用户信息存放在mysql.User中。MySQL可以通过DROP USER来彻底删除一个用户,其用法为:
DROP USER user_name;
例如,要删除用户Bob,可以用:
DROP USER Bob;

猜你喜欢

转载自san-yun.iteye.com/blog/1695667