#!/bin/bash
mysql_password="новый_пароль"
/etc/init.d/mysql stop
echo "UPDATE mysql.user SET Password=PASSWORD('${mysql_password}') WHERE User='root';" > /tmp/mysql-set-pass
echo "FLUSH PRIVILEGES;" >> /tmp/mysql-set-pass
su -c "mysqld_safe --init-file=/tmp/mysql-set-pass &" mysql --shell=/bin/bash
sleep 5
echo "[mysql]
password = ${mysql_password}
[mysqldump]
password = ${mysql_password} " > /root/.my.cnf
/etc/init.d/mysql restart
mysql --password="" -e "SELECT 1;" 2>/dev/null && exit 1
mysql -e "SELECT 1;" 1>/dev/null || exit 1
rm /tmp/mysql-set-pass
Сбрось скриптом на новый пароль.