reset_db_root_password.sh 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. #!/bin/bash
  2. # Author: yeho <lj2007331 AT gmail.com>
  3. # BLOG: https://blog.linuxeye.com
  4. #
  5. # Notes: OneinStack for CentOS/RadHat 5+ Debian 6+ and Ubuntu 12+
  6. #
  7. # Project home page:
  8. # http://oneinstack.com
  9. # https://github.com/lj2007331/oneinstack
  10. export PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
  11. clear
  12. printf "
  13. #######################################################################
  14. # OneinStack for CentOS/RadHat 5+ Debian 6+ and Ubuntu 12+ #
  15. # Reset Database root password for OneinStack #
  16. # For more information please visit http://oneinstack.com #
  17. #######################################################################
  18. "
  19. . ./options.conf
  20. . ./include/color.sh
  21. # Check if user is root
  22. [ $(id -u) != "0" ] && { echo "${CFAILURE}Error: You must be root to run this script${CEND}"; exit 1; }
  23. Reset_db_root_password()
  24. {
  25. [ ! -d "$db_install_dir" ] && { echo "${CFAILURE}The Database is not installed on your system! ${CEND}"; exit 1; }
  26. while :
  27. do
  28. echo
  29. read -p "Please input the root password of database: " New_dbrootpwd
  30. [ -n "`echo $New_dbrootpwd | grep '[+|&]'`" ] && { echo "${CWARNING}input error,not contain a plus sign (+) and &${CEND}"; continue; }
  31. (( ${#New_dbrootpwd} >= 5 )) && break || echo "${CWARNING}database root password least 5 characters! ${CEND}"
  32. done
  33. $db_install_dir/bin/mysqladmin -uroot -p"$dbrootpwd" password "$New_dbrootpwd" -h localhost > /dev/null 2>&1
  34. status_Localhost=`echo $?`
  35. $db_install_dir/bin/mysqladmin -uroot -p"$dbrootpwd" password "$New_dbrootpwd" -h 127.0.0.1 > /dev/null 2>&1
  36. status_127=`echo $?`
  37. if [ $status_Localhost == '0' -a $status_127 == '0' ]; then
  38. sed -i "s+^dbrootpwd.*+dbrootpwd='$New_dbrootpwd'+" ./options.conf
  39. echo
  40. echo "Password reset succesfully! "
  41. echo "The new password: ${CMSG}${New_dbrootpwd}${CEND}"
  42. echo
  43. else
  44. echo "${CFAILURE}Reset Database root password failed! ${CEND}"
  45. fi
  46. }
  47. Reset_db_root_password