一、建立用戶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';