How to Reset or Change MySQL Root Password on Linux or Windows


MySQL is a database management system operated under the Open Source software model. It has become a very popular software package for web application software. Like many web services, MySQL has an administrator-level or root password. The root password allows a user to perform all top-level functions in the database.

If you’ve never set a root password on your MySQL database, you should be able to connect to it. However, this is not a good idea as it means anyone can access your database.

If your database already has a root password, but you lost track of it, this guide will help you reset a Mysql Root password on Linux and Windows.

guide how to reset mysql password on linux or windows


  • An existing MySQL database
  • Access to a Linux or Windows server running MySQL
  • Administrator privileges on the computer that hosts the MySQL database
  • A text editor. Notepad is included by default in Window. Vim is installed by default in Linux.
  • Access to a command-line interface (or terminal)

How to Change MySQL User Root Password in Linux

Step 1: Log in as the MySQL User

When you boot into your Linux installation, make sure you’re logged in as the same user that normally runs MySQL.

Although you can log in as root, once you start the MySQL server, make sure you start it with the --user=mysqloption.

Otherwise, the system may create files owned by the root user, which can cause problems.

Step 2: Find the .pid File for the MySQL Service

The location depends on your distribution of Linux, as well as a few other variables.

Many systems store them in /var/lib/mysql/, /var/run/mysqld/, or /usr/local/mysql/data/. The filename usually starts with mysqld (or your system’s hostname) and uses the .pid extension.

Step 3: Kill the MySQLD Process

To kill the process, open a command line, and run the following:

kill `cat /mysql-data-directory/`

Use the filename you found in the previous step in instead of mysql-data-directory/

Also, ensure to specify the whole path to the file. Also, use the back-tick key (usually above the tab key) instead of a single-quote mark.

Step 4: Create the Password File

1. Open your favorite text editor with the command:

sudo vim

2. Next, add the following line in it:

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

create a password file for mysql

Bear in mind to include the single-quote marks and the semicolon. Switch out NewPassword for the password you want to use. Finally, make sure to use a strong secure password, like these examples.

This command will work for the machine you’re currently working on. If you’re connecting to a different system, replace localhost with the correct hostname.

3. Save the file to home/me/mysql-init.

Substitute your user account for /home/me, and save the file to a password-protected user account.

Step 5: Restart the MySQL Server and Apply the New Password

In the terminal window, enter:

mysqld --init-file=/home/me/mysql-init &

This will launch MySQL, and apply the text-file password change. Depending on how you start your server, you may need to add other options (such as –defaults-file.)

Step 6: Cleaning Up

Lastly, log into your MySQL server using the root account, and verify the new password works. Then, delete the file you created in Step 4.

How to Reset MySQL Root Password in Windows

Step 1: Stop the MySQL server

1. Begin by checking if you’re logged in as an administrator.

2. Press Win+R (hold the Windows/Super key, and press “r”.) Once the “Run” box appears type:


3. Click OK.

4. Scroll down the list of services to find the MySQL service. Right-click that entry, then left-click Stop.

find-windows services using run

Step 2: Launch a Text Editor

Click on the menu and search for Notepad.

Alternatively, you can use the path: menu > Windows Accessories > Notepad.

Step 3: Create a New Text File with the Password Command

1. Enter the following line into the text editor:

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

Make sure you keep the quote marks and semicolon. Replace NewPassword with the password of your choice.

create a file with password command windows

2. Use the File > Save As menu to save the file to the root of your hard drive (C: ). Choose a filename that makes sense, such as mysql-init.txt.

Consequently, the localhost command will make the password change on your local system. If you’re trying to change the password on a system over the network, substitute the hostname for localhost.

Step 4: Open a Command Prompt

1. Press Ctrl+Shift+Esc.

2. Then, click on the File menu > Run new task.

3. Type cmd.exe, and check the box to run as administrator.

4. Click OK.

Step 5: Restart the MySQL Server with Your New Config File

1. Navigate to the MySQL directory using the command prompt:

cd "C:Program FilesMySQLMySQL Server 8.0bin"

Accordingly, the command line will show that you’re working in this folder.

2. Enter the following:

mysqld --init-file=C:mysql-init.txt

Note that there are two slashes after the C: prompt.
Also, if you chose a different name in Step 2, use that name after the double slash.

Step 6: Clean up

Now, you can log into your MySQL server as root using the new password.

Double-check to make sure it works. If you have unique configuration options (such as launching MySQL with a –defaults-file option), go ahead and do so.

Once MySQL launches, and you’ve confirmed the password change, delete the C:myswl-init.txt file.


After reading this guide, you should be ready to reset the root password on MySQL in Linux and Windows. Granted, it is not too challenging, yet it is a secure way to change a sensitive password.

Dejan Tucakov
Dejan is the Head of Content at phoenixNAP with over 7 years of experience in Web publishing and technical writing. Prior to joining PNAP, he was Chief Editor of several websites striving to advocate for emerging technologies. He is dedicated to simplifying complex notions and providing meaningful insight into data center and cloud technology.
Next you should read
How To Remove Or Delete A MySQL User Account
December 1, 2019

This article covers the basics of using the DROP USER statement used to delete MySQL user account. Follow the step-by-step instructions and remove one or multiple accounts in a matter of seconds.
Read more
Install And Get Started With MySQL Workbench On Ubuntu 18.04
November 19, 2019

Workbench is a visual tool for managing MySQL databases . Its graphical interface allows administrators and database architects to model data, build and run SQL queries, and maintain a database.
Read more
How To Rename A MySQL Database
May 6, 2019

As an administrator, you may need to rename a database. However, for security, the command to rename a database directly was removed in MySQL 5.1.23. This guide will give thr
Read more
How To Import And Export MySQL Databases In Linux
April 25, 2019

MySQL is a versatile application. It can be used for something as simple as a product database, or as complex as a website. This guide will walk you through how to export the database and import it from a dump file in MySQL.
Read more
  • © 2021 Copyright phoenixNAP | Global IT Services. All Rights Reserved.