Install MongoDB on WSL

Raymond Tang Raymond Tang 1 3403 2.13 index 2/1/2021

MongoDB  is one of the most famous NoSQL databases that uses JSON-like documents with optional schemas.

Prerequisites

Before installing MongoDB on WSL, please ensure you have WSL enabled on your Windows 10 system.

Follow Install Windows Subsystem for Linux on a Non-System Drive to install WSL on a non-C drive.

This tutorial provides steps to install MongoDB on WSL Debian distro. You can follow similar steps to install it in other Linux distros.

Step by step guide

  1. Open Debian distro through WSL command:

    wsl -d Debian
    
  2. Update Debian packages:

    sudo apt update
    

    You need to type user password.

  3. Install Redis using the following command:

    sudo apt install mongodb
    

    Type Y to continue when asked.

  4. Wait until the installation is completed.

  5. Verify MongoDB version:

    $ mongod --version
    db version v3.2.11
    git version: 009580ad490190ba33d1c6253ebd8d91808923e4
    OpenSSL version: OpenSSL 1.0.2l  25 May 2017
    allocator: tcmalloc
    modules: none
    build environment:
        distarch: x86_64
        target_arch: x86_64
    

By default, MongoDB stores data in /data/db and host the daemon service on port 27017. And mongodis the daemon (host process for the database) and mongois the command-line shell that connects to a specific instance of mongod.

Test MongoDB server

  1. Start MongoDB using the following command:

    sudo service mongodb start
    # or
    sudo /etc/init.d/mongodb start
    
  2. Check the status of the server

    sudo service mongodb status
    # or
    sudo /etc/init.d/mongodb status
    

The command prints out the following text: [ ok ] Checking status of database: mongodb running. 3. Use mongoCLI:

```
$ mongo
MongoDB shell version: 3.2.11
connecting to: test
Welcome to the MongoDB shell.
For interactive help, type "help".
For more comprehensive documentation, see
        http://docs.mongodb.org/
Questions? Try the support group
        http://groups.google.com/group/mongodb-user
> help
        db.help()                    help on db methods
        db.mycoll.help()             help on collection methods
        sh.help()                    sharding helpers
        rs.help()                    replica set helpers
        help admin                   administrative help
        help connect                 connecting to a db help
        help keys                    key shortcuts
        help misc                    misc things to know
        help mr                      mapreduce

        show dbs                     show database names
        show collections             show collections in current database
        show users                   show users in current database
        show profile                 show most recent system.profile entries with time >= 1ms
        show logs                    show the accessible logger names
        show log [name]              prints out the last segment of log in memory, 'global' is default
        use <db_name>                set current database
        db.foo.find()                list objects in collection foo
        db.foo.find( { a : 1 } )     list objects in foo where a == 1
        it                           result of the last line evaluated; use to further iterate
        DBQuery.shellBatchSize = x   set default number of items to display on shell
        exit                         quit the mongo shell
> show dbs
local  0.000GB
>
```
  1. Type exitto quite mongo CLI.

  2. Run the following command to stop Mongo daemon service:

    sudo /etc/init.d/mongodb stop
    # or
    sudo service mongodb stop
    

Create objects

Using mongo CLI to insert some documents:

use appdb.users.insertOne(   { "_id" : "1",     "firstName" : "Raymond",     "lastName" : "Tang",     "roles" :  ["user","admin"]   })db.users.insertOne(   { "_id" : "2",     "firstName" : "Kontext",     "lastName" : "",     "roles" :  ["admin"]   })

The above code snippets creates a collection named usersin database app. Two user records are inserted into the collection.

> db.users.find(){ "_id" : "1", "firstName" : "Raymond", "lastName" : "Tang", "roles" : [ "user", "admin" ] }{ "_id" : "2", "firstName" : "Kontext", "lastName" : "", "roles" : [ "admin" ] }

The above command prints out the documents in the collection.

Fix issue

If mongodcannot be started successfully, try run the following command to repair and then restart the service:

sudo mongod --repair
sudo service mongodb start
mongodb windows10 wsl

Join the Discussion

View or add your thoughts below

Comments