2

I have a brand new mysql installation. I want to delete a user only if exists and I have only root user. I'm trying to run from shell:

DROP USER IF EXISTS foo;

or

DROP USER IF EXISTS 'foo'@'localhost';

But mysql returns this kind of messages:

ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'IF EXISTS foo' at line 1

What am I doing wrong?

I read this post but does not explain why IF EXISTS statement does not work

Community
  • 1
  • 1
floatingpurr
  • 7,749
  • 9
  • 46
  • 106
  • 1
    Possible duplicate of [MySQL: Check if the user exists and drop it](http://stackoverflow.com/questions/598190/mysql-check-if-the-user-exists-and-drop-it) – robsn Mar 30 '16 at 13:52
  • 3
    What's the version of mysql you are running? The if exists option is available from v5.7.8 only. – Shadow Mar 30 '16 at 14:01
  • @robsn That post does not explain why `IF EXISTS` statements do not work as mentioned in the official docs. – floatingpurr Mar 30 '16 at 14:02
  • @Shadow probably you got the point, my version: `Server version: 5.5.47-0ubuntu0.14.04.1 (Ubuntu)` – floatingpurr Mar 30 '16 at 14:09

1 Answers1

4

From your own link:

As of MySQL 5.7.8, the IF EXISTS clause can be used, which causes the statement to produce a warning for each named account that does not exist, rather than an error.

You can find out your server version with e.g.:

select @@version;
Álvaro González
  • 142,137
  • 41
  • 261
  • 360
  • 1
    `5.5.47-0ubuntu0.14.04.1` that's the problem :( I believed I was working on last version. I'm sorry, not a great question... – floatingpurr Mar 30 '16 at 14:12