How Reset root password in MySQL and MariaDB
If you assigned a root password in MySQL or MariaDB previously but have forgotten it, you can assign a new password. The following sections provide generic instructions for that apply to any system. On any platform, you can reset the password using the
mysql client (but this approach is less secure):
- Stop the MySQL or MariaDB server
~]$ /etc/init.d/mysql stop [ ok ] Stopping mysql (via systemctl): mysql.service.
- Then restart it with the
--skip-grant-tablesoption. This enables anyone to connect without a password and with all privileges, and disables account-management statements such as
SET PASSWORD. Because this is insecure, you might want to use
--skip-grant-tablesin conjunction with
--skip-networkingto prevent remote clients from connecting.
~]$ mysqld --skip-grant-tables --skip-networking 2018-02-13 15:10:54 140002431263296 [Note] mysqld (mysqld 10.1.26-MariaDB-0+deb9u1) starting as process 6793 ...
- Connect to the MySQL or MariaDB server using the
mysqlclient. No password is necessary because the server was started with
~]$ mysql Welcome to the MariaDB monitor. Commands end with ; or \g. Your MariaDB connection id is 7 Server version: 10.1.26-MariaDB-0+deb9u1 Debian 9.1 Copyright (c) 2000, 2017, Oracle, MariaDB Corporation Ab and others. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
- In the mysql client, tell the server to reload the grant tables so that account-management statements work:
mysql> FLUSH PRIVILEGES;
- Then change the
'root'@'localhost'account password. Replace the password with the password that you want to use. To change the password for a root account with a different host name part, modify the instructions to use that host name.
MySQL 5.7.6 and later:
mysql> ALTER USER 'root'@'localhost' IDENTIFIED BY 'MyNewPass';
MySQL 5.7.5 and earlier:
mysql> SET PASSWORD FOR 'root'@'localhost' = PASSWORD('MyNewPass');
- You should now be able to connect to the MySQL server as root using the new password. Stop the server and restart it normally (without the
~]$ /etc/init.d/mysql start [ ok ] Starting mysql (via systemctl): mysql.service.
ALTER USER statement fails to reset the password, try repeating the procedure using the following statements to modify the user table directly:
UPDATE mysql.user SET authentication_string = PASSWORD('MyNewPass') WHERE User = 'root' AND Host = 'localhost'; FLUSH PRIVILEGES;