Connect to MongoDB on Dokku with your local command line or Robo3T
Tags: deployment, database, dokku, mongodb
Published: 2018-12-31 16:28:15

---
title: Connect to MongoDB on Dokku with your local command line or Robo3T
published: true
tags: deployment, database, dokku, mongodb
canonical_url: https://codewithhugo.com/connect-to-mongodb-on-dokku-with-your-local-command-line-or-robo3t/
---

The Dokku mongo plugin provides a `mongo:connect` command, which opens a tunnelled connection to you MongoDB instance on Dokku. This is how you can connect to your database using the tool of your choice, the examples will be using Robo3T.

## Pre-requisites

### dokku-mongo is installed

See [github.com/dokku/dokku-mongo](https://github.com/dokku/dokku-mongo), to install it:

- ssh into the server running Dokku as root
- run: `sudo dokku plugin:install https://github.com/dokku/dokku-mongo.git mongo`

### You have a database to play with

Create one with: `ssh -t dokku@YOUR_INSTANCE_IP mongo:create public-database`

You should get the following output, the bolded string is the mongo connection URI:

```
Waiting for container to be ready
=====> MongoDB container created: public-database
=====> Container Information
    Config dir: /var/lib/dokku/services/mongo/public-database/config
    Data dir: /var/lib/dokku/services/mongo/public-database/data
    Dsn: mongodb://public-database:289b4a4b32ab3182895d8aac7def0ee1@dokku-mongo-public-database:27017/public-database
    Exposed ports: -
    Id: 67163f2006dd9e17f8325620814bae9b881ee6465088b8b1c571fbb6c08ac801
    Internal ip: 172.17.0.11
    Links: -
    Service root: /var/lib/dokku/services/mongo/public-database
    Status: running
    Version: mongo:3.4.9
```

## Connect to your remote MongoDB instance on Dokku

First off, let’s check that we can connect using the included dokku-mongo `:connect` command. Run the following command:

```
ssh -t dokku@YOUR_INSTANCE_IP mongo:connect public-database
```

It should open a MongoDB shell:

```
MongoDB shell version v3.4.9
connecting to: mongodb://127.0.0.1:27017/public-database
MongoDB server version: 3.4.9
>
```

### Dokku’s behaviour around access of MongoDB

If we look back at the `mongo:info` output we’ll notice the following line:

```
Exposed ports: -
```

That means this MongoDB instance is not exposed to anything outside of Dokku, that’s good for security, bad for accessing it from your local tools.

### Exposing our MongoDB instance

We’ll need to expose it, and pass some ports we want to map to (this helps with predictability/reproducibility)

```
ssh -t dokku@YOUR_INSTANCE_IP mongo:expose public-database 20017 20018 20019 20020
```

Which yields the following output:

```
-----> Service public-database exposed on port(s) [container->host]: 27017->20017 27018->20018 27019->20019 28017->20020
```

Which means the MongoDB ports 27017, 27018, 27019 and 28017 were exposed at the server’s 20017, 20018, 20019, 20020 ports respectively.

### Connecting with Robo3T

In order to connect with Robo3T we’ll create a new connection to the MongoDB instance.

- Open the “Connect” menu (File \> Connect)
- In the first line, click “Create”
- Fill out the “Connection” tab, of course, replace YOUR\_SERVER\_IP with your actual server IP or domain name.

![](https://d2mxuefqeaa7sj.cloudfront.net/s_5F4BC56CEE8442961498E30A88107FB43112E81DEC4BBA7ABD2A9A1388573B9B_1546270597650_image.png)

- Fill out the “Authentication” tab, if you remember our connection string from `dokku mongo:info public-database`, ` **mongodb://public-database:289b4a4b32ab3182895d8aac7def0ee1@dokku-mongo-public-database:27017/public-database** `
  - The format is `mongodb://USERNAME:PASSWORD@INTERNAL_HOSTNAME:INTERNAL_PORT/DATABASE_NAME`
  - So we have
  - Database: `public-database`
  - User Name: `public-database`
  - Password: `289b4a4b32ab3182895d8aac7def0ee1` (use your actual password)

![](https://d2mxuefqeaa7sj.cloudfront.net/s_5F4BC56CEE8442961498E30A88107FB43112E81DEC4BBA7ABD2A9A1388573B9B_1546270788604_image.png)

- You can now hit “Test” and it should tell you everything is fine
- You can “Save” and “Connect” to this new database

## Testing out the connection

Create a new collection “newest-public-spaces”

![](https://d2mxuefqeaa7sj.cloudfront.net/s_5F4BC56CEE8442961498E30A88107FB43112E81DEC4BBA7ABD2A9A1388573B9B_1546271101758_image.png)

![](https://d2mxuefqeaa7sj.cloudfront.net/s_5F4BC56CEE8442961498E30A88107FB43112E81DEC4BBA7ABD2A9A1388573B9B_1546271148845_image.png)

Insert some data

![](https://d2mxuefqeaa7sj.cloudfront.net/s_5F4BC56CEE8442961498E30A88107FB43112E81DEC4BBA7ABD2A9A1388573B9B_1546271216423_image.png)

![](https://d2mxuefqeaa7sj.cloudfront.net/s_5F4BC56CEE8442961498E30A88107FB43112E81DEC4BBA7ABD2A9A1388573B9B_1546271218792_image.png)

Let’s disconnect and go back to the dokku-mongo plugin connection and get data for the `newest-public-spaces` collection to see if we were talking to the same database.

```
ssh -t dokku@YOUR_SERVER_IP mongo:connect public-database
MongoDB shell version v3.4.9
connecting to: mongodb://127.0.0.1:27017/public-database
MongoDB server version: 3.4.9
> db.getCollection('newest-public-spaces').find({})
{ "_id" : ObjectId("5c2a39e6e632f9bc24ec1a2d"), "name" : "my park", "location" : "next door" }
```

## Security concerns

These connections are not done with TLS (transport layer security), which is fine for non-sensitive data.

I would also suggest using `dokku mongo:unexpose YOUR_DATABASE_NAME` when not actively using the connection.

For more about Dokku and deployment see: [Deployment options: Netlify + Dokku on DigitalOcean vs now.sh, GitHub Pages, Heroku and AWS](https://dev.to/hugo__df/deployment-options-netlify--dokku-on-digitalocean-vs-nowsh-github-pages-heroku-and-aws-4cab) or the [“Deployment”](https://codewithhugo.com/tags/deployment) category on Code with Hugo

If you’re looking for a good place to host your Dokku instance, I recommend [DigitalOcean](https://m.do.co/c/4ee8b2d1adb4), they have a great [One-click install and deploy Dokku](https://www.digitalocean.com/products/one-click-apps/dokku/), if you use [this referral link you’ll get $100 free credit](https://m.do.co/c/4ee8b2d1adb4).

[<svg xmlns="http://www.w3.org/2000/svg" style="height:12px;width:auto;position:relative;vertical-align:middle;top:-2px;fill:white" viewbox="0 0 32 32"><title>unsplash-logo</title>
<path d="M10 9V0h12v9H10zm12 5h10v18H0V14h10v9h12v-9z"></path></svg>Cameron Kirby](https://unsplash.com/@ckirby?utm_medium=referral&utm_campaign=photographer-credit&utm_content=creditBadge "Download free do whatever you want high-resolution photos from Cameron Kirby")