---
title: "Deploy MongoDB"
description: "Latest Self Hosted Database Service"
category: "Other"
url: https://railway.com/deploy/mongodb-latest
---

# Deploy MongoDB

Latest Self Hosted Database Service

**[Deploy MongoDB on Railway](https://railway.com/template/mongodb-latest)**

- **Category:** Other

## Template content

### MongoDB

- **Image:** mongo:latest

## Documentation

# Deploy and Host MongoDB Latest on Railway

MongoDB Latest is a self-hosted deployment of the official MongoDB Docker image that provides a flexible, scalable NoSQL document database. This Railway template deploys the latest MongoDB version with authentication enabled and persistent storage, making it ideal for powering web applications, APIs, AI agents, analytics platforms, automation tools, and internal services.

## About Hosting MongoDB Latest

Hosting MongoDB Latest on Railway provides a fast and reliable way to deploy a production-ready MongoDB instance without managing servers or manually configuring containers. Railway automatically deploys the latest official MongoDB image, provisions persistent storage, and offers secure networking for both private services and external clients.

The database stores all data on a Railway Volume, ensuring collections, indexes, and documents persist across redeployments and service restarts. Applications running within the same Railway project can securely connect using Railway's private networking, while external tools such as MongoDB Compass, DBeaver, or custom applications can connect through Railway's TCP Proxy. This template is suitable for development, staging, internal applications, and lightweight production deployments.

## Common Use Cases

* Primary database for web applications and REST APIs
* Backend database for AI agents and automation platforms
* Data storage for internal dashboards and business applications
* Mongo Express and self-hosted database management tools
* Development, testing, and staging environments
* Content management systems and e-commerce platforms
* Analytics and event logging applications
* Real-time applications using flexible document storage

## Dependencies for MongoDB Latest Hosting

| Dependency                    | Purpose                     |
| ----------------------------- | --------------------------- |
| Official MongoDB Docker Image | MongoDB database server     |
| Railway Volume                | Persistent database storage |

### Deployment Dependencies

* MongoDB Official Docker Image: [https://hub.docker.com/_/mongo](https://hub.docker.com/_/mongo)
* MongoDB Documentation: [https://www.mongodb.com/docs/](https://www.mongodb.com/docs/)
* Railway Documentation: [https://docs.railway.com](https://docs.railway.com)

### Implementation Details

#### Docker

This template deploys the latest official MongoDB Docker image.

Start Command:

```bash
docker-entrypoint.sh mongod --ipv6 --bind_ip ::,0.0.0.0 --setParameter diagnosticDataCollectionEnabled=false
```

No additional build configuration is required.

---

#### Public Networking

| Setting         | Value                   |
| --------------- | ----------------------- |
| Proxy Type      | TCP Proxy               |
| Internal Port   | 27017                   |
| External Access | Railway TCP Proxy       |
| Internal Access | Railway Private Network |

---

#### Environment Variables

| Variable                     | Required | Description                    |
| ---------------------------- | -------- | ------------------------------ |
| `MONGO_INITDB_ROOT_USERNAME` | Yes      | MongoDB administrator username |
| `MONGO_INITDB_ROOT_PASSWORD` | Yes      | MongoDB administrator password |

Example:

```env
MONGO_INITDB_ROOT_USERNAME=mongo
MONGO_INITDB_ROOT_PASSWORD=${{secret(32)}}

MONGO_PUBLIC_URL=mongodb://${{MONGO_INITDB_ROOT_USERNAME}}:${{MONGO_INITDB_ROOT_PASSWORD}}@${{RAILWAY_TCP_PROXY_DOMAIN}}:${{RAILWAY_TCP_PROXY_PORT}}

MONGO_URL=mongodb://${{MONGO_INITDB_ROOT_USERNAME}}:${{MONGO_INITDB_ROOT_PASSWORD}}@${{RAILWAY_PRIVATE_DOMAIN}}:27017

MONGOHOST=${{RAILWAY_PRIVATE_DOMAIN}}
MONGOPORT=27017
MONGOUSER=${{MONGO_INITDB_ROOT_USERNAME}}
MONGOPASSWORD=${{MONGO_INITDB_ROOT_PASSWORD}}
```

---

#### Persistent Storage

A Railway Volume is **required**.

Mount the volume at:

```text
/data/db
```

The persistent volume stores:

* MongoDB databases
* Collections
* Indexes
* User accounts
* Configuration data

This ensures your data remains available after redeployments and service restarts.

---

#### Database Connections

Once deployed, MongoDB can be accessed in several ways:

##### Railway Database Tab

Browse collections, run queries, and manage your database directly from Railway's built-in Database interface.

##### Railway Console

Run MongoDB shell commands directly from the Railway Console.

##### Railway CLI

Create a secure local tunnel without exposing the database publicly:

```bash
railway connect MongoDB
```

##### External Database Clients

Compatible with:

* MongoDB Compass
* DBeaver
* TablePlus
* Studio 3T
* DataGrip

Use either:

* `MONGO_PUBLIC_URL`

or configure manually:

| Setting                 | Value                             |
| ----------------------- | --------------------------------- |
| Host                    | `${{RAILWAY_TCP_PROXY_DOMAIN}}`   |
| Port                    | `${{RAILWAY_TCP_PROXY_PORT}}`     |
| Username                | `${{MONGO_INITDB_ROOT_USERNAME}}` |
| Password                | `${{MONGO_INITDB_ROOT_PASSWORD}}` |
| Authentication Database | `admin`                           |

##### Private Networking

Applications deployed within the same Railway project should connect using:

```text
mongodb://${{MONGO_INITDB_ROOT_USERNAME}}:${{MONGO_INITDB_ROOT_PASSWORD}}@${{RAILWAY_PRIVATE_DOMAIN}}:27017
```

This provides secure, low-latency communication without exposing the database publicly.

---

#### Comparison

| Feature                | This Template | MongoDB Atlas | Self-managed VPS |
| ---------------------- | ------------- | ------------- | ---------------- |
| One-Click Deployment   | ✅             | ⚠️            | ❌                |
| Latest MongoDB Version | ✅             | ✅             | ✅                |
| Persistent Storage     | ✅             | ✅             | Manual           |
| Private Networking     | ✅             | ✅             | Manual           |
| Full Database Control  | ✅             | Limited       | ✅                |
| Low Maintenance        | ✅             | ✅             | ❌                |
| Railway Integration    | ✅             | ❌             | ❌                |

## Why Deploy MongoDB Latest on Railway?

Railway is a singular platform to deploy your infrastructure stack. Railway will host your infrastructure so you don't have to deal with configuration, while allowing you to vertically and horizontally scale it.

By deploying MongoDB Latest on Railway, you are one step closer to supporting a complete full-stack application with minimal burden. Host your servers, databases, AI agents, and more on Railway.


## Similar templates

- [Rocky Linux](https://railway.com/deploy/rocky-linux) — [Jul'26] Hosted Rocky Linux 9 workspace with SSH and persistent storage. 🚀
- [Foundry Virtual Tabletop](https://railway.com/deploy/X5tR6G) — A Self-Hosted & Modern Roleplaying Platform
- [Letta Code Remote](https://railway.com/deploy/letta-code-remote) — Run a Letta Code agent 24/7. No inbound ports, just deploy.

Open this page in a browser: https://railway.com/deploy/mongodb-latest
