tags:

views:

3062

answers:

4

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; :
+1  A: 

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.

Jason Cohen
I think the OP refers to a real database user.
Tomalak
Not a database table.
Cherian
+4  A: 

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!

AvatarKava
It's the "mysql" database. From scratch on a console login, you'd> CONNECT mysql;Then execute what I put higher up.
AvatarKava
You should mention why "DROP USER" will not work. I found this nice bit of info: http://www.mail-archive.com/[email protected]/msg139709.html
Tomalak
This will not work from 5.0 onwards
Cherian
+1  A: 
Cherian
Works well, thank you :)
Sophia
A: 

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

phyzome