当前位置: 首页 > 知识库问答 >
问题:

sequelizeconconnectionrefusederror:使用docker时连接经济恢复127.0.0.1:5432使用sequelize

薛征
2023-03-14

当使用sequelize作为orm后端时,我得到了以下错误

未处理的拒绝 续集连接拒绝错误:连接 ECONNDENY 127.0.0.1:5432 app_1 |at connection.connect.err (/home/app/node_modules/sequelize/lib/dialects/postgres/connection-manager.js:170:24)

这些代码行似乎是罪魁祸首,docker 不应该连接这些凭据,因为这适用于我的本地。

if (process.env.NODE_ENV === "production") {
  var sequelize = new Sequelize(process.env.DATABASE_URL);
} else {
  // docker is looking at these credentials..... when it should not
  var sequelize = new Sequelize("elifullstack", "eli", "", {
    host: "127.0.0.1",
    dialect: "postgres",
    pool: {
      max: 100,
      min: 0,
      idle: 200000,
      // @note https://github.com/sequelize/sequelize/issues/8133#issuecomment-359993057
      acquire: 1000000,
    },
  });
}

码头组合.yml

# docker-compose.yml
version: "3"
services:
  app:
    build: ./server
    depends_on:
      - database
    ports:
      - 5000:5000
    environment:
      # database refers to the database server at the bottom called "database"
      - PSQL_HOST=database
      - PSQL_USER=postgres
      - PORT=5000
      - PSQL_NAME=elitypescript
    command: npm run server
  client:
    build: ./client
    image: react_client
    links:
      - app
    working_dir: /home/node/app/client
    volumes:
      - ./:/home/node/app
    ports:
      - 3001:3001
    command: npm run start
    env_file:
      - ./client/.env

  database:
    image: postgres:9.6.8-alpine
    volumes:
      - database:/var/lib/postgresql/data
    ports:
      - 3030:5432

volumes:
  database:

./server/dockerFile

FROM node:10.6.0
COPY . /home/app
WORKDIR /home/app
COPY package.json ./
RUN npm install
EXPOSE 5000

我查看了其他类似的问题,如下所示,但最终无助于解决问题。

Docker-SequelizeConnectionRejectedError:connect ECONNREFUSED 127.0.0.1:3306

连接ECONNREFUSED127.0.0.1:3306

共有1个答案

钮边浩
2023-03-14

我解决了。。。

我所做的就是改变这一切

 host: "127.0.0.1",

到这个

let sequelize;
if (process.env.NODE_ENV === "production") {
  sequelize = new Sequelize(process.env.DATABASE_URL);
} else {
  sequelize = new Sequelize(
    process.env.POSTGRES_DB || "elitypescript",
    process.env.POSTGRES_USER || "eli",
    "",
    {
      host: process.env.PSQL_HOST || "localhost",
      dialect: "postgres",
      pool: {
        max: 100,
        min: 0,
        idle: 200000,
        // @note https://github.com/sequelize/sequelize/issues/8133#issuecomment-359993057
        acquire: 1000000,
      },
    }
  );
}

这样主机就会像这样设置为docker环境变量

PSQL_HOST: database

这与

database:
    image: postgres:9.6.8-alpine
    volumes:
      - database:/var/lib/postgresql/data
    ports:
      - 3030:5432

编辑

# docker-compose.yml
version: "3"
services:
  app:
    build: ./server
    depends_on:
      - database
    ports:
      - 5000:5000
    environment:
      PSQL_HOST: database
      POSTGRES_PASSWORD: ${POSTGRES_PASSWORD:-password}
      POSTGRES_USER: ${POSTGRES_USER:-postgres}
      POSTGRES_DB: ${POSTGRES_DB:-elitypescript}
    command: npm run server
  client:
    build: ./client
    image: react_client
    links:
      - app
    working_dir: /home/node/app/client
    volumes:
      - ./:/home/node/app
    ports:
      - 3001:3001
    command: npm run start
    env_file:
      - ./client/.env

  database:
    image: postgres:9.6.8-alpine
    volumes:
      - database:/var/lib/postgresql/data
    ports:
      - 3030:5432

volumes:
  database:
 类似资料:
  • 尝试使用Docker容器将Kibana连接到ES时出错: kibana-product-624|{"type":"log","@time戳":"2018-05-25T14:56:36Z","tags":["警告","elasticsearch","admin"],"pid": 1,"消息":"无法恢复连接:超文本传输协议://elasticsearch: 9200/"}kibana-product

  • null 创建套接字->将其绑定到随机端口 呼叫连接:连接到本地主机,端口12345 =>连接调用失败,错误为10049,如上所述 这里是包括“服务器”在内的主要功能:

  • 我有一个使用Mongo数据库的Node express服务器。 我正在尝试使用为它们中的每一个创建一个容器。 这是我的

  • 我想让应用程序恢复音乐当我们回到应用程序时,我尝试过使用方法,但服务中没有方法。TIA

  • 问题内容: 有某些功能,例如不带https的JavaScript服务工作者,仅可在localhost上运行,但是当我使用在docker- machine上运行的docker-compose在docker容器内运行我的应用程序时,我需要使用我从那里得到的地址 有没有办法映射到该IP? 问题答案: 您可以向前添加VirtualBox端口,以将Docker主机上的端口映射到本地计算机。 假设您的dock

  • 使用docker通过docker compose将springboot连接到postgres。在postgres上使用5432端口可以正常工作,如果我尝试其他端口,它就会失败 工作代码 Spring docker compose **拒绝连接代码** Spring docker compose 错误: spring boot rest服务器| org。postgresql。util。PSQLExc