Guide: How to Change MySQL Root Password

Guide: How to Change MySQL Root Password

Changing the root password for MySQL is an important security measure to protect your database from unauthorized access. In this guide, we will walk you through the steps to change the MySQL root password.


Before we begin, make sure you have the following:

  • Access to a terminal or command prompt
  • Root access to the MySQL server

Step 1: Connect to MySQL

Open a terminal or command prompt and connect to the MySQL server using the following command:

mysql -u root -p

This will prompt you to enter the current root password. If you have never set a password for the root user, you can skip this step.

Step 2: Change the Root Password

Once you are connected to the MySQL server, run the following SQL command to change the root password:

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

Replace new_password with your desired password. Make sure to choose a strong password that includes a combination of letters, numbers, and special characters.

Step 3: Flush Privileges

After changing the root password, you need to flush the privileges to apply the changes. Run the following command:


This will ensure that the new password takes effect immediately.

Step 4: Test the New Password

To test the new root password, exit the MySQL prompt by typing exit and press Enter. Then, reconnect to the MySQL server using the new password:

mysql -u root -p

If the new password is correct, you will be granted access to the MySQL server.

Commands Examples

Command Description
mysql -u root -p Connect to the MySQL server
ALTER USER 'root'@'localhost' IDENTIFIED BY 'new_password'; Change the root password
FLUSH PRIVILEGES; Flush privileges to apply the changes

Similar Commands

  • SET PASSWORD FOR 'user'@'localhost' = PASSWORD('new_password'); – Change the password for a specific user
  • ALTER USER 'user'@'localhost' IDENTIFIED WITH mysql_native_password BY 'new_password'; – Change the password using the mysql_native_password plugin
  • UPDATE mysql.user SET Password=PASSWORD('new_password') WHERE User='user' AND Host='localhost'; – Update the password directly in the user table

Use Cases

  • Securing your MySQL server
  • Resetting a forgotten root password
  • Changing the password periodically for better security

Ideas for Automation

Automating the process of changing the MySQL root password can save time and effort. Here are some ideas for automation:

  • Create a shell script that prompts for the new password and executes the necessary SQL commands
  • Use a configuration management tool like Ansible or Puppet to manage MySQL passwords across multiple servers
  • Implement a password management system that automatically rotates the root password on a regular basis


Changing the MySQL root password is a crucial step in securing your database. By following the steps outlined in this guide, you can easily change the root password and protect your MySQL server from unauthorized access.

This article incorporates information and material from various online sources. We acknowledge and appreciate the work of all original authors, publishers, and websites. While every effort has been made to appropriately credit the source material, any unintentional oversight or omission does not constitute a copyright infringement. All trademarks, logos, and images mentioned are the property of their respective owners. If you believe that any content used in this article infringes upon your copyright, please contact us immediately for review and prompt action.

This article is intended for informational and educational purposes only and does not infringe on the rights of the copyright owners. If any copyrighted material has been used without proper credit or in violation of copyright laws, it is unintentional and we will rectify it promptly upon notification. Please note that the republishing, redistribution, or reproduction of part or all of the contents in any form is prohibited without express written permission from the author and website owner. For permissions or further inquiries, please contact us.

Leave a reply

Your email address will not be published. Required fields are marked