gpt4 book ai didi

node.js - Sequelize 仅与本地主机连接

转载 作者:行者123 更新时间:2023-12-03 22:26:42 26 4
gpt4 key购买 nike

我正在将 Sequelize 与 nodejs 代码集成,以下是我的 Sequelize 配置:

'use strict';

const Sequelize = require('sequelize');
const config = require('../config');

const sequelize = new Sequelize(config.mysql.database,
config.mysql.username,
config.mysql.password, {
host: 'example.amazonaws.com',
dialect: config.mysql.dialect,
port: config.mysql.pool,
pool: { max: config.mysql.pool.max, min: config.mysql.pool.min, acquire: 1000000, idle: 10000 }
});
sequelize
.authenticate()
.then(() => {
console.log('Connection has been established successfully.');
})
.catch(err => {
debugger;
console.error('Unable to connect to the database:', err);
});

module.exports = sequelize

我的 config.js 看起来像这样:
module.exports = {
postgres: {
username: "root",
password: "secret",
database: "secret",
host: "example.amazonaws.com",
port: "5439",
dialect: "pg",
operatorsAliases: false,
pool: {
max: 5,
min: 0,
acquire: 30000,
idle: 10000
}
},
mysql: {
username: "root",
password: "secret",
database: "secret",
host: "example.amazonaws.com",
port: "3306",
dialect: "mysql",
operatorsAliases: false,
pool: {
max: 20,
min: 5,
acquire: 30000,
idle: 10000
}
}
}

但是我在连接时遇到以下错误:-
< Unable to connect to the database: { SequelizeConnectionError: connect EADDRNOTAVAIL 127.0.0.1 - Local (0.0.0.0:61300)
< at Promise.tap.then.catch.err (/Users/atul/Documents/Workspace/lamda/dealer-rating/node_modules/sequelize/lib/dialects/mysql/connection-manager.js:133:19)
< at tryCatcher (/Users/atul/Documents/Workspace/lamda/dealer-rating/node_modules/bluebird/js/release/util.js:16:23)
< at Promise._settlePromiseFromHandler (/Users/atul/Documents/Workspace/lamda/dealer-rating/node_modules/bluebird/js/release/promise.js:517:31)
< at Promise._settlePromise (/Users/atul/Documents/Workspace/lamda/dealer-rating/node_modules/bluebird/js/release/promise.js:574:18)
< at Promise._settlePromise0 (/Users/atul/Documents/Workspace/lamda/dealer-rating/node_modules/bluebird/js/release/promise.js:619:10)
< at Promise._settlePromises (/Users/atul/Documents/Workspace/lamda/dealer-rating/node_modules/bluebird/js/release/promise.js:695:18)
< at _drainQueueStep (/Users/atul/Documents/Workspace/lamda/dealer-rating/node_modules/bluebird/js/release/async.js:138:12)
< at _drainQueue (/Users/atul/Documents/Workspace/lamda/dealer-rating/node_modules/bluebird/js/release/async.js:131:9)
< at Async._drainQueues (/Users/atul/Documents/Workspace/lamda/dealer-rating/node_modules/bluebird/js/release/async.js:147:5)
< at Immediate.Async.drainQueues [as _onImmediate] (/Users/atul/Documents/Workspace/lamda/dealer-rating/node_modules/bluebird/js/release/async.js:17:14)
< at runCallback (timers.js:705:18)
< at tryOnImmediate (timers.js:676:5)
< at processImmediate (timers.js:658:5)
< name: 'SequelizeConnectionError',
< parent:
< { Error: connect EADDRNOTAVAIL 127.0.0.1 - Local (0.0.0.0:61300)
< at internalConnect (net.js:872:16)
< at defaultTriggerAsyncIdScope (internal/async_hooks.js:294:19)
< at GetAddrInfoReqWrap.emitLookup [as callback] (net.js:1019:9)
< at GetAddrInfoReqWrap.onlookup [as oncomplete] (dns.js:61:10)
< errno: 'EADDRNOTAVAIL',
< code: 'EADDRNOTAVAIL',
< syscall: 'connect',
< address: '127.0.0.1',
< fatal: true },
< original:
< { Error: connect EADDRNOTAVAIL 127.0.0.1 - Local (0.0.0.0:61300)
< at internalConnect (net.js:872:16)
< at defaultTriggerAsyncIdScope (internal/async_hooks.js:294:19)
< at GetAddrInfoReqWrap.emitLookup [as callback] (net.js:1019:9)
< at GetAddrInfoReqWrap.onlookup [as oncomplete] (dns.js:61:10)
< errno: 'EADDRNOTAVAIL',
< code: 'EADDRNOTAVAIL',
< syscall: 'connect',
< address: '127.0.0.1',
< fatal: true } }
< { SequelizeConnectionError: connect EADDRNOTAVAIL 127.0.0.1 - Local (0.0.0.0:61303)
< at Promise.tap.then.catch.err (/Users/atul/Documents/Workspace/lamda/dealer-rating/node_modules/sequelize/lib/dialects/mysql/connection-manager.js:133:19)
< at tryCatcher (/Users/atul/Documents/Workspace/lamda/dealer-rating/node_modules/bluebird/js/release/util.js:16:23)
< at Promise._settlePromiseFromHandler (/Users/atul/Documents/Workspace/lamda/dealer-rating/node_modules/bluebird/js/release/promise.js:517:31)
< at Promise._settlePromise (/Users/atul/Documents/Workspace/lamda/dealer-rating/node_modules/bluebird/js/release/promise.js:574:18)
< at Promise._settlePromise0 (/Users/atul/Documents/Workspace/lamda/dealer-rating/node_modules/bluebird/js/release/promise.js:619:10)
< at Promise._settlePromises (/Users/atul/Documents/Workspace/lamda/dealer-rating/node_modules/bluebird/js/release/promise.js:695:18)
< at _drainQueueStep (/Users/atul/Documents/Workspace/lamda/dealer-rating/node_modules/bluebird/js/release/async.js:138:12)
< at _drainQueue (/Users/atul/Documents/Workspace/lamda/dealer-rating/node_modules/bluebird/js/release/async.js:131:9)
< at Async._drainQueues (/Users/atul/Documents/Workspace/lamda/dealer-rating/node_modules/bluebird/js/release/async.js:147:5)
< at Immediate.Async.drainQueues [as _onImmediate] (/Users/atul/Documents/Workspace/lamda/dealer-rating/node_modules/bluebird/js/release/async.js:17:14)
< at runCallback (timers.js:705:18)
< at tryOnImmediate (timers.js:676:5)
< at processImmediate (timers.js:658:5)
< name: 'SequelizeConnectionError',
< parent:
< { Error: connect EADDRNOTAVAIL 127.0.0.1 - Local (0.0.0.0:61303)
< at internalConnect (net.js:872:16)
< at defaultTriggerAsyncIdScope (internal/async_hooks.js:294:19)
< at GetAddrInfoReqWrap.emitLookup [as callback] (net.js:1019:9)
< at GetAddrInfoReqWrap.onlookup [as oncomplete] (dns.js:61:10)
< errno: 'EADDRNOTAVAIL',
< code: 'EADDRNOTAVAIL',
< syscall: 'connect',
< address: '127.0.0.1',
< fatal: true },
< original:
< { Error: connect EADDRNOTAVAIL 127.0.0.1 - Local (0.0.0.0:61303)
< at internalConnect (net.js:872:16)
< at defaultTriggerAsyncIdScope (internal/async_hooks.js:294:19)
< at GetAddrInfoReqWrap.emitLookup [as callback] (net.js:1019:9)
< at GetAddrInfoReqWrap.onlookup [as oncomplete] (dns.js:61:10)
< errno: 'EADDRNOTAVAIL',
< code: 'EADDRNOTAVAIL',
< syscall: 'connect',
< address: '127.0.0.1',
< fatal: true } }

我已经检查了所有内容,但没有找到任何指向本地主机的内容。即使我检查了配置,这也是正确的。但是每次它尝试使用本地主机而不是 rds 时。

我猜 mysql2 或 sequelize 的版本有问题。

最佳答案

您也可以使用 URL 字符串。

describe('Instantiation with a URL string', () => {
it('should accept username, password, host, port, and database', () => {
const sequelize = new Sequelize('mysql://user:pass@example.com:9821/dbname');
const config = sequelize.config;
const options = sequelize.options;

expect(options.dialect).to.equal('mysql');

expect(config.database).to.equal('dbname');
expect(config.host).to.equal('example.com');
expect(config.username).to.equal('user');
expect(config.password).to.equal('pass');
expect(config.port).to.equal('9821');
});
});

如需进一步配置,请查看 sequelize 的单元测试详情。

configuration unit tests

关于node.js - Sequelize 仅与本地主机连接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57571262/

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