2014-12-10

Disclaimer: Do this at your own risk! It doesn’t apply if you’re using Pluggable authentication and certainly won’t be usable if/when MySQL system tables are stored on InnoDB



Recover your root password with care!

What is the situation?

The situation is the classic “need to recover MySQL root password” but you cannot restart MySQL (because it is the master production server, or any other reason), which makes the –skip-grant-tables solution as a no-no possibility.

What can I do?

There is a workaround, which is the following:

Launch another instance of mysqld, a small one (without innodb).

Copy your user.[frm|MYD|MYI] files from the original datadir to the datadir of the new instance.

Modify them and then copy them back to the original location.

That simple? No, but close. Here is the step by step:

Step by step recovery

Create a new datadir and run mysql_install_db for the new datadir. This one will be removed at the end. Don’t forget to change ownership to mysql user and group:

Launch the new instance. Be careful with the datadir path, the socket file and the port number. Also, disable InnoDB, you won’t need it, just add –skip-innodb AND –default-storage-engine=myisam:

Copy the user.* files from the original mysql instance (the ones that you need to modify) to the new instance’s datadir and login to this instance of mysql:

Execute a “flush tables” command, so the user table will be “reopened” and you can see the data and verify:

Now, update the password field with the desired value:

Verify again:

Flush privileges and verify that the new password is correct, by logging in again:

Now that we have made the changes, we can move back the user.* files to the original location, being extremely careful with owner and privileges:

At this moment, you can shutdown the new mysql instance since is no longer needed. Be very very careful so you don’t end up shutting down your original mysqld!:

Now, the last step is to execute a “FLUSH PRIVILEGES” in the original mysqld. Since we cannot yet access it, we need to send a SIGHUP signal to mysqld. MySQL responds to this signal by reloading the grant tables and flushing tables, logs, the thread cache, and the host cache, so choose wisely the moment of the day when you want to send the SIGHUP since the performance might be degraded (look at “flush tables” ).The way to send SIGHUP is to execute “kill” command with the -1 flag:

Finally, login into MySQL as root!:

You can see your schemas? of course you can! your databases are okay!

We’ve successfully recovered the MySQL root password without the need to restart MySQL and thus avoid downtime.

I hope you never face this situation, but in case you do, there’s a workaround to recover your access! Is there another way to perform this?

Share it with the world!

The post Recover MySQL root password without restarting MySQL (no downtime!) appeared first on MySQL Performance Blog.

Show more