Setup MongoDB replica set locally in docker or with Atlas for Prisma ORM
Tags: prisma, mongodb, backend, database
Published: 2023-04-21 10:56:03


## Overview

Prisma requires MongoDB instance to run as a replica set which is non-trivial to setup locally, although quite easy with Atlas. But if your internet connection is not very good, setting up a local instance becomes mandatory for development

<!--more-->

Why am I writing this article? Because there is just too many ways that you can do it wrong. I found the right way the hard way. If you want a quick solution then you can check my answer on [stackoverflow](https://stackoverflow.com/questions/73570090/prisma-mongodb-replica-set/76043251#76043251). Here I'm going to provide detailed steps with explanation

![Meme](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/1jbw25czwwonam6o189y.png)

[Image source](https://www.linkedin.com/business/sales/blog/customer-stories/ask-the-expert--how-to-sell-to-multiple-decision-makers)

## What is a replica set

A replica set in MongoDB is a group of mongodb processes that handles the same data to provide redundancy

Redundancy increases high availability. In a replica set if one node fails another node can take over to maintain the operations

There is one primary node and other secondary nodes.Here is a diagram that summarizes the interaction between primary and secondary nodes


![Replica Set](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/82zvf61wu1hlw6tcv2jy.png)


If you want to learn more, you can read about it [here](https://www.mongodb.com/docs/manual/replication/)

## Setup Local Instance with Docker

Prisma has published a [docker image](https://hub.docker.com/r/prismagraphql/mongo-single-replica) that creates a single instance replica without additional configuration

- Go to [tags](https://hub.docker.com/r/prismagraphql/mongo-single-replica/tags) and find the latest version available, at this moment it is 5.0.3

- Pull the docker image with

```
docker pull prismagraphql/mongo-single-replica:5.0.3
```

- Run the image with this command

```bash
docker run --name mongo \
      -p 27017:27017 \
      -e MONGO_INITDB_ROOT_USERNAME="monty" \
      -e MONGO_INITDB_ROOT_PASSWORD="pass" \
      -d prismagraphql/mongo-single-replica:5.0.3
```

- Now you have to setup the connection URL, in this case it should resemble this,

```conf
DATABASE_URL="mongodb://monty:pass@localhost:27017/db_name?authSource=admin&directConnection=true"
```

- Replace db_name with the name of your database, if it doesn't exist it will be created automatically

  Notice that the ROOT_USERNAME and authSource is not the same, you can change the ROOT_USERNAME to whatever you like but the authSouce has to admin, as this is the database which contains user credentials

- Finally to test the connection. Run this command inside your project root directory. This will sync your schema to the database

```bash
npx prisma db push
```

## Setup in MongoDB Atlas

1. Create a free account [here](https://www.mongodb.com/cloud/atlas/register)
2. Create new project
3. Click on "Build a Database" button
![Build Database](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/4qr2qexn8ix9uwqztfll.png)
4. Choose the free M0 cluster and provide a name for it.
![Create Cluster](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/6sm4pcjpec9lq4zo4i5e.png)
5. You will prompted to create a user
![Create User](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/srautgru9geg2bzg1jdg.png)
NOTE: You should use an auto generated password for security and note it down somewhere
6. Your IP will be automatically added to the access list, click "Finsh and Close" and go to the database.
![Finish Cluster Creation](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/3ojj5fmbl3g3y4qnq2gq.png)
7. To get the connection url, click "Connect" button and choose "Drivers"
![Connect](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/w5p2mvrxrw3rvo8dhlpu.png)
8. Finally copy the connection url and replace \<password\> with your auto generated password
![Get URL](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/1lspa4zpshfymcaddw6o.png)
9. IMPORTANT : You have to add a database name after the host name. In my case the url looks like this
```conf
DATABASE_URL="mongodb+srv://monty:pass@cluster0.nqtl0pv.mongodb.net/db_name?retryWrites=true&w=majority"
```

Test the connection and sync schema
```bash
npx prisma db push
```

Out of pure love for you guys, I have shut down the cluster I have created so that you don't waste any time trying to get a malware in my database 😎.

Find me on
💻 [Github](https://github.com/renzhamin)
🔘 [LinkedIn](https://www.linkedin.com/in/renzhamin)
⭕ [Twitter](https://twitter.com/renzhamin)