Recovering a lost root password in MySQL or MariaDB restores access to account management, backups, and server-wide configuration changes. A password reset is often faster and safer than rebuilding the data directory or restoring a full database dump.
Authentication is enforced by the privilege (grant) tables in the mysql system schema. Starting the server with --skip-grant-tables bypasses privilege checks, enabling a local session to assign a new authentication method and password for root@localhost before returning the daemon to normal startup.
During bypass mode, connections through the local socket gain unrestricted access, so the server must stay isolated and the window must stay short. The workflow below uses a containerized MySQL instance; on systemd hosts, stop the service and start mysqld_safe with --skip-grant-tables instead.
Steps to set or reset MySQL/MariaDB root password:
- Stop the running container before starting bypass mode.
$ sudo docker stop sg-mysql sg-mysql
- Start a temporary server in bypass mode using --skip-grant-tables and --skip-networking.
$ sudo docker run --rm -d --name sg-mysql-reset --volumes-from sg-mysql --network none mysql:8.0 --skip-grant-tables --skip-networking f7aec53137c5b75145aa41c66114a0f26b236e1331a1aa07bd1280155bc79105
Authentication checks are disabled, so local socket access becomes full administrative access; keep the host isolated; shut down bypass mode immediately after the password change.
- Open a local mysql client session as root without a password.
$ sudo docker exec -it sg-mysql-reset mysql -u root Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 7 Server version: 8.0.44 MySQL Community Server - GPL ##### snipped ##### mysql>
- Reload the grant tables inside the mysql shell.
mysql> FLUSH PRIVILEGES; Query OK, 0 rows affected (0.01 sec)
- Exit the mysql shell.
mysql> exit Bye - Stop the bypass-mode container.
$ sudo docker stop sg-mysql-reset sg-mysql-reset
- Restart the original container with normal authentication.
$ sudo docker start sg-mysql sg-mysql
- Verify password authentication for the root account.
$ sudo docker exec -it sg-mysql mysql -u root -p Enter password: Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 8 Server version: 8.0.44 MySQL Community Server - GPL ##### snipped ##### mysql>
Reaching the mysql> prompt confirms that the new password is active.
Mohd Shakir Zakaria is a cloud architect with deep roots in software development and open-source advocacy. Certified in AWS, Red Hat, VMware, ITIL, and Linux, he specializes in designing and managing robust cloud and on-premises infrastructures.
