如题,脚本如下:
#!/bin/bash confile="/etc/my.cnf" n_passwd="new password" mysql_bin="/etc/init.d/mysqld" client_bin="/usr/bin/mysql" # Backup Config File cp ${confile} ${confile}_bak # skip grant sed -i '/\[mysqld\]/ a skip-grant-tables' $confile # restart mysqld ${mysql_bin} restart if [ $? -ne 0 ]; then exit -1 fi # connect mysql ${client_bin} <<EOF USE mysql; UPDATE user SET Password = password ( '${n_passwd}' ) WHERE User = 'root' ; flush privileges; exit EOF # restart sed -i '/skip-grant-tables/d' $confile ${mysql_bin} restart diff ${confile} ${confile}_bak >> /dev/null if [ -$? -eq 0 ]; then rm -f ${confile}_bak else echo "Warning: Mysql Config File ${confile} has been changed." echo "Keep Backup as ${confile}_bak." fi