Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/bin/bash
- if [ -z $1 ]; then
- echo "Please give a name for the new database en user"
- echo "Usage: create_mysql_db_and_user [databse name]"
- exit;
- fi
- dbname="admin_$1"
- dbuser="admin_$1"
- dbpass=`pwgen -s 20 1`
- green=`tput setaf 2`
- yellow=`tput setaf 3`
- reset=`tput sgr0`
- mysql --login-path=local -e "CREATE USER $dbuser@localhost IDENTIFIED BY '$dbpass'"
- mysql --login-path=local -e "CREATE DATABASE $dbname"
- mysql --login-path=local -e "GRANT ALL PRIVILEGES ON $dbname.* TO $dbuser@localhost IDENTIFIED BY '$dbpass'"
- mysql --login-path=local -e "FLUSH PRIVILEGES"
- cat <<EOF > /[a_secure_path]/mysql_credentials/"$dbname"
- dbname: $dbname
- dbuser: $dbuser
- dbpass: $dbpass
- dbhost: localhost
- EOF
- echo "${green}Your MySQL database with user has been created:${reset}"
- echo "${yellow}dbname:${reset} $dbname"
- echo "${yellow}dbpass:${reset} $dbpass"
Add Comment
Please, Sign In to add comment