How to reset the root mysql password if you forget it

I’ve been setting up a LEMP server, Nginx Mysql PHP, on Ubuntu and lost my root mysql password. Here’s what I did to reset the password in Terminal.

After logging into my server via SSH on Terminal I ran the following lines in Terminal.

# /etc/init.d/mysql stop
# mysqld_safe --skip-grant-tables &
$ mysql -u root

mysql> use mysql;
mysql> update user set password=PASSWORD("NEW-ROOT-PASSWORD") where User='root';
mysql> flush privileges;
mysql> quit

# /etc/init.d/mysql stop
# /etc/init.d/mysql start
$ mysql -u root -p

I found this solution in an answer by Benjamin Manns to a StackOverflow question here.

Leave a Reply

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