How to Reset Root Password for MySQL or MariaDB

Resetting the root password is straightforward if you have access to the server’s command line. This comprehensive guide will walk you through the steps to reset the root password efficiently, ensuring minimal downtime and maintaining database security.

Table of Contents

  1. Understanding the Importance of the Root User
  2. Prerequisites
  3. Step-by-Step Guide to Resetting the Root Password
  4. Alternative Method: Using the skip-grant-tables Option
  5. Efficient Tips for Resetting the Root Password
  6. Security Considerations
  7. Conclusion

Understanding the Importance of the Root User

The root user in MySQL or MariaDB holds all administrative privileges, allowing full control over the database server. Losing access to this account can hinder database management tasks such as creating new users, granting permissions, or performing backups.


Prerequisites

Before proceeding, ensure you have:

  • Server Access: Root or sudo access to the server where MySQL/MariaDB is installed.
  • Terminal Access: Ability to execute commands in the terminal or command prompt.
  • Service Management Commands: Knowledge of starting and stopping services on your operating system.

Step-by-Step Guide to Resetting the Root Password

Step 1: Stop the MySQL/MariaDB Service

First, you’ll need to stop the running MySQL or MariaDB service.

For Ubuntu/Debian:

sudo systemctl stop mysql

For CentOS/RHEL:

sudo systemctl stop mariadb

Step 2: Start MySQL/MariaDB in Safe Mode

Start the database server without loading the grant tables or enabling networking. This allows you to connect without a password.

sudo mysqld_safe --skip-grant-tables --skip-networking &

Note: The & runs the command in the background, returning control to your terminal.

Step 3: Connect to the MySQL/MariaDB Server

Log in to the MySQL/MariaDB server as the root user.

mysql -u root

Step 4: Reset the Root Password

Once logged in, switch to the mysql database and update the root user’s password.

For MySQL 5.7.6 and later, and MariaDB 10.1.20 and later:

USE mysql;
ALTER USER 'root'@'localhost' IDENTIFIED BY 'NewPassword';

For earlier versions:

USE mysql;
UPDATE user SET authentication_string=PASSWORD('NewPassword') WHERE User='root';

Replace 'NewPassword' with your desired strong password.

Step 5: Flush Privileges and Exit

Apply the changes and exit the MySQL/MariaDB shell.

FLUSH PRIVILEGES;
EXIT;

Step 6: Stop Safe Mode and Restart the Service

First, you’ll need to stop the MySQL/MariaDB process running in safe mode.

Find the process ID (PID):

ps aux | grep mysqld

Kill the process using the PID:

sudo kill -9 PID

Now, start the MySQL/MariaDB service normally.

For Ubuntu/Debian:

sudo systemctl start mysql

For CentOS/RHEL:

sudo systemctl start mariadb

Alternative Method: Using the skip-grant-tables Option

If the above method doesn’t work, you can use the --skip-grant-tables option directly.

  1. Stop the MySQL/MariaDB service.
  2. Start the server with:
    sudo mysqld --skip-grant-tables &
        
  3. Follow Steps 3 to 6 as above.

Warning: Running the server with --skip-grant-tables can be insecure. Ensure the server is not accessible over the network during this time.


Efficient Tips for Resetting the Root Password

  • Use Scripts: Automate the process with a script if you manage multiple servers.
  • Backup Configuration: Always back up your configuration files before making changes.
  • Strong Passwords: Use complex passwords to enhance security.
  • Limit Downtime: Schedule the reset during maintenance windows to minimize impact.

Security Considerations

  • Limit Access: Ensure only authorized personnel have root or sudo access.
  • Firewall Rules: During the reset, make sure your firewall rules prevent external access.
  • Update Credentials: After resetting, update any applications or scripts that use the root credentials.

Conclusion

Resetting the root password for MySQL or MariaDB is a critical skill for database administrators. By following the steps outlined in this guide, you can regain access efficiently while maintaining the security and integrity of your database.

Remember: Always safeguard your root credentials and follow best security practices.

Stay in the Loop

Get the daily email from ScoHostings that makes reading the news actually enjoyable. Join our mailing list to stay in the loop to stay informed, for free.

Latest stories

You might also like...