MySQL:检查用户是否存在并删除它 [英] MySQL: Check if the user exists and drop it
问题描述
没有标准的方法来检查一个MySQL用户是否存在并基于它。有什么解决方法吗?
There’s not standard way to check if a MySQL user exists and based on that drop it. Are there any workarounds for this?
编辑:我需要一个直接的方式来运行它而不引发错误
例如
I need a straight way to run this without throwing up an error
e.g.
DROP USER test@localhost; :
推荐答案
这适用于我:
GRANT USAGE ON *.* TO 'username'@'localhost';
DROP USER 'username'@'localhost';
这会创建用户,如果它不存在(并授予它无害的权限)无论哪种方式。找到解决方案: http://bugs.mysql.com/bug.php?id=19166
This creates the user if it doesn't already exist (and grants it a harmless privilege), then deletes it either way. Found solution here: http://bugs.mysql.com/bug.php?id=19166
更新: @Hao建议添加 IDENTIFIED BY
; @andreb(在评论中)建议禁用 NO_AUTO_CREATE_USER
。
Updates: @Hao recommends adding IDENTIFIED BY
; @andreb (in comments) suggests disabling NO_AUTO_CREATE_USER
.
这篇关于MySQL:检查用户是否存在并删除它的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!