Setup MongoDB On WSL Ubuntu 18.04

Aaron Gong - Apr 20 '20 - - Dev Community

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 documents.

mongodb

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

sudo apt-get update
sudo apt-get install mongodb
sudo service mongodb start
Enter fullscreen mode Exit fullscreen mode

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.

sudo service mongod start
initctl: Unable to connect to Upstart: Failed to connect to socket /com/ubuntu/upstart: Connection refused
mongod: unrecognized service
Enter fullscreen mode Exit fullscreen mode

Running as a service issue can be fixed by following the steps in this github comment.

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

sudo service mongod start
Enter fullscreen mode Exit fullscreen mode

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/

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.

# backup
$ mongodump --archive=my_dump

# do some upgrade

# restore
$ mongorestore --archive=my_dump
Enter fullscreen mode Exit fullscreen mode

Setting Up Replication Set

Replication is used to provide redundancy and high-availibility. They are also required if you are need to use Transactions.

Config File Changes

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

replication:
   oplogSizeMB: <int>
   replSetName: <string>
   enableMajorityReadConcern: <boolean>
Enter fullscreen mode Exit fullscreen mode

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

# start up replica set
> rs.initiate()

# check replica set status
> rs.status()
Enter fullscreen mode Exit fullscreen mode

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.

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .