Skip to content

Instantly share code, notes, and snippets.

@michaeltreat
Last active August 25, 2019 11:24
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save michaeltreat/a6d73f6a0fff6dadf3ea6a472191eb61 to your computer and use it in GitHub Desktop.
Save michaeltreat/a6d73f6a0fff6dadf3ea6a472191eb61 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. Open a Windows PowerShell and type wsl.
  2. Type cd ~ to go to the root of the Ubuntu File System.
  3. Paste sudo apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv 2930ADAE8CAF5059EE73BB4B58712A2291FA4AD5
  • This will "... import the MongoDB public GPG Key" so we can use the official MongoDB supported pkg in apt.
  1. Paste echo "deb [ arch=amd64,arm64 ] https://repo.mongodb.org/apt/ubuntu xenial/mongodb-org/3.6 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-3.6.list
  • This will add the deb to your sources list.
  • Note that you must be on Ubuntu Xenial. When Zesty gets LTS we will update this doc.
  1. Reload your local pkg database by typing sudo apt-get update.
  2. Run the install command by pasting sudo apt-get install -y mongodb-org.
  • This will install the most stable version of mongod, 3.6 at time of writing. If you want to install a different version please refer to the link above.

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 sudo 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 sudo 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.
  1. Open a new WSL window and type mongo.
  • You should see a connection notification on the first terminal window pop up.
  • 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!

Troubleshooting.

Here is a link to the Install Docs for MongoDB. Feel free to grab a TA or Instructor to help you with any debugging, or send me an email at michael.leon.treat@gmail.com if you are still stuck and I'll help you out

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