No primary server available
Problem
I have an issue that is rather difficult to debug, and was wondering if anyone sees anything wrong with my configuration. [code block] Nodejs version `4.2.1` and mongoDB version `3.0.7` with mongoose `4.2.8`. This seems to happen randomly and will open many connection until I finally restart the node process. The cluster is healthy at all times during this error. This error happens hundreds of times per hour. There does not seem to be any consistency as to when the error will begin. For example, it occurs when the cluster is operating normally and no changes to the primary have been made. This is what the db stats look like. As you can see the number of connections will steadily increase. If I kill the node process and start a new one everything is fine. <img width="1145" alt="screen shot 2015-11-30 at 5 21 01 pm" src="https://cloud.githubusercontent.com/assets/2230570/11489777/36443d10-9787-11e5-98fe-44abe0e49cb8.png"> Config [code block] Connection String [code block] Stack trace [code block]
Error Output
Error no primary server available
Unverified for your environment
Select your OS to check compatibility.
1 Fix
Implement Connection Pooling and Error Handling for MongoDB
The error 'no primary server available' occurs when the MongoDB driver cannot find a primary node in the replica set to handle write operations. This can be caused by connection leaks, improper handling of connection errors, or exceeding the maximum number of allowed connections. Given the Node.js and MongoDB versions in use, it's likely that the connection pool is not being managed effectively, leading to a gradual increase in connections until the process is restarted.
Awaiting Verification
Be the first to verify this fix
- 1
Enable Connection Pooling
Modify the Mongoose connection options to enable connection pooling and set the maximum number of connections. This helps manage connections more efficiently.
javascriptmongoose.connect('mongodb://yourMongoDBUri', { useNewUrlParser: true, useUnifiedTopology: true, poolSize: 10 }); - 2
Implement Error Handling
Add error handling to the Mongoose connection to properly handle connection errors and log them for debugging. This will help identify issues before they lead to the 'no primary server available' error.
javascriptmongoose.connection.on('error', err => { console.error('MongoDB connection error:', err); }); - 3
Monitor Connection Status
Set up monitoring for the connection status to detect when the connection to the primary server is lost. This can help in taking proactive measures before the application runs out of connections.
javascriptmongoose.connection.on('disconnected', () => { console.warn('MongoDB disconnected!'); }); - 4
Increase Timeout Settings
Adjust the server selection timeout and socket timeout settings to allow more time for the driver to find a primary server during transient network issues.
javascriptmongoose.connect('mongodb://yourMongoDBUri', { serverSelectionTimeoutMS: 5000, socketTimeoutMS: 45000 }); - 5
Upgrade Dependencies
Consider upgrading Node.js, MongoDB, and Mongoose to their latest stable versions to benefit from performance improvements and bug fixes.
bashnpm install mongoose@latest; # Upgrade Mongoose to the latest version
Validation
Monitor the application logs for the 'no primary server available' error after implementing these changes. Additionally, check the number of active connections to the MongoDB server to ensure they are being managed within the defined limits. If the error persists, further investigation into the MongoDB cluster configuration may be necessary.
Sign in to verify this fix
Environment
Submitted by
Alex Chen
2450 rep