Line
Skip to main content
Print

How to Change MySQL User Root Password in Linux

Step 1: Log in as MySQL User

Log into your system as the user that you use to start and run the MySQL server. You can also log in as root, but in that case, you must use the –user=mysql -p option when starting the mysqld service later in step 5. Starting the server as root creates root-owned files in the data directory, which can cause permission-related issues for future server startups.

Step 2: Stop the MySQL Server

          Before changing the password, stop the MySQL server if it is active. Run the following command to stop the mysqld process:

The command stops the MySQL server. Alternatively, run the following command to kill all running instances of the MySQL server:

Step 3: Create a Password File

Create a file in which you will specify the new password. Follow the steps below:

  1. Create a new text file using your favorite text editor. In this tutorial, we will use nano. The syntax is:

Replace [username] with your actual username on your system

2. Next, add the following line to the file

  • Replace the NewPassword with the password you want to use. Include the single-quote marks and the semicolon at the end. Make sure to use a strong and secure password.
  • The command works for the machine you’re currently using. Replace localhost with the appropriate hostname if you’re connecting to a different system.

Save the file with Ctrl+O and exit the editor with Ctrl+X. If you are not logged in with the user account that you use for running the server, set the file permissions to allow the MySQL user to read the file.

Step 4 : Apply the Changes

To apply the password change, use the following syntax :

Make sure to replace [username] with your actual username on your system. For example

The command applies the text-file password change. The output shows the process ID (PID) of the service that was started in the background.

Step 5: Restart the Server

Restart the MySQL server after changing the root password to apply the changes. Enter the command below:

Wait until the process completes.

Step 6: Log in and Clean Up

Log into your MySQL server using the root account and verify the new password works.

If everything works, you can delete the file you created in Step 3

Credit : How to Reset/Change MySQL Root Password on Linux or Windows (phoenixnap.com)

Messenger