简体   繁体   English

Docker Compose 使用 MySQL 和 NodeJS 获取错误 ECONNREFUSED 127.0.0.1:3306

[英]Docker Compose getting error ECONNREFUSED 127.0.0.1:3306 with MySQL and NodeJS

I am trying to setup some containers for my NestJS + TypeORM + MySQL environment by using Docker Compose in a Windows 10 host, but I am getting an ECONNREFUSED error:我正在尝试通过在 Windows 10 主机中使用 Docker Compose 为我的 NestJS + TypeORM + MySQL 环境设置一些容器,但出现 ECONNREFUSED 错误:

connect ECONNREFUSED 127.0.0.1:3306 +2ms
backend_1  | Error: connect ECONNREFUSED 127.0.0.1:3306
backend_1  |     at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1145:16)
backend_1  |     --------------------
backend_1  |     at Protocol._enqueue (/usr/src/app/node_modules/mysql/lib/protocol/Protocol.js:144:48)
backend_1  |     at Protocol.handshake (/usr/src/app/node_modules/mysql/lib/protocol/Protocol.js:51:23)        
backend_1  |     at PoolConnection.connect (/usr/src/app/node_modules/mysql/lib/Connection.js:116:18)
backend_1  |     at Pool.getConnection (/usr/src/app/node_modules/mysql/lib/Pool.js:48:16)
backend_1  |     at /usr/src/app/node_modules/typeorm/driver/mysql/MysqlDriver.js:793:18
backend_1  |     at new Promise (<anonymous>)
backend_1  |     at MysqlDriver.createPool (/usr/src/app/node_modules/typeorm/driver/mysql/MysqlDriver.js:790:16)
backend_1  |     at MysqlDriver.<anonymous> (/usr/src/app/node_modules/typeorm/driver/mysql/MysqlDriver.js:278:51)
backend_1  |     at step (/usr/src/app/node_modules/typeorm/node_modules/tslib/tslib.js:141:27)
backend_1  |     at Object.next (/usr/src/app/node_modules/typeorm/node_modules/tslib/tslib.js:122:57)

I have created the following Dockerfile to configure the NestJS API container:我创建了以下Dockerfile来配置 NestJS API 容器:

FROM node:12-alpine
WORKDIR /usr/src/app

COPY package.json .
RUN npm install

EXPOSE 3000

#CMD ["npm", "start"]

CMD /wait-for-it.sh db:3306 -- npm start

COPY . .

And then I reference this from Docker Compose with the following docker-compose.yml :然后我使用以下docker-compose.yml从 Docker Compose 引用它:

version: "3.8"

networks:
  app-tier:
    driver: bridge

services:
  db:
    image: mysql
    command: --default-authentication-plugin=mysql_native_password
    restart: always
    expose:
      - "3306"
    ports:
      - "3306:3306"    
    networks:
      - app-tier      
    environment:
      MYSQL_DATABASE: school
      MYSQL_ALLOW_EMPTY_PASSWORD: ok
      MYSQL_ROOT_PASSWORD: root
      MYSQL_USER: dbuser
      MYSQL_PASSWORD: dbuser
      MYSQL_ROOT_HOST: '%'
  backend:
    depends_on:
      - db
    build: .
    ports:
      - "3000:3000"
    networks:
      - app-tier      

Finally, I set the TypeORM configuration to match with the Docker Compose file:最后,我将 TypeORM 配置设置为与 Docker Compose 文件匹配:

export const DB_CONFIG: TypeOrmModuleOptions = {
    type: 'mysql',
    host: 'db',
    port: 3306,
    username: 'dbuser',
    password: 'dbuser',
    database: 'school',
    entities: [], // We specify the entities in the App Module.
    synchronize: true,
};

I am kind of new to Docker Compose, but I have tried many things like changing the output port to 3307, setting an explicit network... and the port 3306 is free in my host OS when I run it.我是 Docker Compose 的新手,但我尝试了很多事情,比如将输出端口更改为 3307,设置显式网络......当我运行它时,端口 3306 在我的主机操作系统中是免费的。 Any help?有什么帮助吗?

Edit 1编辑 1

I have included MYSQL_ROOT_HOST and wait-for-it.sh as suggested, but still no results.我已经按照建议包含了MYSQL_ROOT_HOSTwait-for-it.sh ,但仍然没有结果。

I think your db is taking more time to start and your app is starting before the db, try something like this for your docker-compose.yml file:我认为您的数据库需要更多时间启动并且您的应用程序在数据库之前启动,请为您的 docker-compose.yml 文件尝试以下操作:

version: "3.8"

networks:
  app-tier:
    driver: bridge

services:
  db:
    image: mysql
    command: --default-authentication-plugin=mysql_native_password
    restart: always
    expose:
      - "3306"
    ports:
      - "3306:3306"    
    networks:
      - app-tier      
    environment:
      MYSQL_DATABASE: school
      MYSQL_ALLOW_EMPTY_PASSWORD: ok
      MYSQL_ROOT_PASSWORD: root
      MYSQL_USER: dbuser
      MYSQL_PASSWORD: dbuser
      MYSQL_ROOT_HOST: '%'
  backend:
    depends_on:
      - db
    build: .
    command: bash -c 'while !</dev/tcp/db/3306; do sleep 1; done; npm start'
    ports:
      - "3000:3000"
    networks:
      - app-tier   

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 连接到 mysql 时出现错误“connect ECONNREFUSED 127.0.0.1:3306” - Getting error "connect ECONNREFUSED 127.0.0.1:3306" while connecting to mysql Docker 组成:MySQL 与 NodeJS “错误:连接 ECONNREFUSED” - Docker compose : MySQL with NodeJS “Error: connect ECONNREFUSED” 无法让 docker compose、mysql 和我的 nodejs 应用程序正常工作 - 说:Trace: fatal error: connect ECONNREFUSED 172.21.0.2:3306 - Can't get docker compose, mysql and my nodejs app to work - says: Trace: fatal error: connect ECONNREFUSED 172.21.0.2:3306 节点+ MySQL [ECONNREFUSED 127.0.0.1:3306] - Node + MySQL [ECONNREFUSED 127.0.0.1:3306] 使用 Node.js 连接到 MySQL 数据库时出现 ECONNREFUSED 127.0.0.1:3306 错误 - Getting ECONNREFUSED 127.0.0.1:3306 error while conencting toa MySQL db using Node.js 错误:在 MySQL、Heroku 和 Node.js 上连接 ECONNREFUSED 127.0.0.1:3306 - Error: connect ECONNREFUSED 127.0.0.1:3306 on MySQL, Heroku, and Node.js Docker NodeJS 与 MySQL 连接 ECONNREFUSED 127.0.0.1:8000 - Docker NodeJS with MySQL connect ECONNREFUSED 127.0.0.1:8000 在 docker 中使用 jest 连接 ECONNREFUSED 127.0.0.1:3306 - connect ECONNREFUSED 127.0.0.1:3306 with using jest in docker Docker Compose 连接 ECONNREFUSED 172.18.0.4:3306 - Docker Compose connect ECONNREFUSED 172.18.0.4:3306 [SequelizeConnectionRefusedError]:连接ECONNREFUSED 127.0.0.1:3306 - [SequelizeConnectionRefusedError]: connect ECONNREFUSED 127.0.0.1:3306
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM