#!/bin/bash
if [[ $# -eq 0 ]]; then
echo "Usage: $0 <db_name>"
exit 1
fi
/usr/bin/mysqld_safe > /dev/null 2>&1 &
echo "=> Creating database $1"
RET=1
while [[ RET -ne 0 ]]; do
sleep 5
mysql -uroot -e "CREATE DATABASE $1"
RET=$?
done
mysqladmin -uroot shutdown
echo "=> Done!"

#!/bin/bash
###############################################################################
# Create MySQL Database
###############################################################################

## MySQL Database name
MYSQL_DB_NAME=${MYSQL_DB_NAME}

## Check if database name was given
if [[ -z "${MYSQL_DB_NAME}" ]]; then
  echo "No Database name given, exiting."
  exit 1
fi

echo "Creating MySQL database"
## Start MySQL service
echo "Starting MySQL service"
/usr/bin/mysqld_safe > /dev/null 2>&1 &

## Check if mysql service is up
RET=1

while [[ RET -ne 0 ]]; do
  echo "=> Waiting for confirmation of MySQL service startup"

  sleep 5

  mysql -uroot -e "status" > /dev/null 2>&1

  RET=$?
done

## Creating database ${MYSQL_ADMIN_USER}
echo "Creating ${MYSQL_DB_NAME}"
mysql -uroot -e "CREATE DATABASE IF NOT EXISTS ${MYSQL_DB_NAME}"

## Grant permissions to user
# If user is empty, exits script.
if [[ -z "${MYSQL_USER}" ]]; then
  echo "No user name given, exiting."
  exit 1
fi

echo "Creating ${MYSQL_USER}"
mysql -uroot -e "GRANT ALL PRIVILEGES ON ${MYSQL_DB_NAME}.* TO ${MYSQL_USER}@'%' IDENTIFIED BY '${MYSQL_USER_PASSWD}'"

## Stop MySQL service
echo "Stopping MySQL service"
mysqladmin -uroot shutdown

Check if user and database were given and create database and user with permissions
Check if user and database were given and create database and user with permissions
