Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Docker doesn't set MariaDB password

Tags:

docker

mariadb

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?

like image 735
martin_duisburg Avatar asked Mar 27 '26 12:03

martin_duisburg


2 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.

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.

like image 105
hossein emami Avatar answered Mar 31 '26 04:03

hossein emami


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

like image 43
Tarun Lalwani Avatar answered Mar 31 '26 04:03

Tarun Lalwani