Pyae Phyoe Shein
Pyae Phyoe Shein

Reputation: 13837

NodeJS could not connect to MYSQL latest version inside Docker Container

NodeJS cannot connect to MySQL latest version or either 8 onwards and encountered following error message:

ERROR: connect ECONNREFUSED 172.21.0.2:3306

Here is my docker-compose file

version: '2.1'
services:
  db:
      build: ./db
      networks:
        - ppshein
      environment:
          - MYSQL_ALLOW_EMPTY_PASSWORD=yes
      healthcheck:
          test: "exit 0"
  node:
    build: ./app
    depends_on:
      db:
        condition: service_healthy
    ports:
      - 3000:3000
    networks:
      - ppshein
networks:
  ppshein:

Here is db Dockerfiles

FROM mysql:5
COPY init_db.sql /docker-entrypoint-initdb.d/

init_db.sql

CREATE DATABASE IF NOT EXISTS database_docker;
GRANT ALL PRIVILEGES on database_docker.*
TO 'root'@'%' IDENTIFIED BY 'ppshein123456'
WITH GRANT OPTION;

NodeJS Dockerfile

FROM node:9.10.1
ENV NODE_ENV=docker
COPY ./ /var/www
WORKDIR /var/www/
RUN yarn install && yarn add sequelize-cli -g
EXPOSE 3000
ENTRYPOINT [ "npm", "run", "docker" ]

Config.json

"docker": {
    "username": "root",
    "password": "ppshein123456",
    "database": "database_docker",
    "host": "db",
    "dialect": "mysql",
    "logging": false
}

But everything is working file when I've changed to FROM mysql:5 but FROM mysql or FROM mysql:8, I've encountered above error I've mentioned. Please let me know which kind of configuration do I need to miss it?

Upvotes: 1

Views: 1133

Answers (2)

Pyae Phyoe Shein
Pyae Phyoe Shein

Reputation: 13837

I've found a way to fix that authentication issue. I need to add following command --default-authentication-plugin=mysql_native_password and MYSQL_ROOT_PASSWORD=ppshein123456 on docker-compose file.

command: --default-authentication-plugin=mysql_native_password
restart: always
ports:
    - 3306:3306
environment:
    - MYSQL_ROOT_PASSWORD=ppshein123456
    - MYSQL_ALLOW_EMPTY_PASSWORD=yes

Upvotes: 2

Dave Stokes
Dave Stokes

Reputation: 823

MySQL 8 be default uses a new SHA256 encryption for password that your connector probably does not use. You can update your connector to one that understands SHA256 or change the password on the account being used to one using MySQL Native Password (the older password default)

Upvotes: 1

Related Questions