Skip to content

Instantly share code, notes, and snippets.

@Mikeysax
Forked from michaeltreat/WSL_Mongo_install.md
Last active December 31, 2021 06:53
Show Gist options
  • Star 28 You must be signed in to star a gist
  • Fork 7 You must be signed in to fork a gist
  • Save Mikeysax/cc86c30903727c556bcce960f7e4d59b to your computer and use it in GitHub Desktop.
Save Mikeysax/cc86c30903727c556bcce960f7e4d59b to your computer and use it in GitHub Desktop.

Install Mongo with WSL for Windows.

Home

This doc will guide you through installing Mongo DB using WSL through the Command Line.

Most of the steps are listed out here, but this guide will trim them down and make it more straight forward for our needs. There is also 1 step that is not in the link above as well, which will be noted when we come across it.

Install MongoDB Community Edition.

  1. Follow the directions listed on the mongodb official website for your distro of choice.

Add the data/db/ directories. NOTE: This step is not included in the link above.

  1. Make sure you are still on the root of the Ubuntu FS by typing cd ~. If you type pwd it should output /home/<user>/
  2. Type mkdir -p data/db
  • This will make a data directory with a db sub directory.
  • The -p flag means make the parent directory if it doesn't exist.

Run mongod server and mongo shell.

  1. Open a new WSL window and type mongod --dbpath ~/data/db.
  • You should see a bunch of stuff pop up, but the last line should be something like waiting for connections on port 27017.
  • This will run your mongod service for you, allowing users to connect to it.
  • The command --dbpath will change the path where mongo saves your databases and records. You can choose your own location if you want, but in the section above we just added it to the root of your Ubuntu File System.
  • Tip: You can add an alias to your .bashrc or .zshrc to make a shortcut for the command in step 1: alias mongostart="mongod --dbpath ~/data/db
  1. Open a new WSL window and type mongostart.
  • The server should start and output several lines to indicate that it is running. This terminal needs to be kept open for the mongodb server to run.
  • To enter the mongo shell, you can now open a new terminal and type mongo which will launch the new terminal into the shell.
  • In the new window you should see some messages pop up and finally your command line should be changed to a > symbol which means that you are in the mongo shell.
  1. Follow this link to test some commands in your new mongo database.
  2. To exit the shell, press ctrl + c. You should get a neat message, then you'll be returned to your command line!
@scotthernandez
Copy link

I found this gist following a WSL issue about starting mongodb.

It is not a good idea to run mongodb as root, or for that matter anything you don't need to. I'd suggest changing this doc to not have data/db be owned by root, nor start mongod as root. There is no need for sudo at all once you have installed the binaries, since all data will be in the home directory of the current users...

@gerhynes
Copy link

I got as far as step 5 and then ran into this error:

E: Type '"deb' is not known on line 1 in source list /etc/apt/sources.list.d/mongodb-org-3.4.list
E: The list of sources could not be read.

Any suggestions?

@w-biggs
Copy link

w-biggs commented Nov 21, 2018

service mongod start still doesn't work after following these steps, unfortunately.

@deavy
Copy link

deavy commented Nov 23, 2018

You can also run mongod service with sudo mongod --fork --config /etc/mongod.conf command.

@DavidCannon
Copy link

I was getting dependency errors on step 6 when using Mongo 3.6 in step 4. So, I changed it to Mongo 4.0, and it resolved it for me and installed successfully. If you're having a similar issue, try changing step 4 to use version 4.0 (or the latest version), like this:

echo "deb [ arch=amd64,arm64 ] https://repo.mongodb.org/apt/ubuntu xenial/mongodb-org/4.0 multiverse" | sudo t
ee /etc/apt/sources.list.d/mongodb-org-4.0.list

@pikadun
Copy link

pikadun commented Mar 29, 2019

You can also run mongod service with sudo mongod --fork --config /etc/mongod.conf command.

how to restart it?

@rcgalbo
Copy link

rcgalbo commented Aug 10, 2019

i get the error message aborted (core dumped)1 when i try running service mongod start or sudo mongod --dbpath ~/data/db

@Mikeysax
Copy link
Author

Mikeysax commented Aug 12, 2019

FYI, I've updated the instructions to be more streamlined.

For anyone looking here, you cannot start mongodb with the service command in WSL. You need to use the command above to start the server. You can make an alias to run the command so you do not have to type in the full command each time by editing your ~/.bashrc, or if you use zsh, ~/.zshrc, and write the following:

alias mongostart="mongod --dbpath ~/data/db

Save the file and reload the .bashrc or .zshrc by typing: source ~/.bashrc or source ~/.zshrc.

You can now start mongodb with the mongostart command.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment