MySQL中创建用户与授权

参考地址:http://blog.csdn.net/gebitan505/article/details/51726649

一.创建用户(使用root用户登录进入mysql命令行)

create user 'username'@'host' identified by 'password'

说明:

其中username为创建的用户名;

host为主机名,指定该用户在哪个主机上可以登陆,如果是本地用户可用localhost,如果想让该用户可以从任意远程主机登陆,可以使用通配符%;

password为用户密码,密码可以为空,如果为空则该用户可以不需要密码登陆服务器

例子:

1.create user 'xiong'@'localhost' identified by '123456' (xiong可以访问本地服务器)

2.create user 'ying'@'%' identified by '123456' (ying可以访问)

3.create user 'cai'@'%' identified by ''

4.create user 'he'@'%'

二.授予权限(使用root用户登录下)

grant privileges on databasename.tablename to 'username'@'host'

说明:

1.privileges:用户的操作权限,如SELECTINSERTUPDATE等,如果要授予所的权限则使用ALL

2.databasename:数据库名

3.tablename:表名,如果要授予该用户对所有数据库和表的相应操作权限则可用*表示,如*.*

例子:

grant select,insert on test.user to 'xiong'@'localhost;(xiogn用户被授予test数据下user表的select和insert权限)

grant all privileges on *.* to 'ying'@'%' (ying用户被授予所有的数据库和数据表)

注意:

用以上命令授权的用户不能给其它用户授权,如果想让该用户可以授权,用以下命令:

grant privileges on databasename.tablename to 'username'@'host' with grant option;

三.修改密码

set password for 'username'@'host' = password('newpassword');

如果是更改当前登录用户的密码:

set password = password('newpassword');

四.撤销用户权限

revoke privilege on databasename.tablename from 'username'@'host';

说明:

privilege, databasename, tablename:同授权部分

例子:

revoke select on *.* from 'ying'@'%';

revoke all privileges on *.* from 'ying'@'localhost'

注意:

假如你在给用户'ying'@'%'授权的时候是这样的(或类似的):grant select on test.user to 'ying'@'%',则在使用revoke select on *.* from 'ying'@'%';命令并不能撤销该用户对test数据库中user表的select 操作。相反,如果授权使用的是grant select on *.* to 'ying'@'%';则revoke select on test.user from 'ying'@'%';命令也不能撤销该用户对test数据库中user表的select权限。

具体信息可以用命令show grants for 'ying'@'%'; 查看

五.删除用户

drop user 'username'@'host';

原文地址:https://www.cnblogs.com/fireporsche/p/8567758.html