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.
- Follow the directions listed on the mongodb official website for your distro of choice.
- Make sure you are still on the root of the Ubuntu FS by typing
cd ~
. If you type pwd it should output/home/<user>/
- 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.
- 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
- 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.
- Follow this link to test some commands in your new mongo database.
- To exit the shell, press
ctrl + c
. You should get a neat message, then you'll be returned to your command line!
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:Save the file and reload the
.bashrc
or.zshrc
by typing:source ~/.bashrc
orsource ~/.zshrc
.You can now start mongodb with the
mongostart
command.