CREATE USER when user already exists I use this code to create a user:

CREATE USER "${domain}"@"localhost" IDENTIFIED BY "${dbuserp}";
CREATE DATABASE ${domain};
GRANT ALL PRIVILEGES ON ${domain}.* TO "${domain}"@"localhost";


But what if a user with that name already exists and I want to replace the existing one, with the new "blankslate" one? I thought of coping with that in the following way, but I'm not sure it's the right way:

DROP USER IF EXISTS "${domain}"@"localhost";
DROP database IF EXISTS ${domain};
CREATE USER "${domain}"@"localhost" IDENTIFIED BY "${dbuserp}";
CREATE DATABASE ${domain};
GRANT ALL PRIVILEGES ON ${domain}.* TO "${domain}"@"localhost";
Your way would seem fine. Older versions of MySQL do not have DROP USER IF EXISTS.

Here is another way that would get around that issue with older versions

DELETE FROM mysql.user WHERE user='${domain}' AND hostname='localhost';
DELETE FROM mysql.db   WHERE user='${domain}' AND hostname='localhost';
FLUSH PRIVILEGES;
DROP DATABASE IF EXISTS '${domain}';
CREATE DATABASE '${domain}';
GRANT ALL PRIVILEGES ON ${domain}.* TO '${domain}'@'localhost';