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

经常忘记测试机上mysql的root密码,学着写个脚本处理下

2013-10-09 15:54 357 查看
如题,脚本如下:
#!/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


本文出自 “攀爬者的博客” 博客,请务必保留此出处http://surveying.blog.51cto.com/6141135/1306295
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: