Cyberithub

How to reset MySQL root password on RedHat/CentOS 7

In this article, I will take you through the steps to reset MySQL root password on RedHat/CentOS 7.

The MySQL root password allows the root user to have full access to the MySQL database.

Reset MySQL root password

Use the following steps to reset MySQL root password by using the command line interface.

Step 1: Prerequisites

You must have (Linux) root or (Windows) Administrator access to the Cloud Server to reset the MySQL root password.

How to reset MySQL root password on RedHat/CentOS 7 1

Step 2: Stop MySQL Service

You need to stop the mariadb service first using below command.

[root@localhost ~]# service mariadb stop
Redirecting to /bin/systemctl stop mariadb.service

Step 3: Start MySQL in Safe Mode

Run the following command. Do not forget to put ampersand (&) at the end of the command.

[root@localhost ~]# sudo mysqld_safe --skip-grant-tables &
[1] 27027
[root@localhost ~]# 191212 13:34:50 mysqld_safe Logging to '/var/log/mariadb/mariadb.log'.
191212 13:34:51 mysqld_safe Starting mysqld daemon with databases from /var/lib/mysql

Step 4: Connect to MySQL

Run the following command to connect to mysql:-

[root@localhost ~]# mysql -u root
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 2
Server version: 10.1.20-MariaDB MariaDB Server

Copyright (c) 2000, 2016, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

MariaDB [(none)]>

Step 5: Set a new MySQL root password

Run the following command to reset mysql root password:-

MariaDB [(none)]> use mysql;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Database changed
MariaDB [mysql]> update user set authentication_string=PASSWORD("root@123$") where User='root';
Query OK, 4 rows affected (0.03 sec)
Rows matched: 4 Changed: 4 Warnings: 0

MariaDB [mysql]> flush privileges;
Query OK, 0 rows affected (0.24 sec)

MariaDB [mysql]> quit
Bye

Step 6: Stop and start the MySQL service

Stop mariadb service

[root@localhost ~]# service mariadb stop

Start mariadb service

[root@localhost ~]# service mariadb start

Step 7: Log in to the database

Test the new password by logging in to the database.

[root@localhost ~]# mysql -u root -p
Enter password:

Welcome to the MariaDB monitor. Commands end with ; or \g.
Server version: 10.1.20-MariaDB MariaDB Server

Copyright (c) 2000, 2016, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

MariaDB [(none)]>

Also Read: How to install MySQL 5.5 on CentOS

Reference: MySQL 5.5 Manual

Leave a Comment