Skip to content

Commit 2bf981c

Browse files
technotipsvenja11
andauthored
✨ "How to Enable a Replica Set in MongoDB on Coolify (for Prisma ORM Compatibility)" (#1160)
* Add files via upload * Update 01.en.md * Spelling and formatting updates --------- Co-authored-by: svenja.michal <84835304+svenja11@users.noreply.github.com>
1 parent 187b8d1 commit 2bf981c

File tree

4 files changed

+182
-0
lines changed

4 files changed

+182
-0
lines changed
Lines changed: 182 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,182 @@
1+
---
2+
SPDX-License-Identifier: MIT
3+
path: "/tutorials/prisma-mongodb-replica-set"
4+
slug: "prisma-mongodb-replica-set"
5+
date: "2025-06-13"
6+
title: "How to Enable a Replica Set in MongoDB on Coolify (for Prisma ORM Compatibility)"
7+
short_description: "Learn how to enable a MongoDB replica set on Coolify to make it compatible with Prisma ORM, even for single-node deployments."
8+
tags: ["MongoDB", "Prisma"]
9+
author: "Satish"
10+
author_link: "https://github.com/technotip"
11+
author_img: "https://avatars.githubusercontent.com/u/4805549"
12+
author_description: ""
13+
language: "en"
14+
available_languages: ["en"]
15+
header_img: "header-6"
16+
cta: "cloud"
17+
---
18+
19+
## Introduction
20+
21+
If you're using **Prisma ORM** with **MongoDB**, you may encounter an unexpected blocker: Prisma **requires MongoDB to be part of a replica set** — even if you're only running a single-node deployment. This requirement is non-negotiable, and without a replica set, Prisma simply won't work with MongoDB.
22+
23+
In this article, we’ll walk you through how to **enable a single-node replica set** on a MongoDB instance hosted via **Coolify** on your VPS. This is the simplest workaround for Prisma’s replica set dependency when you're not running a full production-ready MongoDB cluster.
24+
25+
**Why Prisma Requires a Replica Set**
26+
27+
Prisma uses features like transactions and change streams, which require MongoDB to be deployed as a replica set — even if you’re using just one node. This doesn’t mean you have to set up multiple MongoDB instances; you can configure a **replica set on a single-node** MongoDB server to satisfy Prisma’s requirements.
28+
29+
**Prerequisites**
30+
31+
* Coolify is already installed (see [this tutorial](https://community.hetzner.com/tutorials/install-and-configure-coolify-on-linux))
32+
* You've deployed a [MongoDB](https://coolify.io/docs/databases/#deploy-a-database) database
33+
* Your app uses [Prisma](https://www.prisma.io/)
34+
35+
## Step 1 - Access Your Coolify Dashboard
36+
37+
Log into your Coolify instance.
38+
39+
You should have a project with a MongoDB resource and a server where Coolify is running on.
40+
41+
![](images/coolify-dashboard.png)
42+
43+
## Step 2 - Generate a Secure Keyfile
44+
45+
The replica set requires a shared keyfile for internal authentication, even if it's just one node.
46+
47+
1. In the dashboard, select the server **where Coolify itself is running**.
48+
2. Switch to the terminal tab.
49+
3. Run the following command to generate a 741-byte base64 key:
50+
51+
```bash
52+
openssl rand -base64 741
53+
```
54+
55+
4. Create a new file:
56+
57+
```bash
58+
nano keyfile-mongo
59+
```
60+
61+
5. Paste the generated key and save the file.
62+
6. Secure the keyfile with proper permissions:
63+
64+
```bash
65+
chmod 400 keyfile-mongo
66+
chown 999:999 keyfile-mongo
67+
```
68+
69+
7. Create a directory to store the keyfile and move it there:
70+
71+
```bash
72+
mkdir -p /root/replica
73+
mv keyfile-mongo /root/replica/replica.key
74+
```
75+
76+
## Step 3 - Mount the Keyfile in Your MongoDB Instance
77+
78+
1. In the Coolify dashboard, select the project with MongoDB and open your MongoDB instance.
79+
2. Go to **Persistent Storage**.
80+
3. Click **+ Add** to add a new volume:
81+
82+
- **Name**: `replica`
83+
- **Source Path**: `/root/replica`
84+
- **Destination Path**: `/tmp/mongodb`
85+
86+
This mounts your keyfile inside the MongoDB container.
87+
88+
![](images/mount-keyfile.png)
89+
90+
## Step 4 - Update the MongoDB Configuration
91+
92+
1. Go to the **General** tab for your MongoDB instance.
93+
2. Scroll down to **Custom MongoDB Configuration**, and enter:
94+
95+
```yaml
96+
replication:
97+
replSetName: "rs0"
98+
security:
99+
authorization: enabled
100+
keyFile: /tmp/mongodb/replica.key
101+
```
102+
103+
![](images/mongodb-config.png)
104+
105+
## Step 5 - Enable Replica Set Initialization
106+
107+
1. Start your MongoDB instance.
108+
1. Go to the **Terminal** tab for your MongoDB instance.
109+
3. Connect to the MongoDB server:
110+
```bash
111+
mongosh -u root -p <mongodb_password> --authenticationDatabase admin
112+
```
113+
4. Now run:
114+
115+
> Replace `27017` with the port of your MongoDB instance.
116+
117+
```js
118+
rs.initiate({
119+
_id: "rs0",
120+
members: [{ _id: 0, host: "localhost:27017" }],
121+
});
122+
```
123+
124+
3. Save and restart your MongoDB service.
125+
126+
## Step 6 - Testing the Setup
127+
128+
After restarting MongoDB:
129+
130+
1. Connect to the MongoDB server:
131+
```bash
132+
mongosh -u root -p <mongodb_password> --authenticationDatabase admin
133+
```
134+
135+
2. Run:
136+
```js
137+
rs.status();
138+
```
139+
140+
3. Confirm that the replica set is active.
141+
4. Test your Prisma application to ensure the connection works.
142+
143+
## Step 7 - Production Considerations (Optional)
144+
145+
This tutorial sets up a **single-node replica set** to satisfy Prisma’s requirements. For high availability and production-grade environments, consider setting up a **three-node replica set** across separate containers or servers.
146+
147+
## Conclusion
148+
149+
You've now configured MongoDB with a **single-node replica set** in Coolify, ensuring full compatibility with Prisma ORM — without the complexity of managing multiple MongoDB instances. This setup is ideal for local development, staging, or lightweight production use cases.
150+
151+
When you're ready to scale, transitioning to a true multi-node replica set will be straightforward.
152+
153+
##### License: MIT
154+
155+
<!--
156+
157+
Contributor's Certificate of Origin
158+
159+
By making a contribution to this project, I certify that:
160+
161+
(a) The contribution was created in whole or in part by me and I have
162+
the right to submit it under the license indicated in the file; or
163+
164+
(b) The contribution is based upon previous work that, to the best of my
165+
knowledge, is covered under an appropriate license and I have the
166+
right under that license to submit that work with modifications,
167+
whether created in whole or in part by me, under the same license
168+
(unless I am permitted to submit under a different license), as
169+
indicated in the file; or
170+
171+
(c) The contribution was provided directly to me by some other person
172+
who certified (a), (b) or (c) and I have not modified it.
173+
174+
(d) I understand and agree that this project and the contribution are
175+
public and that a record of the contribution (including all personal
176+
information I submit with it, including my sign-off) is maintained
177+
indefinitely and may be redistributed consistent with this project
178+
or the license(s) involved.
179+
180+
Signed-off-by: [Satish <satish@technotip.org>]
181+
182+
-->
Loading
Loading
Loading

0 commit comments

Comments
 (0)