How can I set my password for my database user once?

Viewed 169

I have started a new project using docker-compose however my migrations and database queries are falling with the error Access denied for user 'data-jobs'@'172.18.0.3' (using password: YES)

I have 2 config files. .env for my laravel configuration and .db.env for my database environment variables. I got this error because I had changed the value in the .env file but not in the .db.env file.

My question is this:

How can I use can I pass MYSQL_PASSWORD and DB_PASSWORD to Laravel and MariaDB using only one config key to prevent me making this mistake again?

1 Answers

Your env variables names are wrong, the Docker mysql image uses different keys.

MYSQL_USER
MYSQL_DATABASE
....

https://hub.docker.com/_/mariadb https://hub.docker.com/_/mysql

(MariaDB as a drop-in replacement uses the same keys.)

If you want to use your custom keys, you need to map them in the docker-compose.yml:

db-data-jobs:
    image: mariadb:10.6
    container_name: db-data-jobs
    restart: unless-stopped
    tty: true
    env_file:
      - .db.env
    volumes:
      - db-data:/var/lib/mysql
      - type: bind
        source: /etc/localtime
        target: /etc/localtime
    ports:
      - '3307:3306'
    networks:
      - app-net
    environment:
      - MYSQL_USER=${DB_USERNAME}

https://docs.docker.com/compose/environment-variables/

Make sure you don't twist the names of your files: .env and .db.env.

Related