Mysql drop user with grants for multiple hosts

北战南征 提交于 2019-12-10 15:49:06

问题


I added a user to my mysql-db with grants to access from several hosts, like:

GRANT ALL PRIVILEGES ON `db`.* TO 'dbuser'@'host1';
GRANT ALL PRIVILEGES ON `db`.* TO 'dbuser'@'host2';
GRANT ALL PRIVILEGES ON `db`.* TO 'dbuser'@'host3';
GRANT ALL PRIVILEGES ON `db`.* TO 'dbuser'@'host4';
....

What is the shortest way to remove the user completly? I tried:

drop user 'dbuser'@'%';
ERROR 1396 (HY000): Operation DROP USER failed for 'dbuser'@'%'

drop user 'dbuser';
ERROR 1396 (HY000): Operation DROP USER failed for 'dbuser'@'%'

show grants for 'dbuser';
ERROR 1141 (42000): There is no such grant defined for user 'dbuser' on host '%'

I thought, % wouzld be treated as a wildcard. Butr the only way seems to drop the user for every host, like:

drop user 'dbuser'@'host1';
drop user 'dbuser'@'host2';
drop user 'dbuser'@'host3';
...

Isn't there a more convenient way to remove the user?


回答1:


MySQL <= 5.6.x

select user,host,password from mysql.user;

MySQL >=5.7.x

select user,host,authentication_string from mysql.user;

The above you will need to create a rollback plan and believe me you will always need one if deleting a lot of users and you want to save time.

to your question now:

select concat("DROP USER ","'",user,"'@'",host,"';") from mysql.user where host like "127.0.%" or host like "192.168%";

Depending on your platform , explore how to stick the output into a file and execute it




回答2:


SELECT GROUP_CONCAT('\'',user, '\'@\'', host, '\'') INTO @users FROM mysql.user WHERE user = 'root';
SET @users = CONCAT('DROP USER ', @users);
PREPARE stmt FROM @users;
EXECUTE stmt;



回答3:


If you're using MySQL >= 5.0.2, you can remove an account and its privileges as follows:

DROP USER user;

The statement removes privilege rows for the account from all grant tables.

(from: https://dev.mysql.com/doc/refman/5.0/en/drop-user.html)



来源:https://stackoverflow.com/questions/22250870/mysql-drop-user-with-grants-for-multiple-hosts

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!