Monday 25 July 2016

Mysql root password reset

I have been seeing this many times over my career, the need to reset mysql root password.

This could be done in 2 ways.

1- you know the root password and want to reset it:

This is a simple case, we need not bring down the DB, this can be done from any terminal that can login as root to mysql, usually the local server running mysqld.
to do this do:

login to mysql client with the password we have:

# mysql -u root -p

mysql> ALTER USER 'root'@'localhost' IDENTIFIED BY 'MyNewPass';

or 

mysql> UPDATE mysql.user
    SET authentication_string = PASSWORD('MyNewPass'), password_expired = 'N'
    WHERE User = 'root' AND Host = 'localhost';
FLUSH PRIVILEGES;




2- you lost the root password and want to reset it:
This would need to use the generic way to do the reset:
To do it run:

stop mysql services

# systemctl stop mysql.service

then bring up mysql with --skip-grant-tables & --skip-networking this is to prevent external users to connect to our server while all privileges are granted to all users.

# mysqld --skip-grant-tables --skip-networking --user=mysql --pid=/var/run/mysqld/mysqld.pid

the login as root:

# mysql -u root

mysql>FLUSH PRIVILAGES;
mysql>ALTER USER 'root'@'localhost' IDENTIFIED BY 'MyNewPass';

Running "FLUSH PRIVILAGES" is to reload the whole permission system in mysql so that we can change the root password.
Once this is done, we need to kill the skip-grant-tables instance and then bring up mysql normally.

The MySQL documentation describes this quite well at http://dev.mysql.com/doc/refman/5.7/en/resetting-permissions.html


No comments:

Post a Comment