没事干,写了个自己记录以后直接复制来用,适合centos6.x ,如果是其它版本只需要把重启mysql的命令重写下就行了。具体脚本如下
#!/bin/bash #author merci #Brute force password for mysql and centos6.x CONF="/etc/my.cnf" NEWPASS="10idccom" sed -i "/skip-grant-tables/d" $CONF >/dev/null 2>&1 ROWS=`cat $CONF | grep -n "[mysql]" | head -1 | awk -F ':' '{print $1}'` INSERTROWS=`expr $ROWS + 1` sed -i "${INSERTROWS}i\skip-grant-tables" $CONF /etc/init.d/mysqld restart >/dev/null mysql -u root -p\n << EOF use mysql; update user set password=password("$NEWPASS") where user="root"; flush privileges; exit EOF sed -i 's/skip-grant-tables/#skip-grant-tables/g' $CONF /etc/init.d/mysqld restart >/dev/null exit 1