Setup MongoDB replica set locally in docker or with Atlas for Prisma ORM
renzhamin
Posted on April 21, 2023
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
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
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
- Create a free account here
- Create new project
- Click on "Build a Database" button
- Choose the free M0 cluster and provide a name for it.
- You will prompted to create a user NOTE: You should use an auto generated password for security and note it down somewhere
- Your IP will be automatically added to the access list, click "Finsh and Close" and go to the database.
- To get the connection url, click "Connect" button and choose "Drivers"
- Finally copy the connection url and replace <password> with your auto generated password
- 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 š.
Posted on April 21, 2023
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.