DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports
Events Video Library
Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
View Events Video Library
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Integrating PostgreSQL Databases with ANF: Join this workshop to learn how to create a PostgreSQL server using Instaclustr’s managed service

Mobile Database Essentials: Assess data needs, storage requirements, and more when leveraging databases for cloud and edge applications.

Monitoring and Observability for LLMs: Datadog and Google Cloud discuss how to achieve optimal AI model performance.

Automated Testing: The latest on architecture, TDD, and the benefits of AI and low-code tools.

Related

  • MySQL Multi-Source Replication
  • An In-Depth Look at Oracle MySQL HeatWave
  • SQL Query Performance Tuning in MySQL
  • Kubernetes Evolution: Transitioning from etcd to Distributed SQL

Trending

  • Best Practices for Writing Clean Java Code
  • Spring WebFlux Retries
  • A Guide to Data-Driven Design and Architecture
  • Exploring Edge Computing: Delving Into Amazon and Facebook Use Cases
  1. DZone
  2. Data Engineering
  3. Databases
  4. Recover MySQL Root Password without Restarting MySQL (No Downtime!)

Recover MySQL Root Password without Restarting MySQL (No Downtime!)

Peter Zaitsev user avatar by
Peter Zaitsev
·
Dec. 19, 14 · Interview
Like (0)
Save
Tweet
Share
3.77K Views

Join the DZone community and get the full member experience.

Join For Free

Originally Written by Daniel Guzmán Burgos

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!

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

  1. 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:

    [root@machina dbdata]# mkdir datadir
    [root@machina dbdata]# chown -R mysql:mysql datadir/
    [root@machina dbdata]# mysql_install_db --datadir=/dbdata/datadir/ --user=mysql
    Installing MySQL system tables...OK
    Filling help tables...OK
  2. 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:

    [root@machina datadir]# /usr/sbin/mysqld --basedir=/usr --datadir=/dbdata/datadir --plugin-dir=/usr/lib/mysql/plugin --skip-innodb --default-storage-engine=myisam --socket=/var/run/mysqld/mysql2.sock --port=3307 --user=mysql --log-error=/dblogs/log/error2.log --pid-file=/dbdata/data/mysql.pid &
  3. 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:

    [root@machina ~]# cp /dbdata/data/mysql/user.* /dbdata/datadir/mysql/cp: overwrite `/dbdata/datadir/mysql/user.frm'? y
    cp: overwrite `/dbdata/datadir/mysql/user.MYD'? y
    cp: overwrite `/dbdata/datadir/mysql/user.MYI'? y
    [root@machina datadir]# mysql --socket=/var/run/mysqld/mysql2.sock -p
    Enter password:
    Welcome to the MySQL monitor.  Commands end with ; or g.
  4. Execute a “flush tables” command, so the user table will be “reopened” and you can see the data and verify:

    mysql2> flush tables;
    mysql2> select user, host, password from user where user like 'root';
    +------+--------------------------------------+------------------------------------------+
    | user | host                                 | password                                 |
    +------+--------------------------------------+------------------------------------------+
    | root | localhost                            | 696D727429CC43695423FA5F2F0155D92A0AAC08 |
    | root | 127.0.0.1                            | 696D727429CC43695423FA5F2F0155D92A0AAC08 |
    | root | %                                    | 696D727429CC43695423FA5F2F0155D92A0AAC08 |
    +------+--------------------------------------+------------------------------------------+
    3 rows in set (0.00 sec)
  5. Now, update the password field with the desired value:

    mysql2> update mysql.user set password='*696D727429CC43695423FA5F2F0155D92A0AAC08' where user like 'root';
    Query OK, 3 rows affected (0.00 sec)
    Rows matched: 3  Changed: 3  Warnings: 0
  6. Verify again:

    mysql2> select user, host, password from user where user like 'root';
    +------+--------------------------------------+-------------------------------------------+
    | user | host                                 | password                                  |
    +------+--------------------------------------+-------------------------------------------+
    | root | localhost                            | *696D727429CC43695423FA5F2F0155D92A0AAC08 |
    | root | 127.0.0.1                            | *696D727429CC43695423FA5F2F0155D92A0AAC08 |
    | root | %                                    | *696D727429CC43695423FA5F2F0155D92A0AAC08 |
    +------+--------------------------------------+-------------------------------------------+
    3 rows in set (0.00 sec)
  7. Flush privileges and verify that the new password is correct, by logging in again:

    mysql2> flush privileges;
    Query OK, 0 rows affected (0.00 sec)
  8. 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:

    [root@machina ~]# cd /dbdata/datadir/mysql/
    [root@machina mysql]# cp user.* /dbdata/data/mysql/; chown mysql:mysql /dbdata/data/mysql/user.*; chmod 660 /dbdata/data/mysql/user.*
    cp: overwrite `/dbdata/data/mysql/user.frm'? y
    cp: overwrite `/dbdata/data/mysql/user.MYD'? y
    cp: overwrite `/dbdata/data/mysql/user.MYI'? y
  9. 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!:

    [root@machina datadir]# mysqladmin --socket=/var/run/mysqld/mysql2.sock -p shutdown
    Enter password:
    141120 06:59:14 mysqld_safe mysqld from pid file /dbdata/data/mysql.pid ended
  10. 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:

    [root@machina datadir]# kill -1 $(/sbin/pidof mysqld)
  11. Finally, login into MySQL as root!:

    [root@machina datadir]# mysql -p
    Enter password:
    Welcome to the MySQL monitor.  Commands end with ; or g.
    Your MySQL connection id is 101208
    mysql1> select user, host, password from mysql.user where user like 'root';
    +------+--------------------------------------+-------------------------------------------+
    | user | host                                 | password                                  |
    +------+--------------------------------------+-------------------------------------------+
    | root | localhost                            | *696D727429CC43695423FA5F2F0155D92A0AAC08 |
    | root | 127.0.0.1                            | *696D727429CC43695423FA5F2F0155D92A0AAC08 |
    | root | %                                    | *696D727429CC43695423FA5F2F0155D92A0AAC08 |
    +------+--------------------------------------+-------------------------------------------+
    3 rows in set (0.00 sec)
    You can see your schemas? of course you can! your databases are okay!

    mysql1> show databases;
    +--------------------+
    | Database           |
    +--------------------+
    | information_schema |
    | mysql              |
    | percona            |
    | testing            |
    +--------------------+
    4 rows in set (0.03 sec)

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!

 

MySQL

Published at DZone with permission of Peter Zaitsev, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • MySQL Multi-Source Replication
  • An In-Depth Look at Oracle MySQL HeatWave
  • SQL Query Performance Tuning in MySQL
  • Kubernetes Evolution: Transitioning from etcd to Distributed SQL

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends: