server-scripts/create-mysql-user-and-database

29 lines
496 B
Plaintext
Raw Normal View History

2014-10-19 15:27:33 +00:00
#!/bin/sh
if [ $# -lt 1 ]
then
echo "Usage: $0 <username>"
exit 1
fi
if ! [ $EUID -eq 0 ]
then
echo "Must be root!" >&2
exit 1
fi
USER="$1"
PASSWORD="$(tr -dc A-Za-z0-9 < /dev/urandom | head -c 64 | xargs)"
cat <<EOF | mysql --host mysql --user root --password=$(cat /etc/mysql.secret)
create database $USER;
grant usage on *.* to '$USER'@'%' identified by '$PASSWORD';
grant all privileges on $USER.* to '$USER'@'%';
EOF
echo
echo "Password for user $USER is:"
echo
echo $PASSWORD
echo