MongoDB is one of the most popular databases for modern Web Development, especially for those using JavaScript and JSON in their applications. It fits well into the MERN Stack structure, and provides dynamic and scalable performance across the board.
Windows Subsystem for Linux, or WSL is a service provided to developers that allows a Windows 10 user to have a full Linux kernel installed and running in a virtual machine under the hood, allowing for access to a complete Unix filesystem and command line while continuing to use Windows GUI apps.
While WSL provides a potentially fantastic service for developers (I use it extensively, myself), its nature of being a bit more of an experimental feature can sometimes leave documentation for specific tasks a bit hard to find. It's a Linux terminal, yes, but one running with specific concessions that occasionally make installing applications not as simple as the "Ubuntu installation instructions" might make them seem when you're Googling for a solution.
I'm writing this blog post because I specifically had a little trouble getting MongoDB installed and running on my WSL 2 installation, and wanted to put something out there to help others who might be trying to do the same thing.
Let's get started!
Installing MongoDB
First off, I'm going to assume that you're using Ubuntu as your WSL installation of choice. This is the most popular and commonly used Linux distro, and the one that I have experience with.
My examples will also be using Windows Terminal for my command line interaction, which I highly recommend for WSL. It's lightweight and customizable, and fits great with the workflow.
Step 1.) Update Ubuntu packages
This is just to make sure everything is up to date in your WSL installation. Run sudo apt update
in your terminal, enter your Ubuntu password, and let the process finish.
Step 2.) Install MongoDB on the command line
Install the current working package version of MongoDB by typing the following into your terminal:
sudo apt-get install mongodb
Type Y
to confirm when prompted, and a big installation process will take place.
Step 3.) Get familiar with MongoDB services
Once the installation is complete, you should now have access to two services on your command line:
mongod
mongo
mongod
starts your local database server and runs it on the default port (27017), and mongo
runs the MongoDB shell-- allowing you to connect with the database and interact with it manually.
Step 4.) Set up your local database
Now here's the tricky part, where the WSL version of MongoDB gets a little bit funky.
Normally, MongoDB's default directory for the database is /data/db
. However, if you run the mongod
command to start the server, you'll likely be hit with this error:
MongoDB hasn't created the folder for its database properly, so we're going to have to do it ourselves!
Important to note, for those new to Linux or Unix in general, your default starting place when you open up a new terminal window is ~
, which is your home directory. Any new folders or files you create there will show up as ~/code
or ~/pictures
, etc. This is different from the root directory, which is the actual core of your Linux installation. This is represented by a simple /
, and that's the directory where MongoDB is looking for its folders.
This is a small distinction, ~/
versus /
, but it makes all the difference in Unix.
To see this in the GUI, this is where the home folder lives in your WSL file structure:
Whereas the root directory lives here:
And this is where we'll need to put the /data/db
folder structure.
Step 5.) Creating the folders and permissions
Thankfully the solution is easy, once you know what to do!
Type cd /
into your terminal to navigate to your root directory, and type sudo mkdir -p data/db
. The -p
creates the parent folder(s) as well, if they don't already exist.
The folder now exists in your root directory! But we're not quite done. If you try to run mongod
now, you'll hit a new error:
This is because while we've created the folder, we now have to give it administrative write privileges, so that MongoDB can put its data there.
To do this, type sudo chown -R `id -un` data/db
. This will recursively give the proper permission to those folders.
Now, type mongod
again to start the server, and you're done!
The final line in the terminal should be something like "Waiting for connections on port 27017". Your server is now officially running!
Test it out by running the MongoDB shell with mongo
in a separate terminal tab, with the server still running:
If you see the >
character at the end, congratulations! You're now officially inside of the MongoDB shell and can interact with your local database.
I'll end the post here, since this was just a tutorial to get MongoDB installed and running properly, and another post on the functionality of Mongo would be a whole lot longer. I encourage you to take a look at their documentation and experiment with what they have to offer-- it's an incredible service, and a lot of fun to use!
I hope this was helpful to other WSL devs out there, and thanks for reading! ๐
Top comments (22)
Thank you very much. You saved me from wandering in random blogs.
However, I wanted to ask how would you connect this DB to some sort of data browser, like Compass?
It should be a little tricky because it shouldn't work properly, if you run compass from windows. Is that right?
ANSWER FOUND:
For who's wondering, you can use Mongo Compass installed on your Windows, with no problem.
If mongo compass is installed on windows, can I connect to MongoDB from wsl?
In my case the mongo console works but whenever I try to starts my local database server using mongod "685 abort (core dumped) mongod" this error I have tried
"mongod --repair" aswell it gives the same error.
logs
2021-04-12T00:50:25.375+0530 I CONTROL [initandlisten] MongoDB starting : pid=685 port=27017 dbpath=/data/db 64-bit host=LAPTOP-5PG6THJ3
2021-04-12T00:50:25.376+0530 I CONTROL [initandlisten] db version v3.6.8
2021-04-12T00:50:25.376+0530 I CONTROL [initandlisten] git version: 8e540c0b6db93ce994cc548f000900bdc740f80a
2021-04-12T00:50:25.376+0530 I CONTROL [initandlisten] OpenSSL version: OpenSSL 1.1.1f 31 Mar 2020
2021-04-12T00:50:25.377+0530 I CONTROL [initandlisten] allocator: tcmalloc
2021-04-12T00:50:25.377+0530 I CONTROL [initandlisten] modules: none
2021-04-12T00:50:25.377+0530 I CONTROL [initandlisten] build environment:
2021-04-12T00:50:25.378+0530 I CONTROL [initandlisten] distarch: x86_64
2021-04-12T00:50:25.378+0530 I CONTROL [initandlisten] target_arch: x86_64
2021-04-12T00:50:25.378+0530 I CONTROL [initandlisten] options: {}
2021-04-12T00:50:25.380+0530 I STORAGE [initandlisten] wiredtiger_open config: create,cache_size=3510M,session_max=20000,eviction=(threads_min=4,threads_max=4),config_base=false,statistics=(fast),cache_cursors=false,compatibility=(release="3.0",require_max="3.0"),log=(enabled=true,archive=true,path=journal,compressor=snappy),file_manager=(close_idle_time=100000),statistics_log=(wait=0),verbose=(recovery_progress),
2021-04-12T00:50:25.786+0530 E STORAGE [initandlisten] WiredTiger error (17) [1618168825:786509][685:0x7f2a915a0ac0], connection: /data/db/WiredTiger.wt: handle-open: open: File exists
2021-04-12T00:50:25.788+0530 I STORAGE [initandlisten] WiredTiger message unexpected file WiredTiger.wt found, renamed to WiredTiger.wt.13
2021-04-12T00:50:25.789+0530 E STORAGE [initandlisten] WiredTiger error (22) [1618168825:789685][685:0x7f2a915a0ac0], connection: /data/db/: directory-sync: fdatasync: Invalid argument
2021-04-12T00:50:25.789+0530 E STORAGE [initandlisten] WiredTiger error (-31804) [1618168825:789950][685:0x7f2a915a0ac0], connection: the process must exit and restart: WT_PANIC: WiredTiger library panic
2021-04-12T00:50:25.790+0530 F - [initandlisten] Fatal Assertion 28558 at src/mongo/db/storage/wiredtiger/wiredtiger_util.cpp 366
2021-04-12T00:50:25.790+0530 F - [initandlisten]
***aborting after fassert() failure
2021-04-12T00:50:25.800+0530 F - [initandlisten] Got signal: 6 (Aborted).
0x7f2a946e801e 0x7f2a946e790f 0x7f2a946e79ac 0x7f2a918b23c0 0x7f2a916e618b 0x7f2a916c5859 0x7f2a92e53c55 0x7f2a92bfb8ee 0x7f2a92f90089 0x7f2a92c05ec8 0x7f2a92c06228 0x7f2a92c01cc0 0x7f2a92f59cb8 0x7f2a92f5a4eb 0x7f2a92f5752c 0x7f2a92faf54a 0x7f2a92f776b7 0x7f2a92f54fcd 0x7f2a92f34c42 0x7f2a92f07c70 0x7f2a92eed559 0x7f2a930d0fe6 0x7f2a92ec72b9 0x7f2a92eca7e3 0x7f2a92e5861d 0x7f2a916c70b3 0x7f2a92eb4d4e
----- BEGIN BACKTRACE -----
{"backtrace":[{"b":"7F2A923DD000","o":"230B01E","s":"ZN5mongo15printStackTraceERSo"},{"b":"7F2A923DD000","o":"230A90F"},{"b":"7F2A923DD000","o":"230A9AC"},{"b":"7F2A9189D000","o":"153C0"},{"b":"7F2A916A0000","o":"4618B","s":"gsignal"},{"b":"7F2A916A0000","o":"25859","s":"abort"},{"b":"7F2A923DD000","o":"A76C55","s":"_ZN5mongo32fassertFailedNoTraceWithLocationEiPKcj"},{"b":"7F2A923DD000","o":"81E8EE"},{"b":"7F2A923DD000","o":"BB3089"},{"b":"7F2A923DD000","o":"828EC8","s":"wt_err"},{"b":"7F2A923DD000","o":"829228","s":"wt_panic"},{"b":"7F2A923DD000","o":"824CC0"},{"b":"7F2A923DD000","o":"B7CCB8"},{"b":"7F2A923DD000","o":"B7D4EB"},{"b":"7F2A923DD000","o":"B7A52C","s":"wt_open"},{"b":"7F2A923DD000","o":"BD254A","s":"wt_block_manager_create"},{"b":"7F2A923DD000","o":"B9A6B7","s":"wt_schema_create"},{"b":"7F2A923DD000","o":"B77FCD","s":"wt_turtle_init"},{"b":"7F2A923DD000","o":"B57C42","s":"wiredtiger_open"},{"b":"7F2A923DD000","o":"B2AC70","s":"_ZN5mongo18WiredTigerKVEngineC2ERKNSt7cxx1112basic_stringIcSt11char_traitsIcESaIcEEES8_PNS_11ClockSourceES8_mbbbb"},{"b":"7F2A923DD000","o":"B10559"},{"b":"7F2A923DD000","o":"CF3FE6","s":"_ZN5mongo20ServiceContextMongoD29initializeGlobalStorageEngineEv"},{"b":"7F2A923DD000","o":"AEA2B9"},{"b":"7F2A923DD000","o":"AED7E3","s":"_ZN5mongo11mongoDbMainEiPPcS1"},{"b":"7F2A923DD000","o":"A7B61D","s":"main"},{"b":"7F2A916A0000","o":"270B3","s":"libc_start_main"},{"b":"7F2A923DD000","o":"AD7D4E","s":"_start"}],"processInfo":{ "mongodbVersion" : "3.6.8", "gitVersion" : "8e540c0b6db93ce994cc548f000900bdc740f80a", "compiledModules" : [], "uname" : { "sysname" : "Linux", "release" : "4.4.0-19041-Microsoft", "version" : "#488-Microsoft Mon Sep 01 13:43:00 PST 2020", "machine" : "x86_64" }, "somap" : [ { "b" : "7F2A923DD000", "elfType" : 3, "buildId" : "060194AE9AEEF88E6F109520AE16DEC315715B58" }, { "b" : "7FFFEE68B000", "path" : "linux-vdso.so.1", "elfType" : 3 }, { "b" : "7F2A92340000", "path" : "/lib/x86_64-linux-gnu/libstemmer.so.0d", "elfType" : 3, "buildId" : "F129B7D409F6F63A822673139BE40F369F3E4B13" }, { "b" : "7F2A92320000", "path" : "/lib/x86_64-linux-gnu/libz.so.1", "elfType" : 3, "buildId" : "7A04C793A2CE7B847F43840CA650E0D9DD35389D" }, { "b" : "7F2A92310000", "path" : "/lib/x86_64-linux-gnu/libsnappy.so.1", "elfType" : 3, "buildId" : "74CAFD320059B4D17CF89FD6379A5105FF55840F" }, { "b" : "7F2A922B0000", "path" : "/lib/x86_64-linux-gnu/libyaml-cpp.so.0.6", "elfType" : 3, "buildId" : "B9EC1711A8AAD2FE980B735F13E186BEFE9FA4DB" }, { "b" : "7F2A922A0000", "path" : "/lib/x86_64-linux-gnu/libpcrecpp.so.0", "elfType" : 3, "buildId" : "90A529AE5D50FADDD9CC20E87A71C65613B3C28F" }, { "b" : "7F2A92210000", "path" : "/lib/x86_64-linux-gnu/libboost_program_options.so.1.71.0", "elfType" : 3, "buildId" : "CB2EB21D65AF49F187F384ABED5D46D66E998195" }, { "b" : "7F2A921E0000", "path" : "/lib/x86_64-linux-gnu/libboost_filesystem.so.1.71.0", "elfType" : 3, "buildId" : "956195B5ABA44A072A6242BF2399D9EB0B6163E7" }, { "b" : "7F2A91FE0000", "path" : "/lib/x86_64-linux-gnu/libtcmalloc.so.4", "elfType" : 3, "buildId" : "BC036BA2C0C0B4976A51184F1E9F712CCB8611C7" }, { "b" : "7F2A91FC0000", "path" : "/lib/x86_64-linux-gnu/libresolv.so.2", "elfType" : 3, "buildId" : "6BCE6EF9E884DDA05E19F944A6C6BF85C71D09E8" }, { "b" : "7F2A91CE0000", "path" : "/lib/x86_64-linux-gnu/libcrypto.so.1.1", "elfType" : 3, "buildId" : "D30ABD770D1215FFF0F9A0FA9F12B1DE5B50DA29" }, { "b" : "7F2A91C40000", "path" : "/lib/x86_64-linux-gnu/libssl.so.1.1", "elfType" : 3, "buildId" : "4EF02CF97DD73CB0A88495E6DBF584DD6AA5AA22" }, { "b" : "7F2A91C30000", "path" : "/lib/x86_64-linux-gnu/libdl.so.2", "elfType" : 3, "buildId" : "A3F78028221CF3B784DD5E10D05AD7A55A6A85D5" }, { "b" : "7F2A91A30000", "path" : "/lib/x86_64-linux-gnu/libstdc++.so.6", "elfType" : 3, "buildId" : "C45813107D1487F8FC7197424AA6F1E0BE00FE6C" }, { "b" : "7F2A918E1000", "path" : "/lib/x86_64-linux-gnu/libm.so.6", "elfType" : 3, "buildId" : "44B3FC3330F57F48B1EED34A9A8E982C6D1EF76D" }, { "b" : "7F2A918C0000", "path" : "/lib/x86_64-linux-gnu/libgcc_s.so.1", "elfType" : 3, "buildId" : "64E719943721B9650519ABCF9EEFD94A7135DECD" }, { "b" : "7F2A9189D000", "path" : "/lib/x86_64-linux-gnu/libpthread.so.0", "elfType" : 3, "buildId" : "E54761F7B554D0FCC1562959665D93DFFBEBDAF0" }, { "b" : "7F2A916A0000", "path" : "/lib/x86_64-linux-gnu/libc.so.6", "elfType" : 3, "buildId" : "099B9225BCB0D019D9D60884BE583EB31BB5F44E" }, { "b" : "7F2A91620000", "path" : "/lib/x86_64-linux-gnu/libpcre.so.3", "elfType" : 3, "buildId" : "AC83711FBCB2725AD376CA8F8C947F6A782E7A65" }, { "b" : "7F2A915F0000", "path" : "/lib/x86_64-linux-gnu/libunwind.so.8", "elfType" : 3, "buildId" : "E4502D71B7D21D13E84436FAF0BD4A8FF63874C2" }, { "b" : "7F2A923A0000", "path" : "/lib64/ld-linux-x86-64.so.2", "elfType" : 3, "buildId" : "5374B5558386B815E69CC1838A6052CC9B4746F3" }, { "b" : "7F2A915C0000", "path" : "/lib/x86_64-linux-gnu/liblzma.so.5", "elfType" : 3, "buildId" : "533783B82D9BEDA5891A5E10B15D43ADE31085CC" } ] }}
mongod(_ZN5mongo15printStackTraceERSo+0x3E) [0x7f2a946e801e]
mongod(+0x230A90F) [0x7f2a946e790f]
mongod(+0x230A9AC) [0x7f2a946e79ac]
libpthread.so.0(+0x153C0) [0x7f2a918b23c0]
libc.so.6(gsignal+0xCB) [0x7f2a916e618b]
libc.so.6(abort+0x12B) [0x7f2a916c5859]
mongod(_ZN5mongo32fassertFailedNoTraceWithLocationEiPKcj+0x0) [0x7f2a92e53c55]
mongod(+0x81E8EE) [0x7f2a92bfb8ee]
mongod(+0xBB3089) [0x7f2a92f90089]
mongod(wt_err+0xA5) [0x7f2a92c05ec8]
mongod(wt_panic+0x38) [0x7f2a92c06228]
mongod(+0x824CC0) [0x7f2a92c01cc0]
mongod(+0xB7CCB8) [0x7f2a92f59cb8]
mongod(+0xB7D4EB) [0x7f2a92f5a4eb]
mongod(wt_open+0x31C) [0x7f2a92f5752c]
mongod(wt_block_manager_create+0x6A) [0x7f2a92faf54a]
mongod(wt_schema_create+0x5C7) [0x7f2a92f776b7]
mongod(wt_turtle_init+0x7AD) [0x7f2a92f54fcd]
mongod(wiredtiger_open+0x2012) [0x7f2a92f34c42]
mongod(_ZN5mongo18WiredTigerKVEngineC2ERKNSt7cxx1112basic_stringIcSt11char_traitsIcESaIcEEES8_PNS_11ClockSourceES8_mbbbb+0x8C0) [0x7f2a92f07c70]
mongod(+0xB10559) [0x7f2a92eed559]
mongod(ZN5mongo20ServiceContextMongoD29initializeGlobalStorageEngineEv+0x3E6) [0x7f2a930d0fe6]
mongod(+0xAEA2B9) [0x7f2a92ec72b9]
mongod(_ZN5mongo11mongoDbMainEiPPcS1+0x613) [0x7f2a92eca7e3]
mongod(main+0xD) [0x7f2a92e5861d]
libc.so.6(__libc_start_main+0xF3) [0x7f2a916c70b3]
mongod(_start+0x2E) [0x7f2a92eb4d4e]
----- END BACKTRACE -----
[1] 685 abort (core dumped) mongod
Same thing happened to me. My solution was to update to WSL 2. You may have to enable virtualization in your BIOS. The link below helped me. Keep in mind that if you need to enable virtualization, the command to do it will tell you, but it doesn't look like an error. That kept me going in the wrong direction for a bit.
tenforums.com/tutorials/164301-how...
Thank you!!
By the way, could you please explain a little more what does this command do?
sudo chown -R 'id -un' data/db
.What does
id -un
stands for? Who is this guy? ๐I think that's just a way of telling the chown command to change ownership to "current user", as in the user issuing the command at the moment. Note that those are backticks, not single quotes.
unix.stackexchange.com/questions/2...
I get this stuff after step 5
2022-02-01T23:39:04.899+0200 E NETWORK [initandlisten] Failed to unlink socket file /tmp/mongodb-27017.sock Operation not permitted
2022-02-01T23:39:04.899+0200 F - [initandlisten] Fatal Assertion 40486 at src/mongo/transport/transport_layer_asio.cpp 169
2022-02-01T23:39:04.899+0200 F - [initandlisten]
***aborting after fassert() failure
This solved it for me for the time being
stackoverflow.com/questions/298136...
How do you connect to GUI apps like Mongo Compass or Studio 3T over WSL?
I found a way. Stop mongodb service from native Windows and start the mongodb service on WSL.
Brilliant, I can't believe I didn't think of that. Thanks that makes a big difference.
Awesome job!
How can I use mongodb (which is installed in WSL2) in VsCode while connecting it to nodejs.
Will it identify the ports?
Thanks ! this helped me a lot
Just wanted to say THANK YOU! . I saw the error but was not sure if creating the folder would solve the problem from the root, I feel safe now.
This was really helpful, thanks!