# How to copy and store data with MongoDB Replica Set

We recommend using MongoDB Replica Set to protect yourself against server outages and other potential problems that could affect data retrieval. This method allows you to store copies of your data on different servers and instantly retrieve the copied data whenever you need it.

## What is a MongoDB Replica Set?

The primary aim of a [database management system](https://www.ionos.com/digitalguide/hosting/technical-matters/database-management-systems-dbms/) such as [MongoDB](https://www.ionos.com/digitalguide/websites/web-development/mongodb-an-introduction-and-comparison-to-mysql/) is to organize your data in a clear manner and ensure it is stored securely. An important MongoDB tool to help you to protect your data sets is MongoDB Replica Set. When you use this, exact copies of your data sets are created and then distributed across several nodes on different servers. It’s important to note that MongoDB differentiates between primary and secondary nodes.

- **Primary**: The primary node is also referred to as the master node and is the starting point for setting up MongoDB Replica Sets. The primary node has reading and writing rights.
- **Secondary**: Secondary nodes are exact copies of the master node. They are only used to store the copied data and, as a rule, do not have reading and writing rights. They are only used to replace the master in the event of an outage in the primary node. We recommend that you create an odd number of secondary nodes.

## How does the replica set work?

A MongoDB Replica Set is made up of a primary node and multiple secondary nodes, which are exact copies of the master. While there should be at least two secondary nodes, we recommend that you have at least three. Data transfers always occur **exclusively from the primary node to the secondaries**. If there is an outage in the primary node due to an error, a system failure or other necessary maintenance work, the secondary nodes will seamlessly take over from the master. This ensures that the data is still available during the time of the outage. If a defective node can be repaired, it will become a secondary node.

[![Image: An example image of the setup of one primary and three secondary nodes](https://www.ionos.com/digitalguide/fileadmin/_processed_/c/0/csm_an-example-image-of-the-setup-of-one-primary-and-three-secondary-nodes_5dc8cfb73e.webp "An example image of the setup of one primary and three secondary nodes")](https://www.ionos.com/digitalguide/fileadmin/DigitalGuide/Screenshots_2022/an-example-image-of-the-setup-of-one-primary-and-three-secondary-nodes.png) The data is always transferred from the primary to secondary nodes.       ##  What are the advantages of a replica set?

Using a MongoDB Replica Set has many advantages. First and foremost, it is highly recommended for security purposes. If a server experiences a temporary or permanent outage, you will still be able to access the data. Another advantage of having data replicated in a secondary node is the ability to continuously access it when maintenance work is being performed. Additionally, you don’t need to worry about data getting lost. With replica sets, **outages and server problems** will no longer be an issue for you.

## How to create a MongoDB Replica Set

The third biggest advantage of a MongoDB Replica Set is how easy it is to use. You can easily create a MongoDB Replica Set and ensure the security of your data. The basic syntax of the [MongoDB command](https://www.ionos.com/digitalguide/websites/web-development/mongodb-commands/ "MongoDB commands") *--replSet* looks like this:

```none
mongod --port "PORT" --dbpath "YOUR_DB_DATA_PATH" --replSet "NAME_OF_REPLICASET”
```

If you want to create a MongoDB Replica Set, simply do the following:

1. First, close all active MongoDB servers.
2. Enter the following command. In our example, we have chosen the following specifications:

```none
mongo --port 32014 --dbpath "D:\set up\mongodb\data" --replSet Example
```

This creates a mongod-instance with the name “Example” on port 32014.

3. Now you can connect to the instance.
4. Use the *rs.initiate()* command to start a new MongoDB Replica Set.
5. By using the *rs.conf()* command, you can configure the MongoDB Replica Set. You can also check its status with *rs.status()*.
6. If you want to add users to your MongoDB Replica Set, you can do so using the *rs.add()* command. This syntax looks as follows:

```none
rs.add(HOST_NAME:PORT)
```

For our example, it looks like this:

```none
>rs.add("example1.net:32014")
```

---

### Tip

Use our [MongoDB tutorial](https://www.ionos.com/digitalguide/websites/web-development/mongodb-tutorial-first-steps/) to learn the basics of the DBMS.

---


This is a markdown version of: [https://www.ionos.com/digitalguide/websites/web-development/mongodb-replica-set/](https://www.ionos.com/digitalguide/websites/web-development/mongodb-replica-set/) for AI/LLM consumption.