MySQL is one of the most popular database management systems for web applications and eCommerce stores. It is the backbone of countless corporate applications that support business operations and capabilities, such as customer relationship platforms and content management systems.
However, with great power comes great responsibility, and securing MySQL databases is a top priority for business security teams.
One crucial aspect of database security is password management, particularly the root password, which provides complete access to a MySQL server. Whether you’ve forgotten your root password, suspect a security breach, or simply want to change it for enhanced protection, resetting and changing the root password may seem daunting.
This blog will discuss the step-by-step process of resetting the MySQL root password.
So, whether you’re a seasoned database administrator looking to refresh your knowledge or a newcomer to MySQL security, this blog will help you reset and change MySQL root password in Linux.
Let’s start with the prerequisites of the operation.
Prerequisites
Before resetting and changing MySQL root password in Linux, make sure you have:
- Access to an active MySQL database
- Permission to connect to a Linux server running MySQL
- Administrator-level privileges on the machine hosting the MySQL database
- A text editor, Vim, vi, nano (vi is available by default on Linux)
- Access to a command-line interface or terminal
Do you know that you need the MySQL root password to add new tables to the MySQL database?
Reset & Change MySQL Root Password in Linux
Now that you have fulfilled the prerequisites, let’s go into the details of resetting and changing the MySQL root password.
Step #1: Stop the MySQL Server
Before resetting the MySQL root password, you should halt the MySQL server to prevent unauthorized server access during the password reset.
Use the following command in the terminal to stop the MySQL server:
#service mysql stop
Step #2: Restart MySQL in Safe Mode
Now restart the MySQL service in safe mode, where you don’t need to provide a password.
In the terminal, enter the following command to start MySQL in safe mode as a background process:
#mysqld_safe --skip-grant-tables &
IMPORTANT: We strongly recommend using the –skip-grant-tables option with caution. It is a MySQL server startup option that turns off user authentication and potentially grants unrestricted access to the database. This option is often used for password recovery or fixing authentication issues.
Step #3: Connect to the MySQL Server
Now, connect to MySQL without specifying a password by entering the following command:
# mysql -u root
Since the server is in safe mode, you won’t see the password prompt. Instead, you will see the standard mysql> prompt of the MYSQL server CLI.
Step #4: Change the MySQL Root Password
Now, you can change the root password. For this, run the following SQL query, replacing new_password with your new password:
mysql> ALTER USER 'root'@'localhost' IDENTIFIED BY 'new_password';
IMPORTANT: We strongly recommend following the best practices for strong passwords. At the minimum, create a 20-character string containing a mix of uppercase characters, lowercase characters, digits, and special characters. Avoid using dictionary words and personal or corporate information.
Step #5: Reload Privileges
Note that while you have changed the MySQL root password, it is still not in effect. You need to refresh the privileges with the flush privileges command:
mysql> flush privileges;
Step #6: Exit and Restart the MySQL Service
Now, exit the current session with the exit command:
mysql> exit
Next, restart the MySQL service with the following command:
#systemctl start mysql
For MariaDB, replace “mysql” with “mariadb.”
Step #7: Verify the New Password
To confirm that the password change was successful, try logging in with the new password:
#mysql -u root -p
You will be prompted to enter the new password.
You’ve successfully changed the MySQL root user password in Linux. Security experts recommend frequently changing passwords for critical databases to thwart security incidents like insider threats.
Now that you know how to change the MySQL root password, read our guide on how to create MySQL users.
Conclusion
This guide walks you through how to reset and change the MySQL root password on Linux. While this is not a particularly complex task, it is an essential activity that should be a part of your monthly or quarterly security checklist.
At RedSwitches, we take security very seriously. Our support teams proactively monitor the infrastructure and help our users deal with security incidents.
If you’re looking for a robust and secure server for your MySQL databases, we offer the best dedicated server pricing and deliver instant dedicated servers, usually on the same day the order gets approved. Whether you need a dedicated server, a traffic-friendly 10Gbps dedicated server, or a powerful bare metal server, we are your trusted hosting partner.
FAQs
Q. How do I reset the MySQL root password in Linux if I’ve forgotten the current password?
If you’ve forgotten the current MySQL root password, you can reset it by stopping the MySQL service using sudo systemctl, starting it in recovery mode, and then using the update user command to change the password.
Q. Why is changing the database root password important for security reasons?
Regularly changing the database root password is essential for protecting your database from unauthorized access and potential security threats.
Q. What is the role of the mysqld process in resetting the MySQL root password?
The mysqld process is the MySQL database server process. You need to stop this process using sudo systemctl to ensure that changes to the root password are applied correctly.
Q. How can I grant or restrict root access to the MySQL database server after resetting the root password?
After resetting the MySQL root password, you can control root access by modifying the user’s privileges using SQL commands. This allows you to grant or restrict access to specific tasks or databases.
Q. What should I consider before updating the database root password in a production environment?
Back up your database before planning to update the database root password. Before the move, remember to notify relevant stakeholders and schedule the change during a maintenance window to minimize disruptions.