Reputation: 111
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?
Upvotes: 11
Views: 41230
Reputation: 286
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.
New update on Tuesday. June 6, 2023.
According to the "Environment Variables" section in the https://hub.docker.com/_/mariadb When you start the MariaDB image, you can adjust the initialization of the MariaDB instance by passing one or more environment variables on the docker run command line (or in docker compose file). Do note that none of the variables below will have any effect if you start the container with a data directory that already contains a database: any pre-existing database will always be left untouched on container startup.
It means that any changes in the "MARIADB_ROOT_PASSWORD" environment variable will have any effect if you start the container with a data directory. therefore you need to delete the data directory in order to initiate a fresh instance.
Upvotes: 10
Reputation: 3065
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
Upvotes: 2
Reputation: 169
MySQL user is defined by username and host that request come from. For example, there is three different user [email protected]
, 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.
Upvotes: 1
Reputation: 689
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:
Upvotes: 1
Reputation: 146630
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
Upvotes: 2