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 來統一管理。