Lessons

How to host MongoDB on Render.com

As of March 2022, Render does not have a specific MongoDB service, and you may be wondering how do I use a MongoDB for my database layer? The good news is that you can solve this by using a "Private Service" on Render. They have this guide which links out to this Github repo. The only frustration (but also good) about using a Private Service is that you cannot connect with a tool like Mongo Compass or through the command line on your local machine.

Create a new Github repository. It can be a copy or fork of the Render one. I recommend making this a private repository. Within that repository, you will have four files:

  • README
  • LICENSE
  • render.yaml
  • Dockerfile

The README and LICENSE are pretty obvious on purpose. The Dockerfile has this:

1 FROM mongo:4.2

And the render.yaml file has:

1 2 3 4 5 6 7 8 services: - type: pserv name: mongodb env: docker disk: name: data mountPath: /data/db sizeGB: 10

This render file is defining the Render service set up. It is optional and allows for the "Deploy to Render" button. I will repeat the setup manually. It is a private docker service that has 10 gb of storage. We are going to go through

On your Render account, hit the "New" button at the top, then "Private Service". Search for your new repository.

Screenshot of Github Workflow Running

Screenshot of Github Workflow Running

Select the plan that makes sense for you. I've used the lowest one and had no issues. Select "Advance". Click "Add Disk".

Screenshot of Github Workflow Running

Repeating what we put in the Render.yml file.

Screenshot of Github Workflow Running

Once completed, hit "Create Private Service". Your new service should deploy.

Connecting

For connecting, you will use the Render private URL. I'll demonstrate using Go below, but you can use any language. My connection string is as follows:

1 mongodb://kw-db:27017

kw-db is the name of my private service. 27017 is the default port for Mongo. I usually set this via environment variable on my API:

1 2 DB_CONNECTION="mongodb://kw-db:27017" \ go run main.go

I have a package called db at the root. Within it, I have db/db.go. Here is the contents:

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 package db import ( "context" "fmt" "log" "os" "go.mongodb.org/mongo-driver/mongo" "go.mongodb.org/mongo-driver/mongo/options" ) // Use this for connect to Mongo. func CreateDatabaseConnection(dbName string) (*mongo.Client, error) { dbURL := os.Getenv("DB_CONNECTION") if dbURL == "" { dbURL = "mongodb://localhost:27017" } clientOptions := options.Client().ApplyURI(dbURL) // Connect to MongoDB client, err := mongo.Connect(context.TODO(), clientOptions) if err != nil { log.Fatal(err) } // Check the connection err = client.Ping(context.TODO(), nil) if err != nil { log.Fatal(err) } fmt.Println("Connected to MongoDB!") return client, err }

Here is my main.go file:

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 dbName := "my-db-name" client, err := db.CreateDatabaseConnection(dbName) if err != nil { fmt.Println("Failed to connect to DB") panic(err) } defer client.Disconnect(context.TODO()) err = client.Ping(context.TODO(), nil) if err != nil { fmt.Println("Failed to connect to DB :: 002") panic(err) } db := client.Database(dbName)

On This Page