Home > Enterprise >  Deleting vs dropping user in mysql
Deleting vs dropping user in mysql

Time:11-12

Im writing a bash script which is checking to see if database exists, if not then it will create the database and then the user.

db=mydb
user=user
echo "Creating database $db"
if [ ! -d "/var/lib/mysql/$db" ] ; then
    mysql -e "CREATE DATABASE $db /*\!CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci; */;"
    mysql -e "CREATE USER IF NOT EXISTS $user@localhost IDENTIFIED BY '$user';"
    mysql -e "GRANT ALL PRIVILEGES ON $db.* TO '$user'@'localhost';"
    mysql -e "FLUSH PRIVILEGES;"
else
    echo "Database already exists"
fi

What I am trying to do is:

If db doesnt exist then create it, there is an assumption that if the db doesnt exist then chances are neither will the user however, on the off chance there is already a user then it should grant the privileges to newly created db. To test for all possibilities i'm using phpMyAdmin to save me time when checking but discovered a weird anomaly

  • If user is created and I drop user from mysql console then script will recreate the user.
  • If user is created but I *delete* user from phpMyAdmin, I get error below when script is re-run.

ERROR 1133 (28000) at line 1: Can't find any matching row in the user table

I notice the difference in the two is the drop and delete command but why is it not recreating if deleted from phpMyAdmin, is there a record of the user somewhere when using delete?

CodePudding user response:

Found the answer here

I think the issue was down to using delete and privileges remaining. I've run the drop command via cli and all seems to be ok now.

In phpMyadmin I can delete but dont have the option to drop users whereas i can see the drop option everywhere else.

  • Related