How to reset MySQL root password on Linux

Introduction

For some reasons, you may have forgotten MySQL root password and need to reset. Follow this guide to reset the MySQL root password on any linux server. This guide will work for any version of MySQL running on any Linux server.

stop mysql service and start the daemon in safe mode:

# Debian/Ubuntu

sudo systemctl stop mysql

# CentOS 7 server

sudo systemctl stop mysqld

Start MySQL service using mysqld_safe. Run the command mysqld_safe because adds some safety features

sudo mysqld_safe --skip-grant-tables &

mysql -u root

Reset root password:

use mysql;

update user set password=PASSWORD("password") where User='root';

flush privileges;
quit

Restart service to resume normal database operations.:

sudo systemctl restart mysql

Ubuntu 18.04, Ubuntu 20.04, Debian 9, Debian 10, CentOS 7.