How to Reset MySQL Root Password

How to Reset MySQL Root Password

In this blog post, we will show you how to reset the MySQL root Password on a Linux operating system. Resetting the MySQL root password is crucial for security, preventing unauthorized access, data theft, or data destruction, especially since default installations often have no password. It’s also essential for recovery if the password is lost or forgotten, allowing you to regain control and manage your database server effectively. Additionally, we’ll cover how to reset your root password across different Linux distributions and MySQL versions.

Resetting MySQL Root Password is a straightforward process that may take a couple of minutes. Let’s get things done!

Prerequisites

  • A Linux VPS distribution
  • User privileges: root or non-root user with sudo privileges

Step 1. Stop the MySQL database server

Stopping the MySQL database service differs between Ubuntu and Debian, and between CentOS and AlmaLinux. Let’s see the differences:

 NEWER OS Distributions

 #Ubuntu and Debian

 sudo systemctl stop mysql

 #CentOS and AlmaLinux

 sudo systemctl stop mysqld

 ----------------------------

 OLDER OS Distributions

 #Ubuntu and Debian

 sudo /etc/init.d/mysql stop

 #CentOS and AlmaLinux

 sudo /etc/init.d/mysqld stop

As you can see, the difference is in the syntax, and depending on your OS and whether it is an older or newer distribution, that command should be used.

Step 2. Start the MySQL server with the “skip-grant-tables” option

This option disables the permission system, allowing anyone to connect without a password. It should only be used for recovery in a secure, local environment. First, we have to create the /var/run/mysqld/ directory with the correct permissions:

mkdir /var/run/mysqld

chown -R mysql:mysql /var/run/mysqld/ 

Since we already stopped the MySQL service using the commands from the previous step, we can start MySQL in safe mode:

mysqld_safe --skip-grant-tables &

This will disable the MySQL authentication and & run the process in the background.

Step 3. Log in to MySQL as the root user

Now, we can log in to MySQL without a root password:

#NEWER OS distribution, no matter Ubuntu-based or CentOS-based

mysql -u root -p

OR

mysql

------------------------------------------------------------------------------

#OLDER OS distribution, no matter Ubuntu-based or CentOS-based

mysql -u root mysql

Step 4. Reset MySQL password

To reset the MySQL root password, we can use the following commands, depending on whether we have a newer or older MySQL version:

#NEWER MySQL versions, no matter Ubuntu-based or CentOS-based

FLUSH PRIVILEGES;
ALTER USER 'root'@'localhost' IDENTIFIED BY 'YourNewStrongMySQLRootPassword';
EXIT;

------------------------------------------------------------------------------

#OLDER MySQL versions, no matter Ubuntu-based or CentOS-based

UPDATE user SET Password=PASSWORD('YourNewStrongMySQLRootPassword') WHERE User='root';
FLUSH PRIVILEGES;
EXIT;

Step 5. Start the MySQL service

After the password has been reset, start the MySQL server in normal mode:

NEWER OS Distributions

sudo pkill -9 mysql #First kill all mysql processes

#Ubuntu and Debian

sudo systemctl start mysql

#CentOS and AlmaLinux

sudo systemctl start mysqld

----------------------------

OLDER OS Distributions

#Ubuntu and Debian

sudo /etc/init.d/mysql start

#CentOS and AlmaLinux

sudo /etc/init.d/mysqld start

Now you can connect to MySQL as a root user using the new password.

Type “mysql -u root -p” in the terminal, and enter the new password you set in Step 4 when prompted.

Conclusion

That’s it all! You have learned how to reset the MySQL root password across all Linux distributions and for different MySQL versions.

To make this more straightforward, please take a look at the Linux OS distributions and MySQL versions:

#Newer MySQL versions

MySQL 8.0
MySQL 9.0

#Older MySQL versions

MyQSL 5.7
MySQL 5.6


#Newer Linux OS distributions

Ubuntu  24.04
Ubuntu  22.04
Ubuntu  20.04
Ubuntu  18.04

Debian  13
Debian  12
Debian  11

CentOS 7

AlmaLinux 10
AlmaLinux 9
AlmaLinux 8

#Older Linux OS distributions

Ubuntu 16.04
Ubuntu 14.04
Ubuntu 12.04

Debian 10
Debian 9
Debian 8
Debian 7

CentOS 6

If you are one of our customers, you don’t have to follow this tutorial and reset the MySQL root password yourself. Our experienced Linux admins will do that for you. They are available 24×7 and will address your request immediately. Submit a ticket.

If you liked this post, please share it with your friends or leave a comment below. Thanks.

Leave a Comment