我有一个Web应用程序和一个MongoDB数据库。现在,我只想编写脚本以从终端查询数据库。我写了下面的代码。
const userModel = require('../models/Users');
const mongoose = require('mongoose');
const CONN = 'mongodb://tie:<password>@178.62.87.72:27017/news'
const option = {
socketTimeoutMS: 30000,
keepAlive: true,
reconnectTries: 30000,
// useNewUrlParser: true
};
mongoose.connect(CONN, option);
mongoose.connection.on('connected', async function () {
console.log('Database connected: ' + CONN);
try {
let x = await userModel.user.find({ "_id": "5a95bbc0763e1f4bad37b864" }).lean()
console.log(x)
mongoose.connection.close()
console.log("done");
} catch (error) {
console.log(error);
}
});
mongoose.connection.on('disconnected', function () {
console.log('Database disconnected:' + CONN);
});
然后,node kpi.js
在终端中返回一个错误:
events.js:200
throw er; // Unhandled 'error' event
^
Error [MongoError]: failed to connect to server [178.62.87.72:27017] on first connect [Error: connect ECONNREFUSED 178.62.87.72:27017
at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1134:16) {
name: 'MongoError',
message: 'connect ECONNREFUSED 178.62.87.72:27017'
}]
at Pool.<anonymous> (/Users/chengtie/Startup/PRODSERVER/From202005/funfun/node_modules/mongodb-core/lib/topologies/server.js:336:35)
at Pool.emit (events.js:223:5)
at Connection.<anonymous> (/Users/chengtie/Startup/PRODSERVER/From202005/funfun/node_modules/mongodb-core/lib/connection/pool.js:280:12)
at Object.onceWrapper (events.js:313:26)
at Connection.emit (events.js:223:5)
at Socket.<anonymous> (/Users/chengtie/Startup/PRODSERVER/From202005/funfun/node_modules/mongodb-core/lib/connection/connection.js:189:49)
at Object.onceWrapper (events.js:313:26)
at Socket.emit (events.js:223:5)
at emitErrorNT (internal/streams/destroy.js:92:8)
at emitErrorAndCloseNT (internal/streams/destroy.js:60:3)
at processTicksAndRejections (internal/process/task_queues.js:81:21)
Emitted 'error' event on NativeConnection instance at:
at Immediate.<anonymous> (/Users/chengtie/Startup/PRODSERVER/From202005/funfun/node_modules/mongoose/lib/connection.js:325:19)
at processImmediate (internal/timers.js:439:21) {
name: 'MongoError',
message: 'failed to connect to server [178.62.87.72:27017] on first connect [Error: connect ECONNREFUSED 178.62.87.72:27017\n' +
' at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1134:16) {\n' +
" name: 'MongoError',\n" +
" message: 'connect ECONNREFUSED 178.62.87.72:27017'\n" +
'}]'
}
有谁知道如何解决这个问题?我是否需要使用例如MongoClient?
PS:我可以使用以下设置通过Robo 3T查询数据库:
编辑1: ssh [email protected] -nNT -v -L 27014:localhost:27017
返回:
(base) Ties-MBP:kpi chengtie$ ssh [email protected] -nNT -v -L 27014:localhost:27017
OpenSSH_7.9p1, LibreSSL 2.7.3
debug1: Reading configuration data /etc/ssh/ssh_config
debug1: /etc/ssh/ssh_config line 48: Applying options for *
debug1: Connecting to 178.62.87.72 [178.62.87.72] port 22.
debug1: Connection established.
debug1: identity file /Users/chengtie/.ssh/id_rsa type 0
debug1: identity file /Users/chengtie/.ssh/id_rsa-cert type -1
debug1: identity file /Users/chengtie/.ssh/id_dsa type -1
debug1: identity file /Users/chengtie/.ssh/id_dsa-cert type -1
debug1: identity file /Users/chengtie/.ssh/id_ecdsa type -1
debug1: identity file /Users/chengtie/.ssh/id_ecdsa-cert type -1
debug1: identity file /Users/chengtie/.ssh/id_ed25519 type -1
debug1: identity file /Users/chengtie/.ssh/id_ed25519-cert type -1
debug1: identity file /Users/chengtie/.ssh/id_xmss type -1
debug1: identity file /Users/chengtie/.ssh/id_xmss-cert type -1
debug1: Local version string SSH-2.0-OpenSSH_7.9
debug1: Remote protocol version 2.0, remote software version OpenSSH_6.6.1p1 Ubuntu-2ubuntu2.13
debug1: match: OpenSSH_6.6.1p1 Ubuntu-2ubuntu2.13 pat OpenSSH_6.6.1* compat 0x04000002
debug1: Authenticating to 178.62.87.72:22 as 'tie'
debug1: SSH2_MSG_KEXINIT sent
debug1: SSH2_MSG_KEXINIT received
debug1: kex: algorithm: [email protected]
debug1: kex: host key algorithm: ecdsa-sha2-nistp256
debug1: kex: server->client cipher: [email protected] MAC: <implicit> compression: none
debug1: kex: client->server cipher: [email protected] MAC: <implicit> compression: none
debug1: expecting SSH2_MSG_KEX_ECDH_REPLY
debug1: Server host key: ecdsa-sha2-nistp256 SHA256:z8rrvKqvVmSxKFlCnb49HwUTQ/ABB9DUi9A3DUIpKtI
debug1: Host '178.62.87.72' is known and matches the ECDSA host key.
debug1: Found key in /Users/chengtie/.ssh/known_hosts:1
debug1: rekey after 134217728 blocks
debug1: SSH2_MSG_NEWKEYS sent
debug1: expecting SSH2_MSG_NEWKEYS
debug1: SSH2_MSG_NEWKEYS received
debug1: rekey after 134217728 blocks
debug1: Will attempt key: /Users/chengtie/.ssh/id_rsa RSA SHA256:EL7hm5LvdVADZiv662nneDEeoLKy+etj8OT61eugu4Y
debug1: Will attempt key: /Users/chengtie/.ssh/id_dsa
debug1: Will attempt key: /Users/chengtie/.ssh/id_ecdsa
debug1: Will attempt key: /Users/chengtie/.ssh/id_ed25519
debug1: Will attempt key: /Users/chengtie/.ssh/id_xmss
debug1: SSH2_MSG_SERVICE_ACCEPT received
debug1: Authentications that can continue: publickey,password
debug1: Next authentication method: publickey
debug1: Offering public key: /Users/chengtie/.ssh/id_rsa RSA SHA256:EL7hm5LvdVADZiv662nneDEeoLKy+etj8OT61eugu4Y
debug1: Authentications that can continue: publickey,password
debug1: Trying private key: /Users/chengtie/.ssh/id_dsa
debug1: Trying private key: /Users/chengtie/.ssh/id_ecdsa
debug1: Trying private key: /Users/chengtie/.ssh/id_ed25519
debug1: Trying private key: /Users/chengtie/.ssh/id_xmss
debug1: Next authentication method: password
[email protected]'s password:
debug1: Authentication succeeded (password).
Authenticated to 178.62.87.72 ([178.62.87.72]:22).
debug1: Local connections to LOCALHOST:27014 forwarded to remote address localhost:27017
debug1: Local forwarding listening on ::1 port 27014.
debug1: channel 0: new [port listener]
debug1: Local forwarding listening on 127.0.0.1 port 27014.
debug1: channel 1: new [port listener]
debug1: Requesting [email protected]
debug1: Entering interactive session.
debug1: pledge: network
您的代码看起来不错,并且我们也收到来自ECONNREFUSED
或的英语连接被拒绝的服务器的响应。这可能是由几种不同的原因引起的。最常见的是防火墙/ IP问题(这不是您的情况,因为您设法使用来连接robo3T
)。
如果您仔细查看发布的第二张图像,则该ssh
选项卡上会看到一个名为Use SSH Tunnel
标记的框。这就是您无法通过代码进行连接的地方。
打开一个新的终端选项卡,然后运行以下命令:
ssh [email protected] -L [LOCAL_PORT]:localhost:27017
LOCAL_PORT
本地主机上的任何开放端口都可以在哪里。这将创建一个开放的“隧道”,即您的计算机和远程计算机。这就是Mongo与您交流的方式。
然后将您的连接代码更改为此:
const CONN = 'mongodb://127.0.0.1:LOCAL_PORT'
您可以继续使用27017,因为您LOCAL_PORT
通常可以在该端口上运行本地Mongo进程。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句