简体   繁体   English

Docker Compose 连接 ECONNREFUSED 172.18.0.4:3306

[英]Docker Compose connect ECONNREFUSED 172.18.0.4:3306

When I build the container of the project with this command:当我使用此命令构建项目的容器时:

sudo docker build -t PROJECT_NAME .

And then I download the mysql's image through this Docker-Compose config:然后我通过这个 Docker-Compose 配置下载 mysql 的镜像:

  db:
    image: mysql
    restart: always
    ports:
      - "8999:3306"
    networks:
      - webnet
    environment:
      MYSQL_DATABASE: slack
      MYSQL_ROOT_PASSWORD: admin

And then, the project will connect with MySQL through the Sequelize ORM然后,项目将通过Sequelize ORM连接 MySQL

I have this error:我有这个错误:

Unhandled rejection SequelizeConnectionRefusedError: connect ECONNREFUSED 172.18.0.4:3306

在此处输入图片说明

How can I resolve this?我该如何解决这个问题?

The Sequelize config is this: Sequelize 配置是这样的:

const sequelize = new Sequelize(process.env.TEST_DB || 'slack', 'root', 'admin', {
  host: process.env.DB_HOST || 'localhost',
  operatorsAliases: Sequelize.Op,
  dialect: 'mysql',
  define: {
    underscored: true
  }
});

The config of the web is this:网页的配置是这样的:

web:
    image: slack-clone-server
    ports:
      - "8080:8080"
    networks:
      - webnet
    environment:
      DB_HOST: db
      REDIS_HOST: redis
    command: ["./wait-for-it.sh", "db:3306", "--", "npm", "run", "prod"]

The script called "wait for it" is this .名为“等待它”的脚本是这样的

If someone needs the complete code, here you go:如果有人需要完整的代码,请访问:

Sequelize config 续集配置

Docker Compose config Docker Compose 配置

Dockerfile config Dockerfile 配置

wait-for-it.sh by default waits for 15 seconds and returns, even if the target isn't ready yet. wait-for-it.sh默认等待15 秒并返回,即使目标尚未准备好。 You see that in your output too.您也可以在输出中看到这一点。 But the database isn't ready yet.但是数据库还没有准备好。 Make wait-for-it.sh wait longer , maybe with -t 90 to wait for 90 seconds or -t 0 to make it wait forever.使wait-for-it.sh 等待更长时间,可能使用-t 90等待 90 秒或使用-t 0使其永远等待。

(In my experience the Docker database containers routinely take 30-60 seconds to start up, especially the first time.) (根据我的经验,Docker 数据库容器通常需要 30-60 秒才能启动,尤其是第一次。)

David already said that the db is not ready at this moment.大卫已经说过,此时数据库尚未准备好。 You can use depends_on in your docker-compose file as well.您也可以在docker -compose 文件中使用depends_on This will start and initialize the db first and then start the "web" container.这将首先启动并初始化数据库,然后启动“web”容器。

services:
  web:
    ...
    depends_on:
      - db

  db:
    image: mariadb:latest
    ...
db:
    container_name: mysqldb //or whatever you want <<Add This
    image: mysql
    restart: always
    ports:
      - "8999:3306"
    networks:
      - webnet
    environment:
      MYSQL_DATABASE: slack
      MYSQL_ROOT_PASSWORD: admin

And Then assign the value of process.env.DB_HOST to Mysql Container name然后将process.env.DB_HOST的值赋给Mysql Container name

Like: process.env.DB_HOST=mysqldb像: process.env.DB_HOST=mysqldb

Assign the value of process.env.DB_HOST to Mysql Container name将 process.env.DB_HOST 的值赋给 Mysql Container name

Like: process.env.DB_HOST=mysqldb像:process.env.DB_HOST=mysqldb

暂无
暂无

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

相关问题 在 docker 中使用 jest 连接 ECONNREFUSED 127.0.0.1:3306 - connect ECONNREFUSED 127.0.0.1:3306 with using jest in docker Docker Compose 使用 MySQL 和 NodeJS 获取错误 ECONNREFUSED 127.0.0.1:3306 - Docker Compose getting error ECONNREFUSED 127.0.0.1:3306 with MySQL and NodeJS 无法让 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 [SequelizeConnectionRefusedError]:连接ECONNREFUSED 127.0.0.1:3306 - [SequelizeConnectionRefusedError]: connect ECONNREFUSED 127.0.0.1:3306 Docker 组成:MySQL 与 NodeJS “错误:连接 ECONNREFUSED” - Docker compose : MySQL with NodeJS “Error: connect ECONNREFUSED” SequelizeConnectionRefusedError:连接 ECONNREFUSED 127.0.0.1:3306 - SequelizeConnectionRefusedError: connect ECONNREFUSED 127.0.0.1:3306 未处理的拒绝 SequelizeConnectionRefusedError:连接 ECONNREFUSED 127.0.0.1:3306 - Unhandled rejection SequelizeConnectionRefusedError: connect ECONNREFUSED 127.0.0.1:3306 连接到 mysql 时出现错误“connect ECONNREFUSED 127.0.0.1:3306” - Getting error "connect ECONNREFUSED 127.0.0.1:3306" while connecting to mysql 无法连接到“localhost:3306”上的 MySQL 服务器 (99) - 使用 docker-compose - Can't connect to MySQL server on 'localhost:3306' (99) - With docker-compose 错误:连接 ECONNREFUSED 185.248.177.110:3306 at TCPConnectWrap.afterConnect [as oncomplete],cpanel - Error: connect ECONNREFUSED 185.248.177.110:3306 at TCPConnectWrap.afterConnect [as oncomplete], cpanel
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM