reset_db_root_password.sh 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. #!/bin/bash
  2. # Author: yeho <lj2007331 AT gmail.com>
  3. # BLOG: https://blog.linuxeye.cn
  4. #
  5. # Notes: OneinStack for CentOS/RadHat 6+ Debian 7+ and Ubuntu 12+
  6. #
  7. # Project home page:
  8. # https://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 6+ Debian 7+ and Ubuntu 12+ #
  15. # Reset Database root password for OneinStack #
  16. # For more information please visit https://oneinstack.com #
  17. #######################################################################
  18. "
  19. oneinstack_dir=$(dirname "`readlink -f $0`")
  20. pushd ${oneinstack_dir} > /dev/null
  21. . ./options.conf
  22. . ./include/color.sh
  23. . ./include/check_dir.sh
  24. Input_db_root_password() {
  25. [ ! -d "${db_install_dir}" ] && { echo "${CFAILURE}Database is not installed on your system! ${CEND}"; exit 1; }
  26. while :; do echo
  27. read -p "Please input the root password of database: " New_dbrootpwd
  28. [ -n "`echo $New_dbrootpwd | grep '[+|&]'`" ] && { echo "${CWARNING}input error,not contain a plus sign (+) and &${CEND}"; continue; }
  29. (( ${#New_dbrootpwd} >= 5 )) && break || echo "${CWARNING}database root password least 5 characters! ${CEND}"
  30. done
  31. }
  32. Reset_db_root_password() {
  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. if [ "$1" == 'quiet' ]; then
  48. New_dbrootpwd="`< /dev/urandom tr -dc A-Za-z0-9 | head -c8`"
  49. sleep 2 && [ ! -e /tmp/mysql.sock ] && /etc/init.d/mysqld start
  50. Reset_db_root_password
  51. [ $? -eq 0 ] && sed -i '/reset_db_root_password/d' /etc/rc.d/rc.local
  52. else
  53. Input_db_root_password
  54. Reset_db_root_password
  55. fi
  56. popd > /dev/null