Skip to content

Instantly share code, notes, and snippets.

@cheynewallace
Last active January 24, 2021 16:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cheynewallace/7937405 to your computer and use it in GitHub Desktop.
Save cheynewallace/7937405 to your computer and use it in GitHub Desktop.
Simple script using prepared statements to create a MySQL database, with user that has same name as the database, then grant the user full permissions on the database. Simply change the two variable assignments at the top
# Only change these two variables.
# DATABASENAME is used for both the database name and username
SET @DATABASENAME = "service_abc";
SET @USERPASSWORD = "password123";
# ======== PERFORM =========
# Drop Database If Exists
SET @DROP_DB = concat("DROP DATABASE IF EXISTS ",@DATABASENAME);
PREPARE statement FROM @DROP_DB;
EXECUTE statement;
# Create Database
SET @CREATE_DB = concat("CREATE DATABASE ",@DATABASENAME);
PREPARE statement FROM @CREATE_DB;
EXECUTE statement;
# Create User
SET @CREATE_USER = concat("CREATE USER '",@DATABASENAME,"' IDENTIFIED BY '",@USERPASSWORD,"'");
PREPARE statement FROM @CREATE_USER;
EXECUTE statement;
# Grant Permissions
SET @GRANT_PERMISSIONS = concat("GRANT ALL PRIVILEGES ON ",@DATABASENAME,".* to '",@DATABASENAME,"'");
PREPARE statement FROM @GRANT_PERMISSIONS;
EXECUTE statement;
# ====== END PERFORM =======
@egorsmkv
Copy link

Comments in MySQL started from --.

@beinoriusju
Copy link

Shouldn't we also release prepared statements?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment