mysql-db.sh 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. #!/bin/sh
  2. echo -e '
  3. _ _ _ _
  4. __| | |__ | | | |___ ___ _ _
  5. / _` | _ \ | |_| (_-</ -_) _|
  6. \__,_|_.__/ \___//__/\___|_|
  7. '
  8. echo -e "Create new mysql db and user (you will be asked a db name and a password)"
  9. . bin/checkroot.sh
  10. sleep 3
  11. # configure
  12. echo -n "Please provide the mysql root passwd : "
  13. read _root_mysql_passwd
  14. mysql -u root -p$_root_mysql_passwd -e "show databases;"
  15. echo -n "Enter new db name: "
  16. read db_name
  17. while [ "$db_name" = "" ]
  18. do
  19. read -p "enter a db name ? " db_name
  20. if [ "$db_name" != "" ]; then
  21. # TODO check if db already exists
  22. # if id "$db_name" >/dev/null 2>&1; then
  23. # echo "user $db_name alreday exists, you must provide a non existing user name."
  24. # db=""
  25. # else
  26. read -p "is db name $db_name correcte [y|n] " validated
  27. if [ "$validated" = "y" ]; then
  28. break
  29. else
  30. db_name=""
  31. fi
  32. # fi
  33. fi
  34. done
  35. # generate random password for new mysql user
  36. _passwd="$(< /dev/urandom tr -dc _A-Z-a-z-0-9 | head -c16)"
  37. # create new mysql user
  38. mysql -u root -p$_root_mysql_passwd -e "CREATE DATABASE $db_name;"
  39. mysql -u root -p$_root_mysql_passwd -e "CREATE USER '$db_name'@'localhost' IDENTIFIED BY '$_passwd';"
  40. mysql -u root -p$_root_mysql_passwd -e "GRANT ALL ON $db_name.* TO '$db_name'@'localhost';"
  41. mysql -u root -p$_root_mysql_passwd -e "show databases;"
  42. echo "database and user : $db_name installed"
  43. echo " please record your password $_passwd"
  44. echo "press any key to continue."
  45. read continu