Skip to content

Admin password Lost (MariaDb)

Andrew Savinykh edited this page Sep 1, 2022 · 2 revisions

If you did not change that part of the sample docker-compose.yaml, the MariaDb sections looks like this:

  mariadb:
    image: mariadb:10.2
    restart: ${RESTART_MODE}
    # Info : These variables are ignored when the volume already exists (if databases was created before).
    environment:
      - MYSQL_RANDOM_ROOT_PASSWORD=yes
      - MYSQL_DATABASE=postfix
      - MYSQL_USER=postfix
      - MYSQL_PASSWORD=${DATABASE_USER_PASSWORD}
    volumes:
      - ${VOLUMES_ROOT_PATH}/mysql/db:/var/lib/mysql
    networks:
      - mail_network

Note, the random root password setting. If you are running your container for the first time, that is if there is no existing database, the root password will be generated and can be retrieved with this command:

docker-compose logs | grep -i generated

However, if you set up long time ago, chances are the password will not be in your container log. To reset it add command: --skip-grant-tables to the configuration above, like this:

  mariadb:
    image: mariadb:10.2
    restart: ${RESTART_MODE}
    # Info : These variables are ignored when the volume already exists (if databases was created before).
    environment:
      - MYSQL_RANDOM_ROOT_PASSWORD=yes
      - MYSQL_DATABASE=postfix
      - MYSQL_USER=postfix
      - MYSQL_PASSWORD=${DATABASE_USER_PASSWORD}
    volumes:
      - ${VOLUMES_ROOT_PATH}/mysql/db:/var/lib/mysql
    networks:
      - mail_network
    command: --skip-grant-tables

Then recreate the containers: docker-compose up -d --force-recreate

Now you can exec into the Maria DB container with docker exec -it mariadb bash and the login with any password with mysql -u root -pp

From here you can reset the root password like this:

MariaDB [none]> flush privileges;
Query OK, 0 rows affected (0.002 sec)

MariaDB [none]>  ALTER USER 'root'@'localhost' IDENTIFIED BY 'newpassword';
Query OK, 0 rows affected (0.005 sec)
MariaDB [none]>  ALTER USER 'root'@'%'  IDENTIFIED BY 'newpassword';
Query OK, 0 rows affected (0.007 sec)

Use your own password instead of newpassword. Exit from mysql and from the container, revert back the command change above in docker-compose.yaml and recreate the containers again with docker-compose up -d --force-recreate. MariaDb password is now reset.