There’s not standard way to check if a MySQL user exists and based on that drop it. Are there any workarounds for this?
Edit: I need a straight way to run this without throwing up an error
e.g.
DROP USER test@localhost; :
There’s not standard way to check if a MySQL user exists and based on that drop it. Are there any workarounds for this?
Edit: I need a straight way to run this without throwing up an error
e.g.
DROP USER test@localhost; :
If you mean you want to delete a drop from a table if it exists, you can use the DELETE
command, for example:
DELETE FROM users WHERE user_login = 'foobar'
If no rows match, it's not an error.
If you're specifically referring to a MySQL database user (and not a user in some kind of auth system you've implemented on your own), you'd want the following:
DELETE FROM user WHERE User = 'SOMEUSERNAME';
FLUSH PRIVILEGES();
This is a MySQL system table, so handle with care!
This worked for me:
GRANT USAGE ON *.* TO 'username'@'localhost';
DROP USER 'username'@'localhost';
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