重置:mysql-5.7 root用户密码为:root

时间:2021-10-19 06:40:10
# reset-root.sh
# 功能:初始化mysql-5.7初始密码的脚本 root/root
# 时间:2017/08/02 10:46:07
#!/bin/bash

#mycnf="./my.cnf"
mycnf=" /etc/my.cnf"

# IMPORTANT IMPORTANT IMPORTANT IMPORTANT IMPORTANT IMPORTANT
sed -i '/symbolic-links=0/a\skip-grant-tables=1' $mycnf

service mysqld restart

if [ `rpm -qa | grep expect | wc -l` -eq 0 ]
then
yum clean all
yum install -y expect
fi

/usr/bin/expect <<-EOF
spawn mysql -uroot

expect "*mysql>"
send "use mysql;\r"
#send "SET PASSWORD = PASSWORD('root');\r";
send "update user set authentication_string = password('root'), \
password_expired = 'N', password_last_changed = now() where user = 'root';\r"
;

send "flush privileges;\r"

send "delete from user where user<>'root';\r"
send "update user set host='%' where user='root';\r"
send "flush privileges;\r"
send "grant all PRIVILEGES on *.* to 'slave'@'%' identified by 'slave';\r"
send "flush privileges;\r"
# 退出MySQL
expect "*mysql>"
send "exit;\r";

expect eof
EOF

# IMPORTANT IMPORTANT IMPORTANT IMPORTANT IMPORTANT IMPORTANT
sed -i -e '/skip-grant-tables=1/d' $mycnf
service mysqld restart