Guest User

Untitled

a guest
Nov 22nd, 2018
141
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.83 KB | None | 0 0
  1. #!/bin/bash
  2.  
  3. if [ -z $1 ]; then
  4. echo "Please give a name for the new database en user"
  5. echo "Usage: create_mysql_db_and_user [databse name]"
  6. exit;
  7. fi
  8.  
  9. dbname="admin_$1"
  10. dbuser="admin_$1"
  11. dbpass=`pwgen -s 20 1`
  12.  
  13. green=`tput setaf 2`
  14. yellow=`tput setaf 3`
  15. reset=`tput sgr0`
  16.  
  17. mysql --login-path=local -e "CREATE USER $dbuser@localhost IDENTIFIED BY '$dbpass'"
  18. mysql --login-path=local -e "CREATE DATABASE $dbname"
  19. mysql --login-path=local -e "GRANT ALL PRIVILEGES ON $dbname.* TO $dbuser@localhost IDENTIFIED BY '$dbpass'"
  20. mysql --login-path=local -e "FLUSH PRIVILEGES"
  21.  
  22. cat <<EOF > /[a_secure_path]/mysql_credentials/"$dbname"
  23. dbname: $dbname
  24. dbuser: $dbuser
  25. dbpass: $dbpass
  26. dbhost: localhost
  27. EOF
  28.  
  29. echo "${green}Your MySQL database with user has been created:${reset}"
  30. echo "${yellow}dbname:${reset} $dbname"
  31. echo "${yellow}dbpass:${reset} $dbpass"
Add Comment
Please, Sign In to add comment