superfive33
superfive33

Reputation: 1174

Issue with connecting to MariaDB via docker

I am setting up 2 similar projects with Docker. They use the same images: wordpress:php7.4-apache and mariadb:10.3.39. I am using different ports for both projects, just to be able to run both project simultaneously : first MariaDB runs on 3306, second MariaDB runs on 3307. The error also happens if I use the same port for both projects, and run one single project at a time.

Connection to MariaDB does not work for first project, but works fine for second. Both projects have the exact same configuration!

Here is config for project 1:

.env file

# .env
COMPOSE_PROJECT_NAME=project1
MYSQL_ROOT_PASSWORD=root
MYSQL_DATABASE=database
MYSQL_USER=docker
MYSQL_PASSWORD=secret

docker-compose.yml

version: '3'
services:
  db:
    platform: linux/x86_64
    image: mariadb:10.3.39
    ports:
      - "3306:3306"
    volumes:
      - my_db:/var/lib/mysql
    environment:
      MYSQL_ROOT_PASSWORD: ${MYSQL_ROOT_PASSWORD}
      MYSQL_DATABASE: ${MYSQL_DATABASE}
      MYSQL_USER: ${MYSQL_USER}
      MYSQL_PASSWORD: ${MYSQL_PASSWORD}

  wordpress:
    image: wordpress:php7.4-apache
    depends_on:
      - db
    ports:
      - "8000:80"
    volumes:
      - ./:/var/www/html
    environment:
      WORDPRESS_DB_HOST: db
      WORDPRESS_DB_USER: ${MYSQL_USER}
      WORDPRESS_DB_PASSWORD: ${MYSQL_PASSWORD}
      WORDPRESS_DB_NAME: ${MYSQL_DATABASE}

volumes:
  my_db:

here is config for project 2:

.env file

# .env
COMPOSE_PROJECT_NAME=project2
MYSQL_ROOT_PASSWORD=root
MYSQL_DATABASE=database
MYSQL_USER=docker
MYSQL_PASSWORD=secret

docker-compose.yml

version: '3'
services:
  db:
    platform: linux/x86_64
    image: mariadb:10.3.39
    ports:
      - "3307:3306"
    volumes:
      - my_db:/var/lib/mysql
    environment:
      MYSQL_ROOT_PASSWORD: ${MYSQL_ROOT_PASSWORD}
      MYSQL_DATABASE: ${MYSQL_DATABASE}
      MYSQL_USER: ${MYSQL_USER}
      MYSQL_PASSWORD: ${MYSQL_PASSWORD}

  wordpress:
    image: wordpress:php7.3-apache
    depends_on:
      - db
    ports:
      - "8001:80"
    volumes:
      - ./:/var/www/html
    environment:
      WORDPRESS_DB_HOST: db
      WORDPRESS_DB_USER: ${MYSQL_USER}
      WORDPRESS_DB_PASSWORD: ${MYSQL_PASSWORD}
      WORDPRESS_DB_NAME: ${MYSQL_DATABASE}

volumes:
  my_db:

When I try to connect to MariaDB in project 1, I get this error:

docker exec -it 609f7807c859 mysql -udocker -psecret -e 'show databases;'
ERROR 1045 (28000): Access denied for user 'docker'@'localhost' (using password: YES)

Whereas in project 2, it works:

docker exec -it 484a719c2e6e mysql -udocker -psecret -e 'show databases;'

----------------------
| Database           |
----------------------
| database           |
| information_schema |

Thanks for your help.

Upvotes: 0

Views: 448

Answers (1)

zori
zori

Reputation: 2278

Probably you have done docker-compose up with different credentials.

When you do first docker-compose up volumes is created and user MYSQL_USER with password MYSQL_PASSWORD, and even after calling docker-compose down and then changing password in .env and calling again docker-compose up password will be the same. To resolve it you can manually remove volume or just call docker-compose -f docker-compose.yaml down --volumes it will remove also volumes(so if need dump your data before doing it) and recreate it using docker-compose up

Upvotes: 2

Related Questions