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