I tried to recreate a DB user and a DB instance (run over the existing ones), so I ran the following two commands in the Mysql CLI:

create user '${test}'@'localhost' identified by '${psw}';
create database ${test};

I got this output:

ERROR 1396 (HY000) at line 1: Operation CREATE USER failed for 'test'@'localhost'

The reason for the error, ofcourse, is because the user already exist).

My question

How could I make sure the above commands will run over the existing data if I already have a DB user and a DB instance with the same value (test)?

Update after answer

mysql -V brings:

mysql Ver 14.14 Distrib 5.7.20, for Linux (x86_64) using EditLine wrapper

1 Answer 1


Simply use if not exists in your queries. For example:

create if not exists user '${test}'@'localhost' identified by '${psw}'; or create database if not exists ${test};

Your mysql version might be different, refer to right version. But more information could be found at:

  • if not exists? But these does exist, I want to run over / run through, the existing ones with the same name (or otherwise I've missed the point). Dec 2, 2017 at 5:09
  • I've edited the question to best explain myself. Dec 2, 2017 at 5:20
  • @Arcticooling And what should in your opinion be the result of "running over" an existing user? You can run drop user first. Dec 2, 2017 at 8:46
  • It would be better to understand what you want to achieve with "running over"? Change passwords for existing users? Dec 2, 2017 at 11:19
  • By running I mean to reacreate them arbitraraily --- you could say it's one action combining drop and create for the same value, I guess. Dec 2, 2017 at 15:43

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .