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