Your Web News in One Place

Help Webnuz

Referal links:

Sign up for GreenGeeks web hosting
April 21, 2023 10:56 am GMT

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 developement

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 ste 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 summurizes 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
  • 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
  • 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"
  • 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

Setup in MongoDB Atlas

  1. Create a free account here
  2. Create new project
  3. Click on "Build a Database" buttonBuild Database
  4. Choose the free M0 cluster and provide a name for it.Create Cluster
  5. You will prompted to create a userCreate UserNOTE: 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 passwordGet URL
  9. IMPORTANT : You have to add a database name after the host name. In my case the url looks like this
DATABASE_URL="mongodb+srv://monty:[email protected]/db_name?retryWrites=true&w=majority"

Test the connection and sync schema

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 malaware in my database

Find me on
Github
LinkedIn
Twitter


Original Link: https://dev.to/renzhamin/setup-mongodb-replica-set-locally-in-docker-or-with-atlas-for-prisma-orm-54gp

Share this article:    Share on Facebook
View Full Article

Dev To

An online community for sharing and discovering great ideas, having debates, and making friends

More About this Source Visit Dev To