您的位置:首页 > 数据库 > MySQL

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

2017-08-02 23:05 543 查看
# 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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  mysql shell