MySQL is a popular open-source database management system used by many developers and organizations to store and manage data. However, if you have forgotten the password for your MySQL database on a Linux system, it can be quite frustrating to regain access. In this article, we will guide you through the process of resetting a forgotten MySQL password on Linux.
Method 1: Reset Password using MySQL Safe Mode
-
First, stop the MySQL service by running the following command in the terminal:
sudo systemctl stop mysqld
-
Start MySQL in safe mode by running the following command:
sudo mysqld_safe --skip-grant-tables&
-
Connect to MySQL using the root user without a password by running the following command:
mysql -u root
-
Once you are logged into MySQL, switch to the MySQL database by running the command:
use mysql;
- Change the root password with the following command:
update user set authentication_string=password('new_password') where user='root';
Replace new_password
with your desired new password.
-
Flush the privileges to ensure the changes take effect:
flush privileges;
-
Exit MySQL by typing:
exit;
-
Restart the MySQL service by running the command:
sudo systemctl start mysqld
- You should now be able to log in to MySQL with the new password you set.
Method 2: Reset Password by Editing MySQL Configuration File
-
Stop the MySQL service using the following command:
sudo systemctl stop mysqld
-
Edit the MySQL configuration file using a text editor like nano or vim:
sudo nano /etc/mysql/my.cnf
-
Add the following line under the
[mysqld]
section:skip-grant-tables
-
Save and exit the editor.
-
Restart the MySQL service:
sudo systemctl start mysqld
-
Log in to MySQL as root without a password:
mysql -u root
- Change the root password with the following command:
use mysql; update user set authentication_string=password('new_password') where user='root'; flush privileges; exit;
Replace new_password
with your desired new password.
-
Remove the
skip-grant-tables
line from the MySQL configuration file. -
Restart the MySQL service:
sudo systemctl restart mysqld
- You should now be able to log in to MySQL with the new password.
By following either of these methods, you should be able to reset a forgotten MySQL password on a Linux system. Remember to choose a strong and secure password to protect your database from unauthorized access.