Convert a Standalone Self-Managed mongod to a Replica Set (original) (raw)
A standalone mongod instance is useful for testing and development. A standalone instance isn't a good choice for a production deployment because it can be a single point of failure. A replica set, also known as a cluster, provides redundancy and availability. Always use a replica set in production.
If you have a standalone server with data that you want to use in production, convert the standalone server to a replica set first.
Important
If you convert a development server to a replica set for production use, consult the security checklistbefore you expose your cluster to the internet.
You can easily migrate from a standalone server to a MongoDB Atlas cluster. MongoDB Atlas is the fully managed service for MongoDB deployments in the cloud. To learn more, see Migrate or Import Data in the MongoDB Atlas documentation.
This tutorial uses the following servers:
Hostname | Port | Description |
---|---|---|
mongodb0.example.net | 27017 | A running standalone MongoDB Server with data. |
mongodb1.example.net | 27017 | A new MongoDB Server to join the replica set. |
mongodb2.example.net | 27017 | A new MongoDB Server to join the replica set. |
Before you convert your standalone instance, consider whether areplica set or a sharded cluster is more appropriate for your workload.
A sharded cluster is a special kind of cluster. A sharded cluster provides redundancy and availability; it also distributes data acrossshards. Shards are usually hosted on multiple servers and allow for horizontal scaling.
To use authorization with a replica set, you must also configure replica set members to use X.509 certificates or keyfiles to perform internal authentication.
For more information, see:
- Use X.509 Certificates for Membership Authentication with Self-Managed MongoDB
- Deploy Self-Managed Replica Set With Keyfile Authentication
Use mongosh to connect to your existing mongod
instance:
mongosh "mongodb://mongodb0.example.net:27017"
Switch to the admin
database and run shutdown.
use admin
db.adminCommand(
{
shutdown: 1,
comment: "Convert to cluster"
}
)
Update the configuration file on each server and to set the replSetName setting.
replication:
replSetName: "rs0"
Configure member authentication for each server in the replica set.
Configure the replica set to use X.509 certificates for internal member authentication.
For example:
replication:
replSetName: "rs0"
security:
clusterAuthMode: x509
net:
tls:
mode: requireTLS
certificateKeyFile: /etc/mongodb/client.pem
CAFile: /etc/mongodb/ca.pem
clusterFile: /etc/mongodb/member.pem
Configure the replica set to use keyfiles for internal member authentication. To authenticate, each member must have a copy of the same keyfile.
For example:
replication:
replSetName: "rs0"
security:
keyFile: /etc/mongodb/keyfile
Configures a replica set without authorization.
Warning
You should only use this configuration for internal replica sets that are not accessible through the network.
Setting | Option | Description |
---|---|---|
net.bindIp | --bind_ip | Sets the hostnames or IP addresses that MongoDB listens on for client connections. To block network access to the server, set this option to localhost. |
For example:
replication:
replSetName: "rs0"
net:
bindIp: localhost
To initialize the replica set, use mongosh
to reconnect to your server instance.
mongosh "mongodb://mongodb0.example.net:27017"
Then, run the rs.initiate() method:
You only have to initiate the replica set once.
To view the replica set configuration, use rs.conf().
To check the status of the replica set, use rs.status().
The new replica set has a single, primary node. The next step is to add new nodes to the replica set. Review the documentation on clusters before you add additional nodes:
To add nodes, run the rs.add() method:
rs.add("mongodb1.example.net:27017")
rs.add("mongodb2.example.net:27017")
To check that the replica set is correctly configured, run the rs.status() method and check that the new members are listed in the membersfield:
After you convert the standalone server to a replica set, update the connection string used by your applications to the connection string for your replica set:
mongodb0.example.net:27017
mongodb1.example.net:27017
mongodb2.example.net:27017
mongodb://mongodb0.example.net:27017,mongodb1.example.net:27017,mongodb2.example.net:27017
Then, restart your applications.