NodeJS could not connect to MYSQL latest version inside Docker Container

心不动则不痛 提交于 2019-12-11 17:33:29

问题


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?


回答1:


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)




回答2:


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


来源:https://stackoverflow.com/questions/54384845/nodejs-could-not-connect-to-mysql-latest-version-inside-docker-container

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!