MySQL - 用戶及權(quán)限

創(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';

特別注意

  1. 如果需要一個(gè)空密碼或者無(wú)密碼的賬戶,必須先用Create User命令,然后通過(guò)grant來(lái)分配權(quán)限,grant只能創(chuàng)有密碼的賬戶。
  2. 如果想讓授權(quán)的用戶,也可以將這些權(quán)限 grant 給其他用戶,需要選項(xiàng) "with grant option"
  3. grant, revoke修改完權(quán)限以后一定要刷新服務(wù),或者重啟服務(wù),刷新服務(wù)用:flush privileges;

host說(shuō)明

127.0.0.1 - 表示本機(jī)
localhost - 表示本機(jī)*
::1 - 表示ipv6地址的127.0.0.1
% - 表示任何地址

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

推薦閱讀更多精彩內(nèi)容