#!/bin/bash MYCNF=$HOME/.my.cnf DBHOST=mysql0 GRANTHOST='%' [ -f /etc/egscripts/egmakedb.cnf ] && . /etc/egscripts/egmakedb.cnf SIMULATE=0 usage() { cat <&2 exit 1 fi # In addition to granting all privileges for $user, create a user # specific for this db. This will be in the format $user_1, $user_2 # up to $user_99, with the lowest available number being chosen. dbuser=`get_next_username $user` if [ -z "$dbuser" ] ; then echo "Unable to generate a new username for db $db" >&2 exit 1 fi printf 'CREATE DATABASE `%s`;' $dbname | $mysql # If a .my.cnf file exists for the main user, assume they # already have a password set. Otherwise create a new MySQL user # and a .my.cnf in their home directory. if [ ! -e /home/${user}/.my.cnf ] ; then passwd=`pwgen -B 10 1` extra_grants=" IDENTIFIED BY '$passwd'" if [ $SIMULATE -eq 1 ] ; then echo "Create /home/${user}/.my.cnf" else if [ "$LOGNAME" != "$user" ] ; then runasuser="sudo -u $user" fi $runasuser egcreatemycnf /home/${user}/.my.cnf $DBHOST $user $passwd fi fi # Grant all privileges on the database to the main user $mysql < /dev/null ; then exit 1 fi # Exit on any error set -e create_db $dbname $user