We may need to change our MySQL passwords for a number of reasons. You might have lost or forgotten the passwords, or have experienced a security concern that prompts you to change it. In either case, this article includes step-by-step instructions on how to reset your MySQL password on a Linux system.
In order to complete this tutorial, you must have the following:
To begin, we must first stop the MySQL service. Do this by running the following commands in the terminal:
# systemctl stop mysqld.service
# /etc/init.d/mysqld stop
We now have two options on how to proceed.
To do this, open your code editor and generate a file that contains some SQL code, which will be used to reset the password. Create a new file and add the following code inside it:
ALTER USER 'root'@'localhost' IDENTIFIED BY 'new_password';
Replace the root username
with the username for which you wish to reset the password, as well as the new password
string with the desired password. Save the file in /home/user/init-file.txt
. Then run the following command:
# mysqld --user=mysql --init-file=/home/user/init-file.txt --console
Running the above command will start the MySQL service and execute the init-file that we created. This will change the user's (root's) password to the new password we specified in the file.
Note: If you use this approach, you should skip the next step and log in directly with the new MySQL password.
The second method is to start the MySQL service with the --skip-grant-tables
option. If the --skip-grant-tables
command is used to start the server, the option —skip-networking is enabled by default, prohibiting remote connections.
You can use this command to reset your password. Run the command code block below on the terminal.
# mysqld --skip-grant-tables --user=mysql &
# mysql
# FLUSH PRIVILEGES;
To update the password, type the following command into the command prompt. Make sure "new password" is replaced with the exact password you want to use.
# ALTER USER 'root'@'localhost' IDENTIFIED BY 'new password';
The above command will successfully reset the MySQL password.
Now that you've successfully updated the MySQL password, you'll need to restart the MySQL service with the following command:
# systemctl stop mysqld.service
# systemctl restart mysqld.service
# /etc/init.d/mysqld stop
# /etc/init.d/mysqld restart
Run the following code in the terminal to login into MySQL using the new password:
# mysql -u root -p
Then enter the new password to log in to the MySQL service. You can then interact with your data using a SQL client like Arctype. In order to update the new MySQL password with your Arctype workspace, you can check the Arctype documentation for more details.
We learned how to change MySQL passwords on Linux by following these step-by-step instructions. If you follow those steps, you should be able to change your MySQL password easily. If you’re interested in learning more about MySQL, you can visit the Arctype documentation and Arctype blog, which contain additional details.
You can also join the Arctype Discord to get help and inspiration from hundreds of SQL experts from around the world.