March 28, 2019

Resetting Mysql 5.7 Root Password

This will be a quick guide on how to reset your mysql root password.
Recently, I found myself in a situation where I had to restore an old jenkins job that was used to measure the rates at which we send SMS at FrontlineSMS and this also involved setting up a mysql db that had become obsolete and was no longer being used.
The problem was that I could not log into the database since nobody could remember the password as it was no longer being used.
These are the steps I used to reset the password.

Stop the the mysql server

#Linux

service mysql stop  or
/etc/init.d/mysqld stop or
service mysqld stop

#MacOs

brew services stop mysql@5.7 or
/usr/local/opt/mysql@5.7/bin/mysql.server stop

Start mysql with –skip-grant tables to skip password step

mysqld_safe --skip-grant-tables > /dev/null 2>&1 &

Login with root user and change password

mysql -u root
use mysql;
update user set password=PASSWORD('NEW-PASSWORD') where User='root';
flush privileges;
exit

Restart mysql and log in with your new password

#Linux

service mysql restart or
/etc/init.d/mysqld restart or
service mysqld restart

#MacOs

brew services restart mysql@5.7 or
/usr/local/opt/mysql@5.7/bin/mysql.server restart

© Brianmituka 2021