創(chuàng)建用戶
insert into mysql.user(Host,User,Password) values("localhost","test",password("123456"));
create user 'test'@'localhost' identified by '123456';
grant all privileges on db.* to 'test'@'localhost' identified by '123456';
刪除用戶
Delete FROM user Where User='test' and Host='localhost';
刪除用戶并取消權(quán)限
drop user 'test'@'localhost';
將刪除user表的用戶數(shù)據(jù)
將刪除db表和table_priv表的權(quán)限數(shù)據(jù)
將權(quán)限授予一個(gè)已存在的用戶
grant all privileges on db.* to 'test'@'localhost'
將權(quán)限授予用戶
如果用戶存在則更新密碼,如果用戶不存在則創(chuàng)建用戶
grant all privileges on db.* to 'test'@'localhost' identified by '123456';
grant授權(quán)如果指定了數(shù)據(jù)庫(kù)將在db表產(chǎn)生一條數(shù)據(jù)
grant授權(quán)如果指定了數(shù)據(jù)表將在table_priv產(chǎn)生一條數(shù)據(jù)
取消權(quán)限
revoke all privileges on *.* from 'test'@'%';
on 后面的作用范圍必須小于或等于當(dāng)前用戶權(quán)限的作用范圍
例如:
grant all privileges on dbname.* to 'test'@'localhost'
revoke select on *.* from 'test'@'%';//無(wú)效
revoke select on dbname.* from 'test'@'%';//有效
revoke select on dbname.tablename from 'test'@'%';//有效
revoke取消權(quán)限即使將全部權(quán)限取消也不會(huì)刪除db表和table_priv表的權(quán)限數(shù)據(jù)
修改密碼
update mysql.user set Password=password('123456') where User="test" and Host="localhost";
SET PASSWORD FOR 'test'@'localhost' = PASSWORD('123456');
查看 MySQL 用戶權(quán)限
- 查看當(dāng)前用戶(自己)權(quán)限:
show grants;
- 查看其他 MySQL 用戶權(quán)限:
show grants for 'test'@'localhost';
特別注意
- 如果需要一個(gè)空密碼或者無(wú)密碼的賬戶,必須先用Create User命令,然后通過(guò)grant來(lái)分配權(quán)限,grant只能創(chuàng)有密碼的賬戶。
- 如果想讓授權(quán)的用戶,也可以將這些權(quán)限 grant 給其他用戶,需要選項(xiàng) "with grant option"
- grant, revoke修改完權(quán)限以后一定要刷新服務(wù),或者重啟服務(wù),刷新服務(wù)用:flush privileges;
host說(shuō)明
127.0.0.1 - 表示本機(jī)
localhost - 表示本機(jī)*
::1 - 表示ipv6地址的127.0.0.1
% - 表示任何地址