You are on page 1of 3

#/bin/bash

# create a mysql database


# The first argument is the db_name, the second db_user_name and the
# third is the unix username. All arguments optional. The unix_user_name
# is ignored if you are not root.
db_name="$1"
db_user_name="$2"
unix_user_name="$3"
MF_HELPER="sudo -u mysql-creator -H /usr/local/sbin/mf-mysql-create-helper"
function get_db_name ()
{
db_name="$1"
if [ -z "$db_name" ]; then
echo
read -p "DB NAME - Enter the name of the database to create: " d
b_name
fi
if [ -z "$db_name" ]; then
echo
echo "Error: you must enter a database name, even if you don't"
echo "want to create a database, otherwise the database user"
echo "won't have access to anything. If you enter a database "
echo "name that exists, it will not be affected."
echo "Please try again."
get_db_name
fi
# make sure only a-z is in the db name
$MF_HELPER sanitize "$db_name"
if [[ "$?" -ne "0" ]]; then
echo
echo "Error"
echo "DB Name: '$db_name'"
echo "Please only include lower case letters, underscores "
echo "or numbers in your database name."
get_db_name
fi
$MF_HELPER check_length "$db_name" 64
if [[ "$?" -ne "0" ]]; then
echo
echo "Error"
echo "DB Name: '$db_name'"
echo "Please enter a database name that is less than 64 characte
rs."
get_db_name
fi
# make sure this database does not already exists
$MF_HELPER db_exists "$db_name"
if [[ "$?" -eq 0 ]]; then
echo
echo "Error"

echo "DB Name: '$db_name'"


echo "That database already exists. Please choose a different da
tabase name."
get_db_name
fi
}
function get_db_user_name()
{
db_user_name="$1"
if [ -z "$db_user_name" ]; then
echo
echo "USER NAME - Enter the name of the database user to create.
"
read -p "Leave blank if you do not want to create a database use
r: " db_user_name
fi
if [[ ! -z "$db_user_name" ]]; then
$MF_HELPER sanitize "$db_user_name"
if [[ "$?" -ne 0 ]]; then
echo
echo "Error"
echo "DB User Name: '$db_user_name'"
echo "Please only include lower case letters, "
echo "underscores or numbers in your database user name.
"
get_db_user_name
fi
$MF_HELPER check_length "$db_user_name" 16
if [[ "$?" -ne "0" ]]; then
echo
echo "Error"
echo "DB User Name: '$db_user_name'"
echo "Please enter a database user name that is less tha
n 16 characters."
get_db_user_name
fi
# make sure this user does not already exists
$MF_HELPER user_exists "$db_user_name"
if [ "$?" -eq 0 ]; then
echo
echo "Error"
echo "DB User Name: '$db_user_name'"
echo "That user name already exists. Please choose a dif
ferent username."
get_db_user_name
fi
fi
}
function get_unix_user_homedir()
{
me=$(whoami)
if [ "root" = "$me" ]; then

if [[ -z "$unix_user_name" ]]; then


read -p "Unix user name that will use the database: "
unix_user_name="$REPLY"
fi
unix_user_info=$(getent passwd "$unix_user_name")
if [ -z "$unix_user_info" ]; then
echo "That user doesn't exist."
unix_user_name=
get_unix_user_homedir
fi
unix_user_homedir=$(echo "$unix_user_info" | cut -d: -f6)
else
unix_user_homedir="$HOME"
unix_user_name="$me"
fi
}
get_db_name "$db_name"
get_db_user_name "$db_user_name"
$MF_HELPER create_db "$db_name"
if [ "$?" -ne 0 ]; then
echo "There was an error creating the database."
exit
else
echo "Database created."
echo "Database name: $db_name"
fi
passwd=$(pwgen -s -c -n)
if [ -n "$db_user_name" ]; then
$MF_HELPER create_user "$db_name" "$db_user_name" "$passwd"
if [ "$?" -ne 0 ]; then
echo "There was an error creating the db user"
exit
else
echo "Database user created."
echo "Database user: $db_user_name"
echo "Database user password: $passwd"
get_unix_user_homedir
my_cnf="$unix_user_homedir/.my.cnf"
if [ ! -f "$my_cnf" ]; then
echo "[client]" >> "$my_cnf"
echo "user=$db_user_name" >> "$my_cnf"
echo "password=$passwd" >> "$my_cnf"
echo "database=$db_name" >> "$my_cnf"
chmod 600 "$my_cnf"
[[ "root" = $(whoami) ]] && chown "$unix_user" "$my_cnf"
echo "~/.my.cnf created."
else
echo "Not creating .my.cnf file. It already exists."
fi
fi
fi

You might also like