Last Updated: February 25, 2016
· tralamazza

MySQL would you please DROP USER IF EXISTS!?

MySQL can drop almost anything conditionally (e.g. databases, tables, procedures ...), but a user.

So if you have a DB bootstrap script that removes pesky default anonymous users, you probably want to do something like:

DROP USER ''@'localhost';

Now run it again on the same DB and you probably know what happens.

To my delight I found this little workaround gem by Steve Bearman from a April 2006 MySQL thread still working like a charm (tested on MySql 5.5). So now instead you do:

GRANT USAGE ON *.* TO ''@'localhost';
DROP USER ''@'localhost';

And voilá.

Say Thanks

1 Response
Add your response


Thanks! It actually works with any user. Very handy for automatic provisioning!

over 1 year ago ·