How to reset MySQL root password on your Linux server

The following guide will provide you with simple to follow steps on how to reset your administrative root password on Linux.

Stop MySQL

First, stop MySQL server:

# service mysql stop
 * Stopping MySQL database server mysqld              [ OK ]

Start MySQL server>

Start your MySQL server, but skip all grand privileges and networking:

# mkdir -p /var/run/mysqld
# chown mysql:mysql /var/run/mysqld
# /usr/sbin/mysqld --skip-grant-tables --skip-networking &
[1] 8142

Login to MySQL

Next, login to MySQL as root without password:

# mysql -u root
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.7.12-0ubuntu1 (Ubuntu)

Copyright (c) 2000, 2016, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

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

mysql>

Flush Privileges

mysql> FLUSH PRIVILEGES;
Query OK, 0 rows affected, 6 warnings (0.02 sec)

mysql>

Set new password

Next, set a new root password to eg. linuxconfig and quit:

mysql> SET PASSWORD FOR root@'localhost' = PASSWORD('linuxconfig');
Query OK, 0 rows affected, 1 warning (0.01 sec)

mysql> quit
Bye

Restart MySQL database

Gracefully stop current mysql process:

# kill %1
[1]+  Done                    /usr/sbin/mysqld --skip-grant-tables --skip-networking

Next, we need to start MySQL database:

# service mysql start

Log in with new password

# mysql -u root --password=linuxconfig
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 7
Server version: 5.7.12-0ubuntu1 (Ubuntu)

Copyright (c) 2000, 2016, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

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

mysql>


Comments and Discussions
Linux Forum