Table of Contents
In some cases, you’ve forgotten the MySQL root password, or it has mysteriously changed. In this post, we will reset the MySQL root password in Linux by starting MySQL with the –skip-grant-tables option.
Preparing
1. Firstly, you must confirm which version of MySQL on Linux you are running as commands will be different. In our case, the version of MySQL is 10.8.5.
# mysql -V
mysql Ver 15.1 Distrib 10.8.5-MariaDB, for Linux (x86_64) using readline 5.1
2. In order to skip the grant tables and reset the root password, we must first stop the MySQL service. Stop the MySQL server using the appropriate command for your Linux distribution:
###For CentOS and Fedora
sudo service mysqld stop
###For Debian and Ubuntu
sudo service mysql stop
3. Restart the MySQL server with the —skip-grant-tables option. To do this, type the following command:
mysqld_safe --skip-grant-tables &
# sudo mysqld_safe --skip-grant-tables
221103 09:11:29 mysqld_safe Logging to '/var/lib/mysql/ct7.err'.
221103 09:11:29 mysqld_safe Starting mariadbd daemon with databases from /var/lib/mysql
5. Now press to return to the Linux Bash prompt. Then run the following command to login into MySQL without any password.
sudo mysql --user=root mysql
# sudo mysqld_safe --skip-grant-tables
221103 09:11:29 mysqld_safe Logging to '/var/lib/mysql/ct7.err'.
221103 09:11:29 mysqld_safe Starting mariadbd daemon with databases from /var/lib/mysql
# sudo mysql --user=root mysql
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 3
Server version: 10.8.5-MariaDB MariaDB Server
Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
MariaDB [mysql]>
Reset MySQL root password in Linux
Reset root password for MySQL 8+
1. At the prompt, let’s reset the root account password. To do this, type the following command, replacing new_password with the new root password:
ALTER USER 'root'@'localhost' IDENTIFIED BY 'new_password';
2. Flush the privileges then exit.
flush privileges;
exit;
3. Stop the MySQL server using the following command. You will be prompted to enter the new MySQL root password before the MySQL server shuts down:
mysqladmin -u root -p shutdown
# mysqladmin -u root -p shutdown
Enter password:
[1]+ Done mysqld_safe --skip-grant-tables
4. Start the MySQL service on your Linux server using the following command:
###For CentOS and Fedora
sudo service mysqld start
###For Debian and Ubuntu
sudo service mysql start
5. Finally, log in to MySQL again and you should now be prompted for a password. Enter your new MySQL root password to verify it’s working.
# mysql -u root -p
Enter password:
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 3
Server version: 10.8.5-MariaDB MariaDB Server
Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
MariaDB [(none)]>
Reset root password for MySQL 5.7
For MySQL 5.7 on Linux, run below command to change the root password. Don’t forget replacing new_password with your own.
update user set authentication_string=PASSWORD('new_password') where user='root';
Flush the privileges and exit. Don’t forget shutdown then restart the MySQL service.
flush privileges;
exit;
Reset root password for MySQL 5.6
For MySQL 5.6 on Linux, run below command to change the root password. Don’t forget replace new_password with your own.
update user set Password=PASSWORD('new_password') where user='root';