reset_db_root_password.sh 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. #!/bin/bash
  2. # Author: yeho <lj2007331 AT gmail.com>
  3. # BLOG: https://blog.linuxeye.cn
  4. #
  5. # Notes: OneinStack for CentOS/RedHat 6+ Debian 7+ and Ubuntu 12+
  6. #
  7. # Project home page:
  8. # https://oneinstack.com
  9. # https://github.com/lj2007331/oneinstack
  10. export PATH=/sbin:/bin:/usr/sbin:/usr/bin:/usr/local/sbin:/usr/local/bin
  11. clear
  12. printf "
  13. #######################################################################
  14. # OneinStack for CentOS/RedHat 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. [ ! -d "${db_install_dir}" ] && { echo "${CFAILURE}Database is not installed on your system! ${CEND}"; exit 1; }
  25. showhelp() {
  26. echo "Usage: $0 command ...[parameters]....
  27. -h, --help print this help.
  28. -q, --quiet quiet operation.
  29. -f, --force Lost Database Password? Forced reset password.
  30. -p, --password [pass] DB super password.
  31. "
  32. }
  33. New_dbrootpwd="`< /dev/urandom tr -dc A-Za-z0-9 | head -c8`"
  34. TEMP=`getopt -o hqfp: --long help,quiet,force,password: -- "$@" 2>/dev/null`
  35. [ $? != 0 ] && echo "${CWARNING}ERROR: unknown argument! ${CEND}" && showhelp && exit 1
  36. eval set -- "${TEMP}"
  37. while :; do
  38. [ -z "$1" ] && break;
  39. case "$1" in
  40. -h|--help)
  41. showhelp; exit 0
  42. ;;
  43. -q|--quiet)
  44. quiet_yn=y; shift 1
  45. ;;
  46. -f|--force)
  47. force_yn=y; shift 1
  48. ;;
  49. -p|--password)
  50. New_dbrootpwd=$2; shift 2
  51. password_flag=y
  52. ;;
  53. --)
  54. shift
  55. ;;
  56. *)
  57. echo "${CWARNING}ERROR: unknown argument! ${CEND}" && showhelp && exit 1
  58. ;;
  59. esac
  60. done
  61. Input_dbrootpwd() {
  62. while :; do echo
  63. read -p "Please input the root password of database: " New_dbrootpwd
  64. [ -n "`echo ${New_dbrootpwd} | grep '[+|&]'`" ] && { echo "${CWARNING}input error,not contain a plus sign (+) and &${CEND}"; continue; }
  65. (( ${#New_dbrootpwd} >= 5 )) && break || echo "${CWARNING}database root password least 5 characters! ${CEND}"
  66. done
  67. }
  68. Reset_Interaction_dbrootpwd() {
  69. ${db_install_dir}/bin/mysqladmin -uroot -p"${dbrootpwd}" password "${New_dbrootpwd}" -h localhost > /dev/null 2>&1
  70. status_Localhost=`echo $?`
  71. ${db_install_dir}/bin/mysqladmin -uroot -p"${dbrootpwd}" password "${New_dbrootpwd}" -h 127.0.0.1 > /dev/null 2>&1
  72. status_127=`echo $?`
  73. if [ ${status_Localhost} == '0' -a ${status_127} == '0' ]; then
  74. sed -i "s+^dbrootpwd.*+dbrootpwd='${New_dbrootpwd}'+" ./options.conf
  75. echo
  76. echo "Password reset succesfully! "
  77. echo "The new password: ${CMSG}${New_dbrootpwd}${CEND}"
  78. echo
  79. else
  80. echo "${CFAILURE}Reset Database root password failed! ${CEND}"
  81. fi
  82. }
  83. Reset_force_dbrootpwd() {
  84. DB_Ver="`${db_install_dir}/bin/mysql_config --version`"
  85. echo "${CMSG}Stopping MySQL...${CEND}"
  86. /etc/init.d/mysqld stop > /dev/null 2>&1
  87. while [ -n "`ps -ef | grep mysql | grep -v grep | awk '{print $2}'`" ]; do
  88. sleep 1
  89. done
  90. echo "${CMSG}skip grant tables...${CEND}"
  91. ${db_install_dir}/bin/mysqld_safe --skip-grant-tables > /dev/null 2>&1 &
  92. while [ -z "`ps -ef | grep 'mysqld ' | grep -v grep | awk '{print $2}'`" ]; do
  93. sleep 1
  94. done
  95. if echo "${DB_Ver}" | grep -Eqi '^8.0.|^5.7.|^10.2.'; then
  96. ${db_install_dir}/bin/mysql -uroot -hlocalhost << EOF
  97. flush privileges;
  98. alter user 'root'@'localhost' identified by "${New_dbrootpwd}";
  99. alter user 'root'@'127.0.0.1' identified by "${New_dbrootpwd}";
  100. EOF
  101. else
  102. ${db_install_dir}/bin/mysql -uroot -hlocalhost << EOF
  103. update mysql.user set password = Password("${New_dbrootpwd}") where User = 'root';
  104. EOF
  105. fi
  106. if [ $? -eq 0 ]; then
  107. killall mysqld
  108. while [ -n "`ps -ef | grep mysql | grep -v grep | awk '{print $2}'`" ]; do
  109. sleep 1
  110. done
  111. [ -n "`ps -ef | grep mysql | grep -v grep | awk '{print $2}'`" ] && ps -ef | grep mysql | grep -v grep | awk '{print $2}' | xargs kill -9 > /dev/null 2>&1
  112. /etc/init.d/mysqld start > /dev/null 2>&1
  113. sed -i "s+^dbrootpwd.*+dbrootpwd='${New_dbrootpwd}'+" ./options.conf
  114. echo
  115. echo "Password reset succesfully! "
  116. echo "The new password: ${CMSG}${New_dbrootpwd}${CEND}"
  117. echo
  118. fi
  119. }
  120. [ "${password_flag}" == 'y' ] && quiet_yn=y
  121. if [ "${quiet_yn}" == 'y' ]; then
  122. if [ "${force_yn}" == 'y' ]; then
  123. Reset_force_dbrootpwd
  124. else
  125. sleep 2 && [ ! -e /tmp/mysql.sock ] && /etc/init.d/mysqld start
  126. Reset_Interaction_dbrootpwd
  127. [ $? -eq 0 ] && [ -n "`grep reset_db_root_password /etc/rc.local`" ] && sed -i '/reset_db_root_password/d' /etc/rc.d/rc.local > /dev/null 2>&1
  128. fi
  129. else
  130. Input_dbrootpwd
  131. if [ "${force_yn}" == 'y' ]; then
  132. Reset_force_dbrootpwd
  133. else
  134. Reset_Interaction_dbrootpwd
  135. fi
  136. fi
  137. popd > /dev/null