gpt4 book ai didi

node.js - docker-compose POSTGRES & SEQUELIZE 的数据库连接错误

转载 作者:行者123 更新时间:2023-12-02 19:29:14 25 4
gpt4 key购买 nike

我有一个使用 postgres 和 sequelize 的 Node 应用程序。我有一个可以运行我的服务器的 docker 文件。我还有一个 docker compose 文件,该文件将运行链接我的 docker 图像并将它们连接在一起的 web 图像和 db 图像。

我能够通过 docker 文件让我的服务器运行。我正在尝试使用 docker-compose 文件来运行数据库并让它们工作。我正在尝试连接的数据库出现连接错误,我不确定此错误来自何处...

文件

FROM node:10
WORKDIR /app
COPY package.json ./app
RUN npm install
COPY . /app
CMD npm start
EXPOSE 5585

停靠撰写文件:
version: "2"
services:
web:
build: .
ports:
- 80:5585
command: npm start
depends_on:
- db
environment:
- DATABASE_URL=postgres://username:password@db:5432/addidas
db:
image: postgres
restart: always
ports:
- "5432:5432"
environment:
- POSTGRES_USER=username
- POSTGRES_PASSWORD=password
- POSTGRES_DB=addidas

我还使用 sequelize 将我的数据库与 express 连接起来。我需要改变任何东西来做到这一点。

错误:
web_1  | Fri, 09 Nov 2018 18:26:47 GMT sequelize deprecated String based operators are now deprecated. Please use Symbol based operators for better security, read more at http://docs.sequelizejs.com/manual/tutorial/querying.html#operators at node_modules/sequelize/lib/sequelize.js:242:13
web_1 | server is running at http://localhost:3001
web_1 | { SequelizeConnectionRefusedError: connect ECONNREFUSED 172.19.0.3:5432
web_1 | at connection.connect.err (/app/node_modules/sequelize/lib/dialects/postgres/connection-manager.js:116:24)
web_1 | at Connection.connectingErrorHandler (/app/node_modules/pg/lib/client.js:140:14)
web_1 | at Connection.emit (events.js:182:13)
web_1 | at Socket.reportStreamError (/app/node_modules/pg/lib/connection.js:71:10)
web_1 | at Socket.emit (events.js:182:13)
web_1 | at emitErrorNT (internal/streams/destroy.js:82:8)
web_1 | at emitErrorAndCloseNT (internal/streams/destroy.js:50:3)
web_1 | at process._tickCallback (internal/process/next_tick.js:63:19)
web_1 | name: 'SequelizeConnectionRefusedError',
web_1 | parent:
web_1 | { Error: connect ECONNREFUSED 172.19.0.3:5432
web_1 | at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1113:14)
web_1 | errno: 'ECONNREFUSED',
web_1 | code: 'ECONNREFUSED',
web_1 | syscall: 'connect',
web_1 | address: '172.19.0.3',
web_1 | port: 5432 },
web_1 | original:
web_1 | { Error: connect ECONNREFUSED 172.19.0.3:5432
web_1 | at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1113:14)
web_1 | errno: 'ECONNREFUSED',
web_1 | code: 'ECONNREFUSED',
web_1 | syscall: 'connect',
web_1 | address: '172.19.0.3',
web_1 | port: 5432 } }
web_1 | { SequelizeConnectionRefusedError: connect ECONNREFUSED 172.19.0.3:5432
web_1 | at connection.connect.err (/app/node_modules/sequelize/lib/dialects/postgres/connection-manager.js:116:24)
web_1 | at Connection.connectingErrorHandler (/app/node_modules/pg/lib/client.js:140:14)
web_1 | at Connection.emit (events.js:182:13)
web_1 | at Socket.reportStreamError (/app/node_modules/pg/lib/connection.js:71:10)
web_1 | at Socket.emit (events.js:182:13)
web_1 | at emitErrorNT (internal/streams/destroy.js:82:8)
web_1 | at emitErrorAndCloseNT (internal/streams/destroy.js:50:3)
web_1 | at process._tickCallback (internal/process/next_tick.js:63:19)
web_1 | name: 'SequelizeConnectionRefusedError',
web_1 | parent:
web_1 | { Error: connect ECONNREFUSED 172.19.0.3:5432
web_1 | at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1113:14)
web_1 | errno: 'ECONNREFUSED',
web_1 | code: 'ECONNREFUSED',
web_1 | syscall: 'connect',
web_1 | address: '172.19.0.3',
web_1 | port: 5432 },
web_1 | original:
web_1 | { Error: connect ECONNREFUSED 172.19.0.3:5432
web_1 | at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1113:14)
web_1 | errno: 'ECONNREFUSED',
web_1 | code: 'ECONNREFUSED',
web_1 | syscall: 'connect',
web_1 | address: '172.19.0.3',
web_1 | port: 5432 } }

最佳答案

第一次更改为 5432 以默认 postgres 端口

db:
image: postgres
restart: always
ports:
- "5432:3306" //default postgres port
environment:
- POSTGRES_USER=username
- POSTGRES_PASSWORD=password
- POSTGRES_DB=addidas
  • 允许你要连接的端口
  • sudo ufw allow 5432

    关于node.js - docker-compose POSTGRES & SEQUELIZE 的数据库连接错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53231511/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com