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

Node.js 从终端查询MongoDB数据库:[MongoError]:无法连接到服务器,node.js,mongodb,mongoose,Node.js,Mongodb,Mongoose,我有一个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,

我有一个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);
});
有人知道如何解决这个问题吗?我是否需要使用例如MongoClient

PS:我可以通过Robo 3T通过以下设置查询数据库:

编辑1:
sshtie@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
(基本)绑定MBP:kpi承铁$sshtie@178.62.87.72-nNT-v-L 27014:localhost:27017
OpenSSH_7.9p1,LibreSSL 2.7.3
debug1:读取配置数据/etc/ssh/ssh\u config
debug1:/etc/ssh/ssh_config第48行:应用*
debug1:连接到178.62.87.72[178.62.87.72]端口22。
debug1:已建立连接。
debug1:identity file/Users/chengtie/.ssh/id\u rsa类型0
debug1:identity file/Users/chengtie/.ssh/id\u rsa-cert类型-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:本地版本字符串SSH-2.0-OpenSSH_7.9
debug1:远程协议版本2.0,远程软件版本OpenSSH_6.6.1p1 Ubuntu-2ubuntu2.13
debug1:match:OpenSSH_6.6.1p1 Ubuntu-2ubuntu2.13 pat OpenSSH_6.6.1*compat 0x04000002
debug1:验证178.62.87.72:22为“平局”
debug1:SSH2\u MSG\u KEXINIT已发送
debug1:SSH2\u MSG\u KEXINIT已收到
debug1:kex:算法:curve25519-sha256@libssh.org
debug1:kex:主机密钥算法:ecdsa-sha2-nistp256
debug1:kex:server->client cipher:chachacha20-poly1305@openssh.comMAC:压缩:无
debug1:kex:client->server cipher:chachacha20-poly1305@openssh.comMAC:压缩:无
debug1:需要SSH2\u MSG\u KEX\u ECDH\u回复
debug1:服务器主机密钥:ecdsa-sha2-nistp256 SHA256:z8rrvKqvVmSxKFlCnb49HwUTQ/ABB9DUi9A3DUIpKtI
debug1:主机“178.62.87.72”已知,并且与ECDSA主机密钥匹配。
debug1:在/Users/chengtie/.ssh/known_hosts中找到密钥:1
debug1:在134217728块之后重新键入
debug1:SSH2\u MSG\u已发送新密钥
debug1:应为SSH2\u MSG\u NEWKEYS
debug1:SSH2\u MSG\u接收到新密钥
debug1:在134217728块之后重新键入
debug1:将尝试密钥:/Users/chengtie/.ssh/id_rsa rsa SHA256:el7hm5lvdvadziv662nedeeolky+etj8OT61eugu4Y
debug1:将尝试键:/Users/chengtie/.ssh/id\u
debug1:将尝试键:/Users/chengtie/.ssh/id\u ecdsa
debug1:将尝试键:/Users/chengtie/.ssh/id_ed25519
debug1:将尝试键:/Users/chengtie/.ssh/id\u xmss
debug1:SSH2\u消息\u服务\u接收
debug1:可以继续的身份验证:公钥、密码
debug1:下一个身份验证方法:公钥
debug1:提供公钥:/Users/chengtie/.ssh/id_rsa rsa SHA256:el7hm5lvdvadziv662nedeeolky+etj8OT61eugu4Y
debug1:可以继续的身份验证:公钥、密码
debug1:正在尝试私钥:/Users/chengtie/.ssh/id\u dsa
debug1:正在尝试私钥:/Users/chengtie/.ssh/id\u ecdsa
debug1:正在尝试私钥:/Users/chengtie/.ssh/id_ed25519
debug1:正在尝试私钥:/Users/chengtie/.ssh/id\u xmss
debug1:下一个身份验证方法:密码
tie@178.62.87.72的密码:
debug1:身份验证成功(密码)。
经认证为178.62.87.72([178.62.87.72]:22)。
debug1:到LOCALHOST:27014的本地连接已转发到远程地址LOCALHOST:27017
debug1:本地转发侦听::1端口27014。
调试1:通道0:新建[端口侦听器]
debug1:在127.0.0.1端口27014上侦听本地转发。
debug1:通道1:新建[端口侦听器]
debug1:不再请求-sessions@openssh.com
debug1:进入交互式会话。
debug1:保证:网络

您的代码看起来不错,我们还从
ECONNREFUSED
服务器或英语连接拒绝服务器收到了响应。这可能是由几个不同的原因造成的。最常见的是防火墙/IP问题(这不是您的情况,因为您使用
robo3T
进行连接)

如果仔细查看发布的第二幅图像,在
ssh
选项卡上可以看到一个名为
Use ssh Tunnel
的框。这是您无法通过代码连接的地方

打开一个新的终端选项卡并运行如下操作:

ssh user@178.62.87.72 -L [LOCAL_PORT]:localhost:27017
其中,
LOCAL\u PORT
可以是本地主机上任何打开的端口。这将创建一个开放的“隧道”,即您的机器和远程机器。这就是Mongo与您沟通的方式

然后将您的连接代码更改为:

const CONN = 'mongodb://127.0.0.1:LOCAL_PORT'

您可以继续使用27017作为您的
本地\u端口
,但通常人们在该端口上有一个正在运行的本地Mongo进程。

您的代码看起来不错,我们还收到了
EconRefused
服务器或英语连接拒绝的响应。这可能是由几个不同的原因造成的。最常见的是防火墙/IP问题(这不是您的情况,因为您使用
robo3T
进行连接)

如果仔细查看发布的第二幅图像,在
ssh
选项卡上可以看到一个名为
Use ssh Tunnel
的框。这是您无法通过代码连接的地方

打开一个新的终端选项卡并运行如下操作:

ssh user@178.62.87.72 -L [LOCAL_PORT]:localhost:27017
其中,
LOCAL\u PORT
可以是本地主机上任何打开的端口。这将创建一个开放的“隧道”,即您的机器和远程机器。这就是Mongo与您沟通的方式

然后将您的连接代码更改为:

const CONN = 'mongodb://127.0.0.1:LOCAL_PORT'

您可以继续使用27017作为您的
本地\u端口
,但通常人们在该端口上有一个正在运行的本地Mongo进程。

您有
ssh隧道
设置吗?我可以使用
sshtie@178.62.87.72