reset_db_root_password.sh 1.7 KB

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