#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2014
    Posts
    5
    Rep Power
    0

    Create User (if not exists)


    I have a SQL script that creates a MySQL database, needed tables, inserts some basic info, creates the application's MySQL user, and assigns privileges to that MySQL user on the application's database.

    Now, if an installation goes awry and I need to reinstall their database, tables, etc. back to factory default, I import the same SQL script. With databases and tables, I can simply use the "IF NOT EXISTS" directive and with my INSERT statements I can simply use "IGNORE" to pass over values that are still there.

    The issue I am running into is if the db specific user is still present in the MySQL users table, the script stops processing and errors out,
    Code:
    "#1396 - Operation CREATE USER failed for 'MyDBUser'@'%' "
    SO I guess my question is this: is there a way or directive similar to IF NOT EXISTS or IGNORE that I can use with CREATE USER that will check to see if a user exists before trying to create the user? In other words...to ignore recreating the user if the MySQL user already exists?
  2. #2
  3. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2014
    Posts
    5
    Rep Power
    0
    Nevermind. I found simply using a GRANT statement instead of CREATE USER I could also specify the user password and GRANT checks for the existence beforehand.

IMN logo majestic logo threadwatch logo seochat tools logo