2016年11月7日 星期一

‍資料庫的使用者與權限

檢視使用者
select user,host from mysql.user;

新增使用者
create user 'username'@'host' identified by 'password';
#host can be: localhost, 192.168.1.%, %

修改使用者密碼:
set password for 'user'@'host' = password('password');

 刪除使用者
1. drop user 'username'@'host';
2. delete from mysql.user where user='user' and host='host';

檢視grant權限
show grants for 'username'@'host';

grant權限
概念:
1. 使用指令的權限,使用者對所有資料庫使用指令的權限。
grant select on *.* to 'user'@'host';
資料指令權限有select,insert,update,delete,file
結構指令權限create,alter,index,drop,create temporary tables,show view,create routine,alter routine,excute,create view,event,trigger
系統管理指令權限 grant,super,process,reload,shut down,show database,lock tables,create user
2. 對資料庫的權限,可以專為單一資料庫開放所有權限,但其他資料庫只有select的權限。
ex, grant privileges on *.* to 'username'@'host';
(privileges: all privileges, select,insert,update.........)
(*.* : database.*)
ex, grant all privileges on database.* to 'user'@'host';

給與grant權限 
grant privileges on *.* to 'username'@'host' with grant option; 

取消權限
revoke all privileges on *.* from 'username'@'host';
revoke grant option on *.* from 'username'@'host';