Skip to content

Instantly share code, notes, and snippets.

@rutcreate
Created December 7, 2019 03:16
Show Gist options
  • Save rutcreate/a3c78815ee3ac098afd242a805f328d2 to your computer and use it in GitHub Desktop.
Save rutcreate/a3c78815ee3ac098afd242a805f328d2 to your computer and use it in GitHub Desktop.
Create MySQL database, user, and grant permissions for local development
# $ create_mysql_database_and_user dbname [username [password]]
function create_mysql_database_and_user() {
DB=$1
USR=$1
if [[ $2 ]]; then
USR=$2
fi
PWD=$USR
if [[ $3 ]]; then
PWD=$3
fi
CMD="CREATE DATABASE $1; CREATE USER '$USR'@'%' IDENTIFIED BY '$PWD'; GRANT ALL ON $DB.* TO '$USR'@'%';"
mysql -uroot -e $CMD
LIST_USER_CMD="SELECT user, host FROM mysql.user WHERE user = '$USR'"
mysql -uroot -e $LIST_USER_CMD
LIST_GRANT_CMD="SHOW GRANTS FOR '$USR'"
mysql -uroot -e $LIST_GRANT_CMD
}
# $ drop_mysql_user username
function drop_mysql_user() {
CMD="DROP USER $1;"
mysql -uroot -e $CMD
}
# $ drop_mysql_database dbname
function drop_mysql_database() {
CMD="DROP DATABASE $1;"
mysql -uroot -e $CMD
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment