У меня есть веб-приложение и база данных 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 tie@178.62.87.72 -nNT -v -L 27014:localhost:27017
возвращено:
(base) Ties-MBP:kpi chengtie$ ssh tie@178.62.87.72 -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: curve25519-sha256@libssh.org
debug1: kex: host key algorithm: ecdsa-sha2-nistp256
debug1: kex: server->client cipher: chacha20-poly1305@openssh.com MAC: <implicit> compression: none
debug1: kex: client->server cipher: chacha20-poly1305@openssh.com 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
tie@178.62.87.72'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 no-more-sessions@openssh.com
debug1: Entering interactive session.
debug1: pledge: network