Reset Mysql Root Password Script

Lost (forgotten) your mysql root / admin password? Not to worry, it happens to the best of us.

This simple BASH password reset script will enable you to reset any mysql user password without knowing the current password

I would not recommend running it on a live production system as it requires shutting down the mysql server.

Heres the script:, it needs to be run as root.

# Kill any mysql processes currently running
echo 'Shutting down any mysql processes...'
killall -vw mysqld
 
# Start mysql without grant tables
mysqld_safe --skip-grant-tables >res 2>&1 &
 
echo 'Resetting password...'
 
#Sleep for 5 while the new mysql process loads (if get a connection error you might need to increase this.
sleep 5
 
#Update user with new password
mysql mysql -e "UPDATE user SET Password=PASSWORD('$2') WHERE User='$1';FLUSH PRIVILEGES;"
 
echo 'Cleaning up..'
 
#Kill the insecure mysql process
killall -v mysqld

First variable is the user you want to reset, second variable is the password.

sh mysqlreset root newpassword

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="" highlight="">

Go back to top