MySQL用户权限

一、用户授权

mysql> grant all on *.* to 'test'@'%' identified by '123456';            
Query OK, 0 rows affected, 1 warning (0.01 sec)

mysql> grant all privileges on *.* to 'test'@'%' identified by '123456' with grant option;  
Query OK, 0 rows affected, 1 warning (0.01 sec)
  • all/all privileges:表示将所有权限授予给用户。也可指定具体的权限,如:select、creat、drop等。至于all与all privileges有什么区别,目前楼主还没有发现,待补
  • on:表示这些权限对哪些数据库和表生效,格式:数据库名.表名,这里写“*”表示所有数据库,所有表。如果要指定将权限应用到test库的user表中,可以这么写:test.user,如果要指定将权限应用到test库的所有表中,可以这么写:test.*
  • to:将权限授予哪个用户。格式:”用户名”@”登录IP或域名”。%表示没有限制,在任何主机都可以登录。比如:”tang”@”192.168.0.%”,表示tang这个用户只能在192.168.0IP段登录
  • identified by:指定用户的登录密码
  • with grant option:表示允许用户将自己的权限授权给其它用户(grant权限)。
    这里不做过多解释,具体详见:
    http://blog.csdn.net/dongdong9223/article/details/47445625
    https://www.cnblogs.com/aguncn/p/4313724.html

可以使用grant给用户添加权限,权限会自动叠加,不会覆盖之前授予的权限,比如你先给用户添加一个select权限,后来又给用户添加了一个insert权限,那么该用户就同时拥有了select和insert权限。

用户详情的权限列表请参考MySQL官网说明:http://dev.mysql.com/doc/refman/5.7/en/privileges-provided.html

二、刷新权限

对用户做了权限变更之后,一定记得重新加载一下权限,将权限信息从内存中写入数据库。尤其是你对那些权限表user、db、host等做了update或者delete更新的时候。以前遇到过使用grant后权限没有更新的情况,只要对权限做了更改就使用FLUSH PRIVILEGES命令来刷新权限。

mysql> flush privileges;
Query OK, 0 rows affected (0.03 sec)

三、查看用户权限

查看当前用户的权限:

mysql> show grants;
+---------------------------------------------------------------------+
| Grants for root@localhost                                           |
+---------------------------------------------------------------------+
| GRANT ALL PRIVILEGES ON *.* TO 'root'@'localhost' WITH GRANT OPTION |
| GRANT PROXY ON ''@'' TO 'root'@'localhost' WITH GRANT OPTION        |
+---------------------------------------------------------------------+
2 rows in set (0.00 sec)

查看某个用户的权限:

mysql> select host,user from user;
+--------------+---------------+
| host         | user          |
+--------------+---------------+
| localhost    | tang          |
+--------------+---------------+

mysql> show grants for wps;
+----------------------------------------------------+
| Grants for wps@%                                   |
+----------------------------------------------------+
| GRANT USAGE ON *.* TO 'wps'@'%'                    |
| GRANT ALL PRIVILEGES ON `wordpress`.* TO 'wps'@'%' |
+----------------------------------------------------+
2 rows in set (0.00 sec)

mysql> show grants for tang;
ERROR 1141 (42000): There is no such grant defined for user 'tang' on host '%'
mysql> show grants for 'tang'@'localhost';
+----------------------------------------------------------+
| Grants for tang@localhost                                |
+----------------------------------------------------------+
| GRANT USAGE ON *.* TO 'tang'@'localhost'                 |
| GRANT ALL PRIVILEGES ON `discuz`.* TO 'tang'@'localhost' |
+----------------------------------------------------------+
2 rows in set (0.01 sec)

四、回收权限

删除test这个用户的create权限,该用户将不能创建数据库和表。

mysql> revoke create on *.* from 'test'@'localhost';              
Query OK, 0 rows affected (0.03 sec)
mysql> flush privileges;
Query OK, 0 rows affected (0.02 sec)

mysql> create database test02;
ERROR 1044 (42000): Access denied for user 'test'@'localhost' to database 'test02'

五、删除用户

mysql> select host,user from user;
+-----------+---------------+
| host      | user          |
+-----------+---------------+
| %         | root          |
| localhost | test          |
+-----------+---------------+
2 rows in set (0.00 sec)

#删除用户
mysql> drop user 'test'@'localhost';
Query OK, 0 rows affected (0.05 sec)

mysql> select host,user from user;
+-----------+---------------+
| host      | user          |
+-----------+---------------+
| %         | root          |
+-----------+---------------+
1 rows in set (0.00 sec)

六、用户重命名

mysql> rename user 'test'@'%' to 'test_1'@'%';
Query OK, 0 rows affected (0.01 sec)

七、修改密码

1、用update直接编辑user表
mysql> use mysql;
# mysql5.7之前
mysql> update user set password=password('123456') where user='test';
# mysql5.7之后
mysql> update user set authentication_string=password('123654') where user='test'; 
mysql> flush privileges;
2、用set password命令

语法:set password for ‘用户名’@’登录地址’=password(‘密码’)

mysql> set password for 'test'@'%'=password('123456'); 
3、用mysqladmin

语法:mysqladmin -u用户名 -p旧的密码 password 新密码

[root@VM_36_centos ~]# mysqladmin -utest -p123456 password 123654

注意:mysqladmin位于mysql安装目录的bin目录下



原文链接:
http://blog.csdn.net/xyang81/article/details/51822252
参考文档:
http://blog.csdn.net/mchdba/article/details/45934981
https://www.cnblogs.com/wangchaoyuana/p/7545419.html
http://www.cnblogs.com/kissdodog/p/4173337.html
http://www.cnblogs.com/4php/p/4113593.html

你可能感兴趣的:(MySQL用户权限)