Docker doesn't set MariaDB password

Viewed 14552

In my docker-compose.yml

version: '3'
services:
  db:
    image: mariadb:latest
    volumes:
      - ./dc_test_db:/var/lib/mysql
    restart: always
    environment:
      MYSQL_ROOT_PASSWORD: secret

When I connect via:

sudo docker exec -it docker_db_1 mysql -u root -p 

I have to let the password empty to login. What is wrong?

5 Answers

I had this problem in version 10.4 of mariadb which was fixed by changing to version 10.3. But there can be another reason for this problem.

In Docker architecture, it should be noted that images are immutable after the first build. That is, by changing the local variables defined in the docker compose file and re-running the service or re-uping the service, there will be no change in the initial settings of the builded image. To apply these changes, the steps of building the image and container and running the service must be performed again. Which can be done as follows.

1.docker-compose stop (First we stop the service)

2.Docker-compose rm (Then we clean all the related containers)

3.Docker-compose up --build -d (Finally run the service with the --build option to rebuild the images with the newly defined settings.)

Note that performing these steps will erase all data stored inside the containers.

It seems you are starting the mariadb container from an existing db data directory, this will result in using the current database instead of initializing a new one. So to solve this I would suggest to remove any existing mariadb container, remove the current db directory content, run the docker compose again:

$ docker-compose down -v
$ rm -Rf dc_test_db/*
$ docker-compose up -d

That is because you are using the client locally from inside the container itself. The local connection doesn't ask for password.

Try to connect from your host computer to the docker containerip:3306 and then it will ask for password

MySQL user is defined by username and host that request come from. For example, there is three different user root@192.168.0.123, root@localhost and wildcard root@%.

If you set MYSQL_ROOT_PASSWORD env in docker-compose file, your mariadb will set password for user root@%, not password for user root@localhost.

But when you try to test password of mariadb, you use sudo docker exec -it docker_db_1 mysql -u root -p command, it mean mariadb-client in container will use user root@local (without password) to access mariadb-server, not user root@%(that have password you set before).

So if you want to test password you set for that user, use that command:

docker run -it mariadb mysql -u root -h MARIADB-CONTAINER-IP -p

MARIADB-CONTAINER-IP is ip address of your mariadb container.(use docker inspect to check ip address of container). Thanks.

MYSQL_ROOT_PASSWORD will only be set during 1st time running the container with the given volume.

To set the password using MYSQL_ROOT_PASSWORD:

Option1: delete old DB files and start fresh.

rm -rf ./dc_test_db

Option2: use named volume:

version: '3.5'
services:
 db:
  image: mariadb:latest
  volumes:
    - dc_test_db:/var/lib/mysql
  restart: always
  environment:
    MYSQL_ROOT_PASSWORD: secret
volumes:
  dc_test_db:
Related