dockerdocker-compose

docker SQLSTATE[HY000] [1045] Access denied for user 'user'@'172.20.0.3' (using password: YES)


When running at localhost my project is working fine. But, after running the docker compose with the command sudo docker-compose up -d --build in production, when entering the webapp url I see the error:

An exception occurred in driver: SQLSTATE[HY000] [1045] Access denied for user 'user'@'172.20.0.3' (using password: YES)

My docker-compose.yml is

version: '3'

services:
  database:
    build:
      context: ./database
    environment:
      - MYSQL_DATABASE=${DATABASE_NAME}
      - MYSQL_USER=${DATABASE_USER}
      - MYSQL_PASSWORD=${DATABASE_PASSWORD}
      - MYSQL_ROOT_PASSWORD=${DATABASE_ROOT_PASSWORD}
    ports:
      - "3306:3306"
    volumes:
      - ./database/init.sql:/docker-entrypoint-initdb.d/init.sql
      - ./database/data:/var/lib/mysql
    networks:
      - webappnet

  php-fpm:
    build:
      context: ./php-fpm
    depends_on:
      - database
    environment:
      - APP_ENV=${APP_ENV}
      - APP_SECRET=${APP_SECRET}
      - DATABASE_URL=mysql://${DATABASE_USER}:${DATABASE_PASSWORD}@database:3306/${DATABASE_NAME}?serverVersion=mariadb-10.6.4
      - PODIO_CLIENT_ID=${PODIO_CLIENT_ID}
      - PODIO_CLIENT_SECRET=${PODIO_CLIENT_SECRET}
      - DEFAULT_MAIL_FROM=${DEFAULT_MAIL_FROM}
      - SMTP_SERVER=${SMTP_SERVER}
      - MAILER_DSN=${MAILER_DSN}
      - MAILTO=${MAILTO}
    volumes:
      - ../src:/var/www
      - ./php-fpm/php.ini:/usr/local/etc/php/conf.d/php.override.ini:ro

  nginx:
    build:
      context: ./nginx
    tty: true
    volumes:
      - ../src:/var/www
      - ./nginx/nginx.conf:/etc/nginx/nginx.conf
      - ./nginx/sites/:/etc/nginx/sites-available
      - ./nginx/conf.d/:/etc/nginx/conf.d
      - ./nginx/sites/site.conf:/etc/nginx/conf.d/site.conf
      - ./logs:/var/log/nginx/
    depends_on:
      - php-fpm
    links:
      - php-fpm
    ports:
      - "8000:80"

    networks:
      - webappnet

my .env file has the variables declared:

DATABASE_NAME=dbname
DATABASE_USER=user
DATABASE_PASSWORD=password
DATABASE_ROOT_PASSWORD=adminpassword

I also tried to enter to the database container and enter manually to the database. But entering the command mysql -u user -p and when asked, the password password returns same error:

ERROR 1045 (28000): Access denied for user 'user'@'localhost' (using password: YES)

I can't see what I'm doing wrong, and it's working on localhost...


Solution

  • Finally I found a solution, maybe it's not the best one, but it was an affordable one as the production machine it's not working yet and the database was almost empty.

    It seems something was corrupted on the data, I deleted the database, deleted the docker containers, and the images and everything and then I started again the docker-compose up.

    Now it's working, but it doesn't seem a really good solution to me, because if you have a production database plenty of data, deleting it it's not a good idea.