node.jsdocker-compose

docker-compose.yml with mysql error "Access denied for user 'root'@'172.18.0.2' (using password: YES)"


Have the following docker-compose.yml:

version: '3'
services:
  mysql-server:
    container_name: mysql8
    restart: always
    image: mysql:8.0
    #command: mysqld --default-authentication-plugin=mysql_native_password --character-set-server=utf8mb4 --collation-server=utf8mb4_unicode_ci
    environment:
      MYSQL_ROOT_PASSWORD: dummypassword
      #MYSQL_DATABASE: dbname
      #MYSQL_USER: root
      #MYSQL_PASSWORD: dummypassword
#      MYSQL_ALLOW_EMPTY_PASSWORD: "yes"
    ports:
      - '3306:3306'
    volumes:
      - './docker/db/data:/var/lib/mysql'
  node-app:
    build: .
    ports:
      - "8080:8080"
volumes:
  my-datavolume:

In my Nodejs using mysql2

and connecting to the database

      pool = mysql.createPool({
        user: DBUser, <---- root
        password: DBPassword, <--- dummypassword
        host: 'mysql-server',//DBHost,
        database: DBName,
      });

When making a request to my Node.js App returns the following error:

"error": {
            "message": "Access denied for user 'root'@'172.18.0.2' (using password: YES)",
            "code": "ER_ACCESS_DENIED_ERROR",
            "errno": 1045,
            "sqlState": "28000",
            "sqlMessage": "Access denied for user 'root'@'172.18.0.2' (using password: YES)"
        }

So am I not passing correctly the password? what changes do I need to do to my docker-compose.yml to make this work and not return this error.

thanks for any help provided still learning about docker and docker-compose


Solution

  • 1º The main problem was related to the password it couldn't have special caracthers saw that in another post so with a simple password it worked.

    2º Force to clear alls the cache so not running a older version of the Docker:

    $ docker-compose down && docker-compose build --no-cache && docker-compose up
    

    3º Before using Docker the enviroment is windows that is case-insensitive and with Docker is unix that is case-sensitive and when importing/requiring a lib had "./Custom_Modules/logger/logger.js" when the correct is "./Custom_Modules/Logger/logger.js" but had it working because it was windows and when running in Docker was giving error of not founding the file ...