gpt4 book ai didi

mysql - Sequelize 无法从 Node/express 应用程序同步到 docker 容器中的 MySQL (ECONNREFUSED)

转载 作者:搜寻专家 更新时间:2023-11-01 00:49:19 26 4
gpt4 key购买 nike

我第一次同时使用 sequelize、MySQL 和 docker,无法让 sequelize 连接到数据库。

我已经用 DBeaver 测试了数据库的存在,我可以连接。当我通过 DBeaver 连接时,我还可以看到 MySQL 容器在终端中做出响应。但是,当尝试连接代码时,出现 ECONNREFUSED 错误。

我已经使用 sequelize 文档检查了我的代码,并仔细检查了端口设置,但似乎看不出哪里出了问题。

在下面提供了正确答案后,我发现我有一个错误。我错过了 docker-compose.yml 文件中“mysql”容器的以下内容。

networks:
- app-tier

为了避免要求其他用户阅读答案中的评论才能发现这一点,我已将其添加到 docker-compose.yml 文件中,以便问题和答案现在更清楚地对齐

docker-compose.yml 文件:

version: '3'

networks:
app-tier:
driver: bridge
services:
server:
image: bitnami/node
networks:
- app-tier
command: "sh -c 'npm install && npm run dev'"
volumes:
- ./server:/app
ports:
- 5000:5000
depends_on:
- mysql
mysql:
image: 'bitnami/mysql:latest'
environment:
- MYSQL_USER=root
- MYSQL_PASSWORD=password
- MYSQL_ROOT_PASSWORD=password
- MYSQL_DATABASE=school
networks:
- app-tier
ports:
- '3306:3306'
volumes:
- ./db:/bitnami/mysql/data

后续连接:

const Sequelize = require('sequelize');

const sequelize = new Sequelize('school', 'root', 'password', {
dialect: 'mysql'
});

module.exports = sequelize;

尝试从 app.js 同步:

sequelize.sync()
.then(result => console.log(result))
.catch(err => console.log('EEEERRRROOOOOOR',err));

终端输出:

server_1    |  { SequelizeConnectionRefusedError: connect ECONNREFUSED 127.0.0.1:3306
server_1 | at Utils.Promise.tap.then.catch.err (/app/node_modules/sequelize/lib/dialects/mysql/connection-manager.js:139:19)
server_1 | at tryCatcher (/app/node_modules/bluebird/js/release/util.js:16:23)
server_1 | at Promise._settlePromiseFromHandler (/app/node_modules/bluebird/js/release/promise.js:512:31)
server_1 | at Promise._settlePromise (/app/node_modules/bluebird/js/release/promise.js:569:18)
server_1 | at Promise._settlePromise0 (/app/node_modules/bluebird/js/release/promise.js:614:10)
server_1 | at Promise._settlePromises (/app/node_modules/bluebird/js/release/promise.js:689:18)
server_1 | at Async._drainQueue (/app/node_modules/bluebird/js/release/async.js:133:16)
server_1 | at Async._drainQueues (/app/node_modules/bluebird/js/release/async.js:143:10)
server_1 | at Immediate.Async.drainQueues (/app/node_modules/bluebird/js/release/async.js:17:14)
server_1 | at runCallback (timers.js:810:20)
server_1 | at tryOnImmediate (timers.js:768:5)
server_1 | at processImmediate [as _immediateCallback] (timers.js:745:5)
server_1 | name: 'SequelizeConnectionRefusedError',
server_1 | parent:
server_1 | { Error: connect ECONNREFUSED 127.0.0.1:3306
server_1 | at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1191:14)
server_1 | errno: 'ECONNREFUSED',
server_1 | code: 'ECONNREFUSED',
server_1 | syscall: 'connect',
server_1 | address: '127.0.0.1',
server_1 | port: 3306,
server_1 | fatal: true },
server_1 | original:
server_1 | { Error: connect ECONNREFUSED 127.0.0.1:3306
server_1 | at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1191:14)
server_1 | errno: 'ECONNREFUSED',
server_1 | code: 'ECONNREFUSED',
server_1 | syscall: 'connect',
server_1 | address: '127.0.0.1',
server_1 | port: 3306,
server_1 | fatal: true } }

最佳答案

要从 docker 容器连接到您的 mysql 服务器,您需要传递一个正确的服务地址。所以你的初始化应该是这样的。

const Sequelize = require('sequelize');

const sequelize = new Sequelize('school', 'root', 'password', {
host: 'mysql',
dialect: 'mysql'
});

module.exports = sequelize;

关于mysql - Sequelize 无法从 Node/express 应用程序同步到 docker 容器中的 MySQL (ECONNREFUSED),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54639183/

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