Saturday, February 7, 2015

Experimental mysql database setup scripts.

Some experimental mysql database setup scripts. (USE AT YOUR OWN RISK!)

Original script:

#!/bin/bash EXPECTED_ARGS=3 E_BADARGS=65 MYSQL=`which mysql`
#Danger do not use GRANT ALL ON *.*
Q1="CREATE DATABASE IF NOT EXISTS $1;" Q2="GRANT ALL ON *.* TO '$2'@'localhost' IDENTIFIED BY '$3';" Q3="FLUSH PRIVILEGES;" SQL="${Q1}${Q2}${Q3}" if [ $# -ne $EXPECTED_ARGS ] then echo "Usage: $0 dbname dbuser dbpass" exit $E_BADARGS fi $MYSQL -uroot -p -e "$SQL"


To use it, just run:
./createdb testdb testuser secretpass

Someone's modified script:

#!/bin/bash
BTICK=’`’
EXPECTED_ARGS=3
E_BADARGS=65
MYSQL=`which mysql`
Q1=”CREATE DATABASE IF NOT EXISTS ${BTICK}$1${BTICK};”
Q2=”GRANT ALL ON ${BTICK}$1${BTICK}.* TO ‘$2’@’localhost’ IDENTIFIED BY ‘$3′;”
Q3=”FLUSH PRIVILEGES;”
SQL=”${Q1}${Q2}${Q3}”
if [ $# -ne $EXPECTED_ARGS ]
then
echo “Usage: $0 dbname dbuser dbpass”
exit $E_BADARGS
fi
$MYSQL -uroot -p -e “$SQL”

Using a little whiptail:






#!/bin/bash

USERNAME=$(whiptail --title "Mysql username" --inputbox "What is your Mysql username?" 10 60 $USER 3>&1 1>&2 2>&3)

exitstatus=$?
if [ $exitstatus = 0 ]; then
    echo "Your pet name is:" $USERNAME
else
    echo "You chose Cancel." ; exit
fi

PASSWORD=$(whiptail --title "Mysql password" --passwordbox "Enter your password and choose Ok to continue." 10 60 3>&1 1>&2 2>&3)

exitstatus=$?
if [ $exitstatus = 0 ]; then
    echo "Your password is: XXXXXXXX"
else
    echo "You chose Cancel." ; exit
fi

DBNAME=$(whiptail --title "Database name" --inputbox "What is database name?" 10 60 DBNAME 3>&1 1>&2 2>&3)

exitstatus=$?
if [ $exitstatus = 0 ]; then
    echo "Your pet name is:" $DBNAME
else
    echo "You chose Cancel." ; exit
fi
DBUSERNAME=$(whiptail --title "Database username" --inputbox "What is your database user name?" 10 60 DATABASEUSERNAME 3>&1 1>&2 2>&3)

exitstatus=$?
if [ $exitstatus = 0 ]; then
    echo "Your pet name is:" $DBUSERNAME
else
    echo "You chose Cancel." ; exit
fi
DBHOST=$(whiptail --title "Database host name" --inputbox "What is your database host name?" 10 60 DBHOST 3>&1 1>&2 2>&3)

exitstatus=$?
if [ $exitstatus = 0 ]; then
    echo "Your pet name is:" $DBHOST
else
    echo "You chose Cancel." ; exit
fi

DBPASSWORD=$(whiptail --title "Database password" --passwordbox "What is your database password?" 10 60 DBPASSWORD 3>&1 1>&2 2>&3)

exitstatus=$?
if [ $exitstatus = 0 ]; then
    echo "Your password is : XXXXXXXX"
else
    echo "You chose Cancel." ; exit
fi

MYSQL=`which mysql`

Q1="CREATE DATABASE IF NOT EXISTS $DBNAME;"
Q2="GRANT ALL ON $DBNAME.* TO '$DBUSERNAME'@'$DBHOST' IDENTIFIED BY '$DBPASSWORD';"
Q3="FLUSH PRIVILEGES;"
SQL="${Q1}${Q2}${Q3}"

$MYSQL -u $USER -p $PASSWORD -e "$SQL"

No comments:

Post a Comment