Mysql删除用户,为多个主机授予权限

时间:2014-03-07 13:04:27

标签: mysql grant

我在mysql-db中添加了一个用户,可以从多个主机访问,例如:

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';
....

完全删除用户的最短方法是什么?我试过了:

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 '%'

我想,%wouzld被视为通配符。 Butr唯一的方法就是让每个主机都放弃用户,例如:

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

是否有更方便的方法来删除用户?

3 个答案:

答案 0 :(得分:1)

MySQL< = 5.6.x

select user,host,password from mysql.user;

MySQL> = 5.7.x

select user,host,authentication_string from mysql.user;

上面你需要创建一个回滚计划,相信如果你删除了很多用户并且你想节省时间,你总是需要一个。

现在问你的问题:

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

根据您的平台,探索如何将输出粘贴到文件中并执行它

答案 1 :(得分:0)

如果您使用的是MySQL> = 5.0.2,则可以删除帐户及其权限,如下所示:

DROP USER用户;

该语句从所有授权表中删除该帐户的特权行。

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

答案 2 :(得分:0)

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;