一、建立用户mysql
create user 'username'@'host' identified by 'password';sql
参数说明:数据库
username:用户名。服务器
host:能够链接的的主机。'localhost'表示只能本机链接,'%'表示任何一台机器均可以链接,也能够经过ip地址,规定某台远程主机能够链接。ide
password:链接密码。''表示不须要密码。spa
例:create user 'jerry'@'localhost' identified by '123456';//只有本地能够链接ip
create user 'jerry' identified by '';//任何主机均可以链接,并且不须要密码table
二、受权class
grant privileges on databasename.tablename to 'username'@'host';服务器端
参数说明:
privileges:用户的操做权限 如select update delete insert。若是想拥有全部权限,可使用all,默认除了grant权限,其余权限都拥有。
databasename:受权的数据库名,表示能够对哪一个数据库进行操做。 若是想对全部数据库能够操做,使用*
tablename:受权的表名,表示能够对哪一个表进行操做。若是想对全部表能够操做,使用*
例:grand select,insert on *.* 'jerry'@'localhost';
grand all on db1.tb1 to 'jerry'@'localhost';
grand all on *.* to 'jerry'@'localhost';
mysql的权限说的是服务器端的权限。
三、建立用户并受权
grant privileges on databasename.tablename to 'username'@'host' identified by 'password';
例:grant all on *.* to 'hello'@'localhost' identified by '123456';
使用这种方式建立用户,若是用户存在了,权限不会有变化,但能够修改密码。若是用户不存在,则建立用户并受权,当即生效。
四、使用户生效
使用先建立用户,再受权的方式建立用户,须要执行下面的命令,使用户权限生效。
flush privileges;
必须执行flush privileges;不然ERROR 1045 (28000): Access denied for user 'user'@'localhost' (using password: YES )
五、设置与修改密码
SET PASSWORD FOR 'username'@'host' = PASSWORD('newpassword');
六、撤销用户权限
REVOKE privilege ON databasename.tablename FROM 'username'@'host';
具体可看某个用户的权限。
SHOW GRANTS FOR 'username'@'host';