mysql 恢复忘记root密码脚本-centos

#!/bin/bash
newpassword=$1
if [ “$newpassword” == “” ];then
echo “password is emty. please use ./mysql_password_recovery.sh yourpassword”
exit
fi
echo ” Stopping mysql…”
systemctl stop mysqld
echo ” Setting the mySQL environment option..”
systemctl set-environment MYSQLD_OPTS=”–skip-grant-tables”
echo “Startng mysql ignoring password verification..”
systemctl start mysqld
echo “Updating the root user password with your provided password”
# mysql -u root
echo “UPDATE mysql.user SET authentication_string = PASSWORD(‘$newpassword’) WHERE User = ‘root’ AND Host = ‘localhost’;” | mysql -u root
echo “FLUSH PRIVILEGES;” | mysql -u root
#echo “quit” | mysql -u root
echo “Stopping mysql..”
systemctl stop mysqld
echo “Unsetting the mySQL envitroment option so it starts normally next time..”
systemctl unset-environment MYSQLD_OPTS
echo “Start mysql normally..”
systemctl start mysqld

echo “Try to login using your new password:”
mysql -u root -p

发表评论

电子邮件地址不会被公开。 必填项已用*标注