• Home
  • Cloud VPS
    • Hong Kong VPS
    • US VPS
  • Dedicated Servers
    • Hong Kong Servers
    • US Servers
    • Singapore Servers
    • Japan Servers
  • Company
    • Contact Us
    • Blog
logo logo
  • Home
  • Cloud VPS
    • Hong Kong VPS
    • US VPS
  • Dedicated Servers
    • Hong Kong Servers
    • US Servers
    • Singapore Servers
    • Japan Servers
  • Company
    • Contact Us
    • Blog
ENEN
  • 简体简体
  • 繁體繁體
Client Area

How to fix MongoDB Error Code – 10107 – NotWritablePrimary

January 2, 2024

How to Fix MongoDB Error Code – 10107 – NotWritablePrimary

MongoDB is a popular open-source NoSQL database that offers high performance, scalability, and flexibility. However, like any software, it can encounter errors that can disrupt its normal operation. One such error is MongoDB Error Code – 10107 – NotWritablePrimary. In this article, we will explore the causes of this error and provide step-by-step solutions to fix it.

Understanding MongoDB Error Code – 10107 – NotWritablePrimary

MongoDB follows a distributed architecture where data is replicated across multiple nodes to ensure high availability and fault tolerance. In this architecture, one node is elected as the primary node, responsible for handling write operations, while the other nodes act as secondary nodes, replicating data from the primary node.

The MongoDB Error Code – 10107 – NotWritablePrimary occurs when a client attempts to write data to a secondary node instead of the primary node. MongoDB restricts write operations to secondary nodes by default to maintain data consistency. This error indicates that the client is trying to write to a secondary node, which is not allowed.

Possible Causes of MongoDB Error Code – 10107 – NotWritablePrimary

There are several reasons why you might encounter this error:

  • Incorrect Connection String: The client application may be using an incorrect connection string that points to a secondary node instead of the primary node.
  • Failover: If the primary node becomes unavailable due to a network issue or a crash, MongoDB automatically elects a new primary node. If the client is not updated with the new primary node’s address, it may continue to send write requests to the old primary node, resulting in the error.
  • Read Preference: The client application may have set the read preference to “secondary” explicitly, instructing MongoDB to send write requests to secondary nodes.

Fixing MongoDB Error Code – 10107 – NotWritablePrimary

To resolve the MongoDB Error Code – 10107 – NotWritablePrimary, follow these steps:

Step 1: Verify Connection String

Double-check the connection string used by the client application. Ensure that it points to the primary node. The connection string should include the hostname or IP address of the primary node and the appropriate port number.

Step 2: Check for Failover

If the primary node has failed or undergone a failover, you need to update the client application with the new primary node’s address. You can use the MongoDB shell or administrative tools to check the current primary node and update the connection string accordingly.

Step 3: Adjust Read Preference

If the client application explicitly sets the read preference to “secondary,” you need to modify it to “primary” or “primaryPreferred.” This change ensures that write operations are directed to the primary node.

Here’s an example of how to modify the read preference using the MongoDB Node.js driver:


const MongoClient = require('mongodb').MongoClient;
const uri = 'mongodb://primary-node:27017,secondary-node:27017/?readPreference=primary';
const client = new MongoClient(uri, { useNewUrlParser: true });

Summary

MongoDB Error Code – 10107 – NotWritablePrimary occurs when a client attempts to write data to a secondary node instead of the primary node. This error can be fixed by verifying the connection string, checking for failover, and adjusting the read preference. If you encounter this error, make sure to follow these steps to ensure proper functioning of your MongoDB deployment.

For reliable and high-performance VPS hosting solutions, consider Server.HK. Our VPS hosting services offer the perfect environment for running MongoDB and other applications, ensuring optimal performance and scalability.

Recent Posts

  • How to Configure SELinux in CentOS Without Breaking Your System (CentOS Stream 9/10 – 2026)
  • Managing Users and Permissions in CentOS Stream: Best Practices (CentOS Stream 9/10 – 2026)
  • How to Set Up Nginx on CentOS Stream for High-Performance Web Hosting
  • CentOS Stream Explained: Key Differences from CentOS Linux
  • How to Configure FirewallD in CentOS Stream: From Essential to Production-Grade

Recent Comments

No comments to show.

Knowledge Base

Access detailed guides, tutorials, and resources.

Live Chat

Get instant help 24/7 from our support team.

Send Ticket

Our team typically responds within 10 minutes.

logo
Alipay Cc-paypal Cc-stripe Cc-visa Cc-mastercard Bitcoin
Cloud VPS
  • Hong Kong VPS
  • US VPS
Dedicated Servers
  • Hong Kong Servers
  • US Servers
  • Singapore Servers
  • Japan Servers
More
  • Contact Us
  • Blog
  • Legal
© 2026 Server.HK | Hosting Limited, Hong Kong | Company Registration No. 77008912
Telegram
Telegram @ServerHKBot