Creating MySQL databases securely

When building sites I often need to set-up a MySQL database which has all proper settings so I can start working right away knowing that all is fine. Here is a little script which I use to do this;

#!/bin/sh

if [ ! -n “$1” ]; then
echo “Invoke as ./createdb.sh DATABASE_NAME”
exit -1
fi

DBNAME=”$1″

mysqladmin –user=root –password=SOMEPASS create $DBNAME

PASS=`perl -e ‘for($i=0;$i<6;$i++){print rand()*26%26};'`
mysql –user=root –password=SOMEPASS -e “grant all privileges on $DBNAME.* to $DBNAME@’localhost’ identified by ‘$PASS’;”
mysql –user=root –password=SOMEPASS -e “flush privileges;”

echo $PASS

Be the first to leave a comment. Don’t be shy.

Join the Discussion

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>