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 deve…

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” 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
DATABASE_URL="mongodb+srv://monty:pass@cluster0.nqtl0pv.mongodb.net/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


Print Share Comment Cite Upload Translate
APA
Tasnim Ferdous | Sciencx (2024-03-29T07:04:05+00:00) » Setup MongoDB replica set locally in docker or with Atlas for Prisma ORM. Retrieved from https://www.scien.cx/2023/04/21/setup-mongodb-replica-set-locally-in-docker-or-with-atlas-for-prisma-orm/.
MLA
" » Setup MongoDB replica set locally in docker or with Atlas for Prisma ORM." Tasnim Ferdous | Sciencx - Friday April 21, 2023, https://www.scien.cx/2023/04/21/setup-mongodb-replica-set-locally-in-docker-or-with-atlas-for-prisma-orm/
HARVARD
Tasnim Ferdous | Sciencx Friday April 21, 2023 » Setup MongoDB replica set locally in docker or with Atlas for Prisma ORM., viewed 2024-03-29T07:04:05+00:00,<https://www.scien.cx/2023/04/21/setup-mongodb-replica-set-locally-in-docker-or-with-atlas-for-prisma-orm/>
VANCOUVER
Tasnim Ferdous | Sciencx - » Setup MongoDB replica set locally in docker or with Atlas for Prisma ORM. [Internet]. [Accessed 2024-03-29T07:04:05+00:00]. Available from: https://www.scien.cx/2023/04/21/setup-mongodb-replica-set-locally-in-docker-or-with-atlas-for-prisma-orm/
CHICAGO
" » Setup MongoDB replica set locally in docker or with Atlas for Prisma ORM." Tasnim Ferdous | Sciencx - Accessed 2024-03-29T07:04:05+00:00. https://www.scien.cx/2023/04/21/setup-mongodb-replica-set-locally-in-docker-or-with-atlas-for-prisma-orm/
IEEE
" » Setup MongoDB replica set locally in docker or with Atlas for Prisma ORM." Tasnim Ferdous | Sciencx [Online]. Available: https://www.scien.cx/2023/04/21/setup-mongodb-replica-set-locally-in-docker-or-with-atlas-for-prisma-orm/. [Accessed: 2024-03-29T07:04:05+00:00]
rf:citation
» Setup MongoDB replica set locally in docker or with Atlas for Prisma ORM | Tasnim Ferdous | Sciencx | https://www.scien.cx/2023/04/21/setup-mongodb-replica-set-locally-in-docker-or-with-atlas-for-prisma-orm/ | 2024-03-29T07:04:05+00:00
https://github.com/addpipe/simple-recorderjs-demo