本文介绍了Mongo在首次连接时无法连接到服务器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道这是一个讨论的方式,并且已经遍历了我在SO和google上发现的几乎所有与之相关的主题,但是仍然没有解决方案!

I know that's a way discussed issue, and have gone through almost every topic I found on SO and google in general about it, but still no resolution!

问题

在随机的时间点,发生以下错误

At random points in time, the following error occurs

MongoError: failed to connect to server [127.0.0.1:20000] on first connect
    at Pool.<anonymous> (/home/user/node_modules/mongodb-core/lib/topologies/server.js:326:35)
    at emitOne (events.js:96:13)
    at Pool.emit (events.js:189:7)
    at Connection.<anonymous> (/home/user/node_modules/mongodb-core/lib/connection/pool.js:270:12)
    at Object.onceWrapper (events.js:291:19)
    at emitTwo (events.js:106:13)
    at Connection.emit (events.js:192:7)
    at Socket.<anonymous> (/home/user/node_modules/mongodb-core/lib/connection/connection.js:185:10)
    at Object.onceWrapper (events.js:291:19)
    at emitNone (events.js:86:13)

解决方案

杀死该应用程序,重新启动mongod服务(因此.lock文件将被删除),然后重新启动该应用程序.现在,一切运行顺利.显然,这不是一个可以接受的解决方案!

Kill the application, restart the mongod service (so the .lock file is deleted) and restart the application. Now everything runs smoothly. Obviously that's not a accepted solution!

环境和事实

应用程序使用以下代码编写:

The application is written using:

  • ExpressJS框架(版本4.15.2)
  • MongoJS驱动程序(版本2.4.0)
  • NodeJS(版本7.7.3)
  • MongoDB(版本3.4.2)

服务器是运行Centos 7的VPS(OpenVZ).

Server is a VPS (OpenVZ) running Centos 7.

MongoDB是使用推荐的.rpm方法完全按照文档中所述安装的.

MongoDB was installed using the recommended .rpm method exactly as described in the docs.

即使应用程序因上述错误而崩溃,我也可以通过shell连接并发出查询.

Even when the application crashes with the aforementioned error, I'm able to connect and issue queries via shell.

在日志中没有关于该错误或可能导致该错误的内容.唯一的启动警告是关于使用xfs而不是ext4以及关于将MongoDB与OpenVZ一起使用.

There is nothing in the logs about that error or something that may cause it. The only startup warnings are about using xfs instead of ext4 and about using MongoDB with OpenVZ.

对mongod.conf所做的唯一配置更改是侦听端口和启用授权.

The only configuration change made to mongod.conf was the listening port and enabling authorization.

与mongojs一起使用的连接字符串如下

The connection string used with mongojs is the following

var mongojs = require('mongojs');
var db = mongojs('mongodb://username:password@127.0.0.1:20000/database')

寻求帮助的时刻

请大家!帮帮我...这真的很烦人,无法找出是我自己,驱动程序或设置使这搞砸了!

Please guys! Help me out... It's really annoying and can't find out if it's me, the driver or settings that mess this up!

预先感谢

更新

删除了数据库的打包版本并执行了手动安装,但是仍然没有结果!错误仍然存​​在...

Removed the packaged version of the database and performed a manual installation, but still no results! The error persists...

推荐答案

以下是解决我们问题的方法.

Here's what solved our problem.

已更改服务器,并从OpenVZ移出到基于KVM的"VPS" VPS.每个应用程序元素保持相同(应用程序代码/node/npm/mongo/drivers等)和相同的操作系统(仍为Centos 7).

Changed server and moved out of OpenVZ to a KVM "based" VPS.Every application element remained the same (app code/node/npm/mongo/drivers etc), and same OS (still Centos 7).

没有一个线索,但是该应用程序在新服务器上可以正常运行约一周,而不是每隔几分钟或几小时便崩溃...

Not a single clue why, but the app is running perfectly on the new server for about a week instead of crashing every few minutes or hours...

希望这对某人有帮助!

这篇关于Mongo在首次连接时无法连接到服务器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-24 18:42
查看更多