Docker: how to reset the MySQL or MariaDB root password with Docker Compose

03.10.2024 Published by:

How to reset an unavailable MySQL or MariaDB root password in Docker using a temporary entrypoint, then restore the normal Docker Compose configuration.

When taking over maintenance of a server, you may encounter a variety of configurations. One common issue is that the passwords for services running on the server are unavailable.

A typical example is MySQL or MariaDB running in Docker. In this situation, follow these steps.

Override the entrypoint for the MariaDB Docker container in docker-compose.yml by adding:

entrypoint: mysqld_safe --skip-grant-tables --user=mysql

Start the Docker Compose stack:

docker-compose up -d

Open a shell in the Docker container, replacing docker-container-name with its actual name:

sudo docker exec -ti docker-container-name bash

Log in to MySQL as root without a password. If prompted, leave the password blank:

mysql -u root -p

Change the root password in the MySQL command-line interface, replacing the example password with your own:

FLUSH PRIVILEGES;
ALTER USER 'root'@'localhost' IDENTIFIED BY 'your_secret_password';
FLUSH PRIVILEGES;

Exit MySQL and the Docker container by running exit twice. Remove the temporary entrypoint line from docker-compose.yml, then recreate the Docker Compose stack:

docker-compose up -d

You can now open the MariaDB container and connect to the database using the new root password:

sudo docker exec -ti docker-container-name bash
mysql -u root -p

About the author

Leszek Iwaniec is the owner of Projectic. He works on online store implementations, custom applications and technical support. On the blog, he describes problems and solutions involving PrestaShop, Laravel and system integrations, drawing on the team’s project experience.

Back