1. 程式人生 > >mysql數據庫密碼忘記恢復腳本

mysql數據庫密碼忘記恢復腳本

sdff

#!/bin/bash
skip=awk ‘/skip-grant-tables/{print $1}‘ /etc/my.cnf
if [ ! -n "$skip" ];then ##判斷是否有skip-grant-tables
sed -i ‘/^[mysqld]/askip-grant-tables‘ /etc/my.cnf ##變量為空就執行插入
else
sed -i ‘s/#skip-grant-tables/skip-grant-tables/g‘ /etc/my.cnf ##變量不為空就加註釋
fi
sed -i ‘s/validate_password/#validate_password/g‘ /etc/my.cnf
systemctl stop mysqld
systemctl start mysqld
expect << EOF
spawn mysql
expect "mysql>" {send "update mysql.user set authentication_string=password(\"123456\") where user=\"root\";\n"}
expect "mysql>" {send "flush privileges;\n"}
expect "mysql>" {send "quit\n"}
expect "mysql>" {send "quit\n"}
EOF
sed -i ‘s/skip-grant-tables/#skip-grant-tables/g‘ /etc/my.cnf
sed -i ‘s/#validate_password/validate_password/g‘ /etc/my.cnf
systemctl stop mysqld
systemctl start mysqld

mysql數據庫密碼忘記恢復腳本