Setup MongoDB replica set locally in docker or with Atlas for Prisma ORM

renzhamin

renzhamin

Posted on April 21, 2023

Setup MongoDB replica set locally in docker or with Atlas for Prisma ORM

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

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. Here I'm going to provide detailed steps with explanation

Meme

Image source

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

If you want to learn more, you can read about it here

Setup Local Instance with Docker

Prisma has published a docker image that creates a single instance replica without additional configuration

  • Go to 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


Enter fullscreen mode Exit fullscreen mode
  • Run the image with this command


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


Enter fullscreen mode Exit fullscreen mode
  • Now you have to setup the connection URL, in this case it should resemble this,


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


Enter fullscreen mode Exit fullscreen mode
  • 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


npx prisma db push


Enter fullscreen mode Exit fullscreen mode

Setup in MongoDB Atlas

  1. Create a free account here
  2. Create new project
  3. Click on "Build a Database" button Build Database
  4. Choose the free M0 cluster and provide a name for it. Create Cluster
  5. You will prompted to create a user Create User 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
  7. To get the connection url, click "Connect" button and choose "Drivers" Connect
  8. Finally copy the connection url and replace <password> with your auto generated password Get URL
  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


Enter fullscreen mode Exit fullscreen mode

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
šŸ”˜ LinkedIn
ā­• Twitter

šŸ’– šŸ’Ŗ šŸ™… šŸš©
renzhamin
renzhamin

Posted on April 21, 2023

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related