Setup MongoDB On WSL Ubuntu 18.04
Published: 2020-04-20 08:20:46

# Introduction

This article will cover the use of MongoDB on WSL (Windows Subsytem For Linux) Ubuntu. Other than installation and we will cover some caveats, use cases such as migration, upgrade process.

**ADVISORY:** Do not use WSL for production. We are only using it here to learn how to use Ubuntu OS version of MongoDB - on a windows machine.

# Installation

The are 2 commonly used packages available for MongoDB on Ubuntu
- mongodb
- mongodb-org

**mongodb-org** is the package described in official [MongoDB](https://docs.mongodb.com/manual/tutorial/install-mongodb-on-ubuntu/) documents.

## mongodb

If you use **mongodb** package, you will be able to install easily and run smoothly.

```bash
sudo apt-get update
sudo apt-get install mongodb
sudo service mongodb start
```

However, the current version installed is 3.6 and you will miss out on using MongoDB Transactions introduced from 4.0 onwards.

## mongodb-org

For **mongodb-org** package, although you can install and manually run the latest stable version.

**NOTE:** Even though official documentation says MongoDB is not supported on WSL, you can still try and install.

However, the service will not be able to install (I suspect due to WSL Ubuntu only supporting system V init). You will get the following error if you try to run the service.

```bash
sudo service mongod start
initctl: Unable to connect to Upstart: Failed to connect to socket /com/ubuntu/upstart: Connection refused
mongod: unrecognized service
```

Running as a service issue can be fixed by following the steps in [this github comment](https://github.com/microsoft/WSL/issues/796#issuecomment-611626709). 

You manually create the service using the following script [https://raw.githubusercontent.com/mongodb/mongo/master/debian/init.d](https://raw.githubusercontent.com/mongodb/mongo/master/debian/init.d), and now you can use...

```bash
sudo service mongod start
```

## Which One To Use

This will depend on your requirements. If you need to use an updated version, use **mongodb-org**. If you need something quick and easy to install, use **mongodb**.

NOTE: the configuration file names are different for each package
- mongodb - /etc/**mongodb**.conf
- mongodb-org - /etc/**mongod**.conf

---

# Data Migration / Backup / Restore

As mentioned in the official MongoDB documents [https://docs.mongodb.com/manual/core/backups/](https://docs.mongodb.com/manual/core/backups/)

Starting from MongoDB 4.2, **mongodump** should not be used as part of backup strategy if you are using **sharded clusters and transactions in progress**. Use the other recommended methods instead.

However, in a development environment, you can use **mongodump** and **mongorestore** to backup, restore and migrate data and indexes.

```bash
# backup
$ mongodump --archive=my_dump

# do some upgrade

# restore
$ mongorestore --archive=my_dump
```

---

# Setting Up Replication Set

[Replication](https://docs.mongodb.com/manual/replication/) is used to provide redundancy and high-availibility. They are also required if you are need to use [Transactions](https://docs.mongodb.com/manual/core/transactions/).

### Config File Changes

In /etc/mongod.conf (mongodb-org package), set the replication.replSetName property.

```
replication:
   oplogSizeMB: <int>
   replSetName: <string>
   enableMajorityReadConcern: <boolean>
```

Restart the service after changes are made.


### Start Replication

Connect to MongoDB using the  **mongo** client and start up or check for replication set using the following commands

```bash
# start up replica set
> rs.initiate()

# check replica set status
> rs.status()
```

---

# Conclusion

There are many more topics to cover for MongoDB, especially for running smoothly in a production environment.

Some topics to cover include:
- Security
- Sharding
- Change streams
- Administration
- MongoDB internals and storage engines

As an alternative to doing it all by yourself, you can use Mongo Atlas and leave the operational work to others while you focus on your business data and logic.