Reset MySQL Password

Create a script with the following content, and run it as root.  To make it run-able, do:

chmod o+x [scriptFilename]

 

Content of the file:

#!/bin/bash
#
# Script to reset MySQL password in Gentoo
#

echo
echo "Mysql password recovery utility"
echo

# Stop the mysql server nicely
echo "Stopping MySQL daemon..."
/etc/init.d/mysql stop
# Make *sure* the mysql server stops
sleep 2
/etc/init.d/mysql zap > /dev/null
sleep 2
/usr/bin/killall mysqld > /dev/null
sleep 5


# Run mysqld in permissionless mode
echo "Start MySQL daemon in permissionless mode..."
/sbin/start-stop-daemon --start --quiet --exec /usr/bin/mysqld_safe \
        --background -- --skip-grant-tables >/dev/null 2>&1

sleep 1

#Ask user for new password
read -p "Enter your new root password: " PASSWORD
echo "The password you typed was: ${PASSWORD}"

echo "Changing password..."

# Execute queries to change password
mysql -u root mysql -e "UPDATE user SET Password=PASSWORD('${PASSWORD}') WHERE \
                        user='root'; \
                        FLUSH PRIVILEGES;"

# Display results of attempt to password change
if [[ $? -eq 0 ]]
then
        echo " ** SQL root password updated"
else
        echo " ** SQL root password update unsuccesful"
fi

# Restart the mysql server
echo "Restarting MySQL daemon..."
/usr/bin/killall mysqld > /dev/null
/etc/init.d/mysql restart
echo
echo
exit 0

Powered by Linux