Last active
January 24, 2021 16:51
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 ======= |
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
Comments in MySQL started from
--
.