从终端查询MongoDB数据库:[MongoError]:无法连接到服务器

柔音

我有一个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] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

无法连接到您的数据库服务器

SQL无法连接到数据库服务器

本地服务器无法使用PHP连接到远程Postgresql数据库,但是本地终端可以连接

无法连接到服务器(WAMP,phpmyadmin),将 VS 连接到数据库?

如何通过Macbook上的终端连接到服务器上的MYSQL数据库?

如何连接到PostgreSQL服务器查询数据库名称列表

连接到远程服务器并执行简单的数据库查询时出现问题

Python:如何连接到服务器数据库?

连接到远程mysql数据库服务器

“ psql:无法连接到服务器:连接被拒绝”连接到远程数据库时出错

无法连接到我的AWS数据库实例| psql:无法连接到服务器:操作超时

无法连接到数据库postgres:无法连接到服务器:没有这样的文件或目录

createuser:无法连接到数据库postgres:无法连接到服务器:没有这样的文件或目录

mongodb失败:连接到数据库服务器时出错:没有可访问的服务器

TF246017:Team Foundation服务器无法连接到数据库

我似乎无法从Azure Linux VM连接到MySQL服务器的Azure数据库

错误消息:无法连接到数据库服务器

Elastic Beanstalk 应用程序无法连接到数据库,但服务器可以

无法在docker-compose(php和MariaDB)中连接到数据库服务器

无法使用带有服务器CA验证的TLS连接到AWS数据库

报表服务器无法连接到其数据库

无法使用Hostinger中提供的设置连接到数据库服务器

EF Code First:无法连接到本地 SQL 服务器数据库

Neo4j无法连接到图形数据库服务器

无法在端口 1433 上连接到 SQL Server 数据库服务器

无法从Azure资源连接到Postgres服务器的Azure数据库

无法在程序[VB]中连接到MySQL服务器数据库

无法从Spring Boot Docker容器连接到本地MySQL数据库服务器

SQL Server 2016数据库邮件将无法连接到邮件服务器