๐Ÿš€ How to Self-Host a MongoDB Server (with Cloudflare Tunnel) Like a Pro โ€” And Have Fun Doing It! ๐Ÿ๐Ÿ’ป

in HiveDevs โ€ข 10 days ago

๐ŸŽ‰ Hey Hive Rockstars! ๐Ÿฅณ

Ready to level up your tech game, save money, and have some laughs? Grab your favorite beverage, because today we're going on an EPIC ADVENTURE to self-host a MongoDB serverโ€”powered by Cloudflare Tunnel! All with pictures, guides, and plenty of emojis! ๐Ÿ˜โœจ


๐Ÿšฆ Prerequisites (a.k.a. What you'll need before liftoff!)

  • ๐Ÿš€ A domain (Mine: sagarkothari88.one)
  • โ˜๏ธ Cloudflare account
  • ๐Ÿค“ Basic knowledge of Cloudflare Tunnels

Setup MongoDB Server

mongodb-docker

Okay. Let's get started. You are in for a treat for saving your costs of mongodb by self-hosting it.

๐Ÿ—๏ธ Step 1: Access Your System

  • SSH or use Chrome Remote Desktop (I love Chrome Remote Desktop for all my systems!)
  • Once you're in, you're golden! ๐Ÿ€

๐Ÿณ Step 2: Install Docker

Pro-tip: Docker is like giving your MongoDB its personal luxury apartment.

๐ŸŽฏ Docker-install-step-1: Remove any old baggage

for pkg in docker.io docker-doc docker-compose docker-compose-v2 podman-docker containerd runc; do sudo apt-get remove $pkg; done

๐ŸŽฏ Docker-install-step-2: Add Dockerโ€™s home to your address book

sudo apt-get update
sudo apt-get install ca-certificates curl
sudo install -m 0755 -d /etc/apt/keyrings
sudo curl -fsSL https://download.docker.com/linux/ubuntu/gpg -o /etc/apt/keyrings/docker.asc
sudo chmod a+r /etc/apt/keyrings/docker.asc
echo 
โ€œdeb arch=$(dpkg โ€“print-architecture) signed-by=/etc/apt/keyrings/docker.asc https://download.docker.com/linux/ubuntu 
$(. /etc/os-release && echo โ€œ${UBUNTU_CODENAME:-$VERSION_CODENAME}โ€) stableโ€ | 
sudo tee /etc/apt/sources.list.d/docker.list > /dev/null
sudo apt-get update

๐ŸŽฏ Docker-install-step-3: Give Docker the keys!

sudo apt-get install docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin

๐Ÿƒ Step 3: Spin Up MongoDB in Docker!

mongodb-docker

Ready? Set. GO! Swap out the username & password, then run:

sudo docker run -d \
  --name mongodb_custom \
  -p 27017:27017 \
  -e MONGO_INITDB_ROOT_USERNAME="your-mongo-db-super-admin-username-here" \
  -e MONGO_INITDB_ROOT_PASSWORD="your-mongo-db-super-admin-password-here" \
  mongo:latest

Congratulations! ๐Ÿฅณ Your private MongoDB server is now live.


๐ŸŒฉ๏ธ Setting up Cloudflare Tunnel (where security meets cool tech)

Setting up cloudflare

Wanna access your MongoDB from ANYWHERE but keep it ultra-secure? You need a Cloudflare Tunnel! ๐Ÿš‡๐Ÿฆธโ€โ™‚๏ธ


Zero Trust Magic

  • Open Cloudflare dashboard
  • Click Zero Trust (trust me, click it!)

Open Cloudflare Dashboard & click on Zero Trust

Tunnels Time!

  • Go to Network > Tunnels

Access Tunnel

Create a Tunnelโ€”just like creating a secret passage!

Create a new tunnel

Give your Tunnel a cool name (I called mine "mongodb")

Name your tunnel

Follow setup instructions for Ubuntu

Setup tunnel on your ubuntu

Once you see:

2025-08-03T12:32:03Z INF Using Systemd
2025-08-03T12:32:05Z INF Linux service for cloudflared installed successfully

celebrate! Your tunnel is ready ๐Ÿฅณ

Final Step: Bind your tunnel like a pro

  • Bind to tcp://localhost:27017 (VERY IMPORTANT!)
  • Click โ€˜Complete setupโ€™

Last step for cloudflare tunnel

Voilร ! Your MongoDB server is world-ready, but only accessible to your secure, trusted peers. (Intruders = Bye Felicia! ๐Ÿ‘‹๐Ÿšซ)


๐Ÿ“จ Connecting with MongoDB Client (the fun continues!)

Time to connect to your shiny new MongoDB from another system (โ€œmongodb-clientโ€ aka party guest system).


๐Ÿ”ง Install Cloudflared on the Client

For macOS users

brew install cloudflared

or you can use macPorts

sudo port install cloudflared

For Ubuntu adventurers

Add GPG & Repo:

sudo apt install curl lsb-release
curl -L https://pkg.cloudflare.com/cloudflare-main.gpg | sudo tee /usr/share/keyrings/cloudflare-archive-keyring.gpg >/dev/null
echo "deb [signed-by=/usr/share/keyrings/cloudflare-archive-keyring.gpg] https://pkg.cloudflare.com/cloudflared $(lsb_release -cs) main" | sudo tee /etc/apt/sources.list.d/cloudflared.list

Install:

sudo apt update
sudo apt install cloudflared

๐Ÿ›ก๏ธ Login Cloudflared

cloudflared tunnel login
  • This opens a browser ๐Ÿ‘จโ€๐Ÿ’ป
  • Log into Cloudflare & authorize your domain. (Mine: sagarkothari88.one)

๐ŸŒ‰ Start Bridge!

cloudflared access tcp --hostname mongodb.sagarkothari88.one --url localhost:27017
  • Bridge created! Your remote server is now accessible safely as localhost.

๐Ÿฅž Connect with MongoDB!

  • For NodeJS:
    mongodb://admin-user:admin-password@localhost:27017/
  • To test: use MongoDB Compass, start the bridge FIRST, then connect.

connect using compass


Whoa! You did it! ๐ŸŽŠ Your own self-hosted MongoDB โ€” secure, portable, AND budget-friendly.


๐Ÿ’ฌ Wrapping Up

Thanks for joining this fun ride ๐Ÿ’™
More power to the Hive Blockchain ๐Ÿ
More power to all our community members ๐Ÿ™Œ
Until next time, Happy Coding! ๐Ÿ’ปโœจ


๐Ÿ“ Final Note


๐Ÿš€ My Contributions to โ™ฆ๏ธ Hive Ecosystem

ContributionToHiveEcosystem
Hive Witness NodeHive API Node (in progress)3Speak Video Encoder Node Operator (highest number of nodes)3Speak Mobile App Developer
3Speak Podcast App Developer3Speak Shorts App Developer3Speak Support & Maintenance TeamDistriator Developer
CheckinWithXYZHive InboxHiFindHive Donate App
Contributed to HiveAuth Mobile AppEcency โ†” 3Speak IntegrationEcency โ†” InLeo IntegrationEcency โ†” Actifit Integration
Hive Stats AppVote for Witness AppHiveFlutterKitNew 3Speak App

๐Ÿ™Œ Support Back

โค๏ธ Appreciate my work? Consider supporting @threespeak & @sagarkothari88! โค๏ธ


Sort: ย 
Loading...

Please stop downvoting my original content with your alt account @letusbuyhive you have not sold it to anyone ๐Ÿ˜‚๐Ÿ˜‚๐Ÿ˜‚๐Ÿ˜‚๐Ÿ˜‚๐Ÿ˜‚๐Ÿ˜‚

If you have sold the account who have you sold it too ๐Ÿค”๐Ÿ˜‚๐Ÿ˜‚๐Ÿ˜‚๐Ÿ˜‚๐Ÿ˜‚๐Ÿ˜‚

ย 10 days agoย (edited)ย 

Blank me as much as you like it just proves your guilt and you won't blank me in real life ๐Ÿ’ฏ

Best one for me. Got to know something really new and helpful.