How to reset / recover a MySQL root password

Update February 2022

Resetting MySQL password remains a nightmare due to all the differences between MySQL 7 and 8, and MariaDB.

If you’re running the latest MySQL 8, see this article, in summary:

mysql> CREATE USER 'root'@'localhost' IDENTIFIED BY 'PASSWORD';
mysql> GRANT ALL PRIVILEGES ON *.* TO 'root'@'localhost' WITH GRANT OPTION;

If you get an error such as `You are not allowed to create a user with GRANT`

create user 'root'@'localhost' identified by 'password';

IF you get this error `Operation CREATE USER failed for ‘root’@’localhost’`

Perhaps do this:

drop user 'root'@'localhost'

Update December 2021

What if you can’t find mysqld_safe command not found error? Try this:

sudo systemctl set-environment MYSQLD_OPTS="--skip-grant-tables --skip-networking"

[[email protected] ~]$ mysql -u root


mysql> select user(), current_user();
| user() | current_user()                    |
| [email protected]  | skip-grants [email protected] host |
1 row in set (0.00 sec)

mysql> ALTER USER 'root'@'localhost' IDENTIFIED BY 'sup3rPw#'
ERROR 1290 (HY000): The MySQL server is running with the --skip-grant-tables option so it cannot execute this statement
Query OK, 0 rows affected (0.02 sec)

mysql> ALTER USER 'root'@'localhost' IDENTIFIED BY 'sup3rPw#'
Query OK, 0 rows affected (0.08 sec)

mysql> exit
[[email protected] ~]$ systemctl stop mysqld
[[email protected] ~]$ sudo systemctl unset-environment MYSQLD_OPTS
[[email protected] ~]$ systemctl start mysqld


Old Article

The fact is changing MySQL root really depends a lot on the version of MySQL and there is no exact way. That’s why googling it will always bring up new answers.

You could try this:

sudo /etc/init.d/mysql stop

sudo mysqld_safe –skip-grant-tables &

mysql -u root mysql

Now in MySQL:

Try this first:
update mysql.user set password=password('secret') where user='root';

Otherwise this:
SET PASSWORD FOR [email protected]’localhost’ = PASSWORD(‘password’);


sudo /etc/init.d/mysql stop

sudo /etc/init.d/mysql stop

mysql -uroot -ppassword


Be warned, multiple references and not all methods work.

Possible Caveats:

MariaDB [mysql]> SET PASSWORD FOR [email protected]’localhost’ = PASSWORD(‘secret’);
ERROR 1131 (42000): You are using MariaDB as an anonymous user and anonymous users are not allowed to modify user settings

Try this command instead:
update mysql.user set password=password(‘secret’) where user=’root’;

Be careful when testing, you might have to sudo mysql to test! If sudo mysql works but without sudo doesn’t work, you need an additional grant statement:

[email protected]:~/Code/api$ mysql -uroot -ppassword
ERROR 1698 (28000): Access denied for user ‘root’@’localhost’

[email protected]:~/Code/api$ sudo mysql -uroot -ppassword
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 37

Server version: 10.1.38-MariaDB-0ubuntu0.18.04.1 Ubuntu 18.04
Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.

Type ‘help;’ or ‘\h’ for help. Type ‘\c’ to clear the current input statement.

MariaDB [(none)]> grant all privileges on *.* to ‘root’@’localhost’ identified by ‘password’ with grant option;
Query OK, 0 rows affected (0.00 sec)

Another caveat:

190213 12:31:31 mysqld_safe A mysqld process already exists

You tried this already and now there is an open process. Use kill to continue. First

ps -ef | grep mysql

Then kill -9 one by one.


Share this article

Leave a Reply

Your email address will not be published.

Scroll to Top