mysql刪除資料庫使用者
- IT科技
- 關注:1.04W次
mysql刪除使用者有兩種方法:
1、使用drop
drop user XXX;刪除已存在的使用者,預設刪除的是'XXX'@'%'這個使用者,如果還有其他的使用者如'XXX'@'localhost'等,不會一起被刪除。如果要刪除'XXX'@'localhost',使用drop刪除時需要加上host即drop user 'XXX'@'localhost'。
2、使用delete
delete from user where user='XXX' and host='localhost';其中XXX為使用者名稱,localhost為主機名。
3、drop和delete的區別:
drop不僅會將user表中的資料刪除,還會刪除其他許可權表的內容。而delete只刪除user表中的內容,所以使用delete刪除使用者後需要執行FLUSH PRIVILEGES;重新整理許可權,否則下次使用create語句建立使用者時會報錯。
拓展資料:
MySQL新增使用者、刪除使用者、授權及撤銷許可權
一、建立使用者:
mysql> insert into mysql.user(Host,User,Password) values("localhost","test",password("1234"));
#這樣就建立了一個名為:test 密碼為:1234 的使用者。
注意:此處的"localhost",是指該使用者只能在本地登入,不能在另外一臺機器上遠端登入。如果想遠端登入的話,將"localhost"改為"%",表示在任何一臺電腦上都可以登入。也可以指定某臺機器(例如192.168.1.10),或某個網段(例如192.168.1.%)可以遠端登入。
二、為使用者授權:
授權格式:grant 許可權 on 資料庫.* to 使用者名稱@登入主機 identified by "密碼";
首先為使用者建立一個數據庫(testDB):
mysql>create database testDB;
授權test使用者擁有testDB資料庫的所有許可權(某個資料庫的所有許可權):
mysql>grant all privileges on testDB.* to test@localhost identified by '1234';
mysql>flush privileges;//刷新系統許可權表,即時生效
如果想指定某庫的部分許可權給某使用者本地操作,可以這樣來寫:
mysql>grant select,update on testDB.* to test@localhost identified by '1234';
mysql>flush privileges;
#常用的許可權有select,insert,update,delete,alter,create,drop等。可以檢視mysql可授予使用者的執行許可權瞭解更多內容。
授權test使用者擁有所有資料庫的某些許可權的遠端操作:
mysql>grant select,delete,update,create,drop on *.* to test@"%" identified by "1234";
#test使用者對所有資料庫都有select,delete,update,create,drop 許可權。
檢視使用者所授予的許可權:
mysql> show grants for test@localhost;
三、刪除使用者:
mysql>Delete FROM user Where User='test' and Host='localhost';
mysql>flush privileges;
刪除賬戶及許可權:
>drop user 使用者名稱@'%';
>drop user 使用者名稱@ localhost;
四、修改指定使用者密碼:
mysql>update mysql.user set password=password('新密碼') where User="test" and Host="localhost";
mysql>flush privileges;
五、撤銷已經賦予使用者的許可權:
revoke 跟 grant 的語法差不多,只需要把關鍵字 “to” 換成 “from” 即可:
mysql>grant all on *.* to dba@localhost;
mysql>revoke all on *.* from dba@localhost;
六、MySQL grant、revoke 使用者許可權注意事項:
grant, revoke 使用者許可權後,該使用者只有重新連線 MySQL 資料庫,許可權才能生效。
如果想讓授權的使用者,也可以將這些許可權 grant 給其他使用者,需要選項 "grant option"
mysql>grant select on testdb.* to dba@localhost with grant option;
mysql>grant select on testdb.* to dba@localhost with grant option;
這個特性一般用不到。實際中,資料庫許可權最好由 DBA 來統一管理。
- 文章版權屬於文章作者所有,轉載請註明 https://shqsg.com/dianzi/r1pv0q.html