開心生活站

位置:首頁 > IT科技 > 

mysql刪除用戶命令

IT科技2.29W

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添加用戶、刪除用戶、授權及撤銷權限

一、創建用戶:

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刪除用戶命令 第2張

三、刪除用戶:

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

標籤:命令 mysql