reset_db_root_password.sh 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. #!/bin/bash
  2. # Author: yeho <lj2007331 AT gmail.com>
  3. # BLOG: https://linuxeye.com
  4. #
  5. # Notes: OneinStack for CentOS/RedHat 7+ Debian 9+ and Ubuntu 16+
  6. #
  7. # Project home page:
  8. # https://oneinstack.com
  9. # https://github.com/oneinstack/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 7+ Debian 9+ and Ubuntu 16+ #
  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. Show_Help() {
  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}" && Show_Help && exit 1
  36. eval set -- "${TEMP}"
  37. while :; do
  38. [ -z "$1" ] && break;
  39. case "$1" in
  40. -h|--help)
  41. Show_Help; exit 0
  42. ;;
  43. -q|--quiet)
  44. quiet_flag=y; shift 1
  45. ;;
  46. -f|--force)
  47. force_flag=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}" && Show_Help && exit 1
  58. ;;
  59. esac
  60. done
  61. Input_dbrootpwd() {
  62. while :; do echo
  63. read -e -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. service mysqld stop > /dev/null 2>&1
  87. while [ -n "`ps -ef | grep mysqld | grep -v grep | awk '{print $2}'`" ]; do
  88. sleep 1
  89. done
  90. echo "${CMSG}skip grant tables...${CEND}"
  91. sed -i '/\[mysqld\]/a\skip-grant-tables' /etc/my.cnf
  92. service mysqld start > /dev/null 2>&1
  93. sed -i '/^skip-grant-tables/d' /etc/my.cnf
  94. while [ -z "`ps -ef | grep 'mysqld ' | grep -v grep | awk '{print $2}'`" ]; do
  95. sleep 1
  96. done
  97. if echo "${DB_Ver}" | grep -Eqi '^8.0.|^5.7.|^10.[4-5].|^10.11.'; then
  98. ${db_install_dir}/bin/mysql -uroot -hlocalhost << EOF
  99. update mysql.user set authentication_string=password("${New_dbrootpwd}") where user="root";
  100. flush privileges;
  101. EOF
  102. else
  103. ${db_install_dir}/bin/mysql -uroot -hlocalhost << EOF
  104. update mysql.user set password = Password("${New_dbrootpwd}") where User = 'root';
  105. EOF
  106. fi
  107. if [ $? -eq 0 ]; then
  108. killall mysqld
  109. while [ -n "`ps -ef | grep mysqld | grep -v grep | awk '{print $2}'`" ]; do
  110. sleep 1
  111. done
  112. [ -n "`ps -ef | grep mysqld | grep -v grep | awk '{print $2}'`" ] && ps -ef | grep mysqld | grep -v grep | awk '{print $2}' | xargs kill -9 > /dev/null 2>&1
  113. service mysqld start > /dev/null 2>&1
  114. sed -i "s+^dbrootpwd.*+dbrootpwd='${New_dbrootpwd}'+" ./options.conf
  115. [ -e ~/ReadMe ] && sed -i "s+^MySQL root password:.*+MySQL root password: ${New_dbrootpwd}+" ~/ReadMe
  116. echo
  117. echo "Password reset succesfully! "
  118. echo "The new password: ${CMSG}${New_dbrootpwd}${CEND}"
  119. echo
  120. fi
  121. }
  122. [ "${password_flag}" == 'y' ] && quiet_flag=y
  123. if [ "${quiet_flag}" == 'y' ]; then
  124. if [ "${force_flag}" == 'y' ]; then
  125. Reset_force_dbrootpwd
  126. else
  127. sleep 2 && [ ! -e /tmp/mysql.sock ] && service mysqld start
  128. Reset_Interaction_dbrootpwd
  129. fi
  130. else
  131. Input_dbrootpwd
  132. if [ "${force_flag}" == 'y' ]; then
  133. Reset_force_dbrootpwd
  134. else
  135. Reset_Interaction_dbrootpwd
  136. fi
  137. fi
  138. popd > /dev/null