问题
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!
The problem
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)
The solution
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 environment and the facts
The application is written using:
- ExpressJS framework (version 4.15.2)
- MongoJS driver (version 2.4.0)
- NodeJS (version 7.7.3)
- MongoDB (version 3.4.2)
Server is a VPS (OpenVZ) running Centos 7.
MongoDB was installed using the recommended .rpm
method exactly as described in the docs.
Even when the application crashes with the aforementioned error, I'm able to connect and issue queries via shell.
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.
The only configuration change made to mongod.conf was the listening port and enabling authorization.
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')
The cry for help moment
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!
Thanks in advance
UPDATE
Removed the packaged version of the database and performed a manual installation, but still no results! The error persists...
回答1:
Here's what solved our problem.
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...
Hope this helps someone!
来源:https://stackoverflow.com/questions/42946340/mongo-failed-to-connect-to-server-on-first-connect