Bash script to create MySQL database and user

words by Brian Racer

Here is a little script I made to quickly and easily create users and databases for MySQL. I only use this for development, for actual deployed applications you would probably want to be more specific about the privileges given:

#!/bin/bash
 
EXPECTED_ARGS=3
E_BADARGS=65
MYSQL=`which mysql`
 
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

That command would create a database named testdb, and user testuser with the password of secretpass.

  • Eiso

    Thank you for this script – it saved me some time!

  • Eiso

    Thank you for this script – it saved me some time!

  • perry

    props, thanks for the script

  • perry

    props, thanks for the script

  • Alfonso

    Very useful script. Thanks

  • Alfonso

    Very useful script. Thanks

  • Jah

    thanks

  • Jah

    thanks

  • grant ALL ON *.* seems to give all privileges to ALL databases, shouldn’t it be GRANT ALL ON ‘$1’.* ?
    I see in it a security hole, just preventing future users of this script that executing will allow access to all databases and not only the specified one.

    also warning, “ALL” privileges allow shutting down database for example…

    regards.

  • grant ALL ON *.* seems to give all privileges to ALL databases, shouldn’t it be GRANT ALL ON ‘$1’.* ?
    I see in it a security hole, just preventing future users of this script that executing will allow access to all databases and not only the specified one.

    also warning, “ALL” privileges allow shutting down database for example…

    regards.

  • Thanks by you code, its excellent

  • gabriel

    ammm… nunca me habian baneado mi habbo pero me lo banearon me llego un mensaje diciendo
    dame tu contraseña o te quitaremos tu habbo y di mi contraseñe y me salio has sido jakeado la
    razon es k te drogas y yo nunca me he drogado al revez io pague por creditos por favor desbane mi
    habbo :recompensa 20 habbo creditos

  • gabriel

    amm.mi habbo es starcool654 como ia saben me llego el mensaje diciendo dame tu contraseña o te quitaremos tu habbo ia saben k de recompensa son 20 habbo creditos por favor debane mi habbo io no me drogo

  • Pingback: Delicious Bookmarks for August 15th through August 16th « Lâmôlabs()

  • Technology Manager

    #!/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”

  • M00seman645

    Awesome love this for just testing thanks a ton

  • Thanks, very useful :). I made some changes though, I didn’t want every user to have access to every database as someone else suggested too.

  • Konstantin

    Thanks a lot ! You’ve save my time!!!