create-database-config.sh (1483B)
1 #!/bin/bash 2 3 usage() { 4 echo "Usage: $0 [-r <root-username>] [-p <root password>] [-d <database>] [ -u username ] [ -c password ]" 1>&2; 5 exit 1; 6 } 7 8 ROOT_USER=root 9 ROOT_PASSWORD= 10 DATABASE=openrat 11 USER=openrat 12 PASSWORD= 13 14 while getopts "r:p:d:u:c:h" o; do 15 case "${o}" in 16 r) 17 ROOT_USER=${OPTARG} 18 ;; 19 p) 20 ROOT_PASSWORD=${OPTARG} 21 ;; 22 d) 23 DATABASE=${OPTARG} 24 ;; 25 h) 26 usage; exit 1; 27 ;; 28 u) 29 USER=${OPTARG} 30 ;; 31 c) 32 PASSWORD=${OPTARG} 33 ;; 34 *) 35 usage 36 ;; 37 esac 38 done 39 shift $((OPTIND-1)) 40 41 if [ -z "${PASSWORD}" ]; then 42 PASSWORD=`< /dev/urandom tr -dc _A-Z-a-z-0-9 | head -c${1:-32}` 43 fi 44 45 46 function mysqlcmd { 47 CMD=$1 48 mysql --silent --user=$ROOT_USER --password=$ROOT_PASSWORD --execute="$CMD" 49 } 50 51 mysqlcmd "CREATE DATABASE $DATABASE;" 52 mysqlcmd "CREATE USER '$USER'@'%';" 53 mysqlcmd "ALTER USER '$USER'@'%' IDENTIFIED BY '$PASSWORD';" 54 mysqlcmd "GRANT SELECT, INSERT, UPDATE, DELETE, CREATE, INDEX, DROP, ALTER ON $DATABASE.* TO '$USER'@'%';"; 55 mysqlcmd "FLUSH PRIVILEGES;" 56 57 echo "# database configuration for openrat content management system" 58 echo "# copy this to config.yml or config-<host>.yml" 59 echo "database:" 60 echo " db_$DATABASE:" 61 echo " dsn : \"mysql:host=localhost; dbname=$DATABASE; charset=utf8\"" 62 echo " user: \"$USER\"" 63 echo " password: \"$PASSWORD\"" 64 echo