Reputation: 303
I was trying to install MongoDB in WSL running Ubuntu 18.04 after seeing this documentation. But MongoDB says that:
IMPORTANT
Themongodb
package provided by Ubuntu is not maintained by MongoDB Inc. and conflicts with the officialmongodb-org
package. If you have already installed the mongodb package on your Ubuntu system, you must first uninstall the mongodb package before proceeding with these instructions.
MongoDB also says that WSL doesn't support mongodb-org
WINDOWS SUBSYSTEM FOR LINUX (WSL) - UNSUPPORTED
MongoDB does not support the Windows Subsystem for Linux (WSL).
So, I installed mongod
using sudo apt install mongodb
and when I ran mongo
it shows the error:
connecting to: mongodb://127.0.0.1:27017/?compressors=disabled&gssapiServiceName=mongodb
2020-06-21T13:33:40.606+0530 E QUERY [js] Error: couldn't connect to server 127.0.0.1:27017
Is there is a way to install the appropriate package and make it running?
Upvotes: 23
Views: 52261
Reputation: 176
I was able to make bonyem's solution work on Ubuntu 22 just now with the following addition for local dev.
sudo service mongod stop
sudo rm -rf /tmp/mongod*
sudo mkdir -p /var/lib/mongodb
sudo chmod -R a+wxr /var/lib/mongodb
sudo chown -R mongodb:mongodb /var/lib/mongodb
Upvotes: 1
Reputation: 1218
I have come across this situation recently.
Even though MongoDB says that it is not supported in WSL, you can actually install it. So, I suggest you follow that steps given in MongoDB docs.
Note : If you have already installed mongodb
please remove all those before you install mongodb-org
since it may cause some issues during installation :
sudo dpkg --remove --force-remove-reinstreq mongo-tools
sudo dpkg --remove --force-remove-reinstreq mongodb-server-core
sudo apt-get --fix-broken install
For installing mongodb community edition, I have added the commands below:
wget -qO - https://www.mongodb.org/static/pgp/server-4.2.asc | sudo apt-key add -
sudo apt-get install gnupg
wget -qO - https://www.mongodb.org/static/pgp/server-4.2.asc | sudo apt-key add -
echo "deb [ arch=amd64,arm64 ] https://repo.mongodb.org/apt/ubuntu bionic/mongodb-org/4.2 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-4.2.list
sudo apt-get update
sudo apt-get install -y mongodb-org
Now, to get mongoDB running,
sudo nano /etc/init.d/mongod
and paste the contents in this link into the file and save it.
#give permissions
sudo chmod +x /etc/init.d/mongod
#start the service
sudo service mongod start
Now, you can run mongo
to reach the database.
Upvotes: 66
Reputation: 126
I was running into an error when using @bonyem's solution. I saw this error in the logs that it did not have permission to write to /var/run/mongod.pid
. I saw this by running sudo cat /var/log/mongodb/mongod.log
I had to make a few permission changes and changes to the init.d script to get it to work. first I changed this line PIDFILE=/var/run/$NAME.pid
to PIDFILE=/var/run/mongodb/$NAME.pid
.
Then ran the command sudo chown -R mongodb:mongodb /var/run/mongodb/
I tried it at first without the -R and each time the .pid file was created it was owned by root. Adding the -R seemed to permanently make new files owned by mongodb. @user296656's answer helped me figure that out!
After doing that running sudo service mongodb start
worked and the error was gone in the logs.
Upvotes: 0
Reputation: 2720
I tried all the methods above but none of the methods worked. I came across Microsoft's official documentation and everything works like a charm.
https://learn.microsoft.com/en-us/windows/wsl/tutorials/wsl-database#install-mongodb
Upvotes: 7
Reputation: 562
Easiest way to bypass this issue is to run MongoDB on Windows. You'll still be able to connect from WSL2 over localhost.
For me, @bonyem's solution resulted in an error:
System has not been booted with systemd as init system (PID 1). Can't operate.
Failed to connect to bus: Host is down
I'm going to install Mongo 4.4. Many instructions were taken from @Lyubosumaz's directions here.
Get rid of all your Mongo PPAs.
~ ❯ sudo ls /etc/apt/sources.list.d
mongodb-org-4.4.list yarn.list
~ ❯ sudo rm -i /etc/apt/sources.list.d/mongodb-org-4.4.list
Remove all Mongo packages and bits and pieces.
sudo rm -r /var/log/mongodb
sudo rm -r /var/lib/mongodb
sudo dpkg --remove --force-remove-reinstreq mongo-tools
sudo dpkg --remove --force-remove-reinstreq mongodb-org
sudo dpkg --remove --force-remove-reinstreq mongodb-org-server
sudo dpkg --remove --force-remove-reinstreq mongodb-server-core
sudo dpkg --remove --force-remove-reinstreq mongodb-org-mongos
sudo dpkg --remove --force-remove-reinstreq mongodb-org-shell
sudo dpkg --remove --force-remove-reinstreq mongodb-org-tools
sudo apt-get --fix-broken install
yes | sudo apt autoremove
Add new PPA and install Mongo 4.4.
wget -qO - https://www.mongodb.org/static/pgp/server-4.4.asc | sudo apt-key add -
echo "deb [ arch=amd64,arm64 ] https://repo.mongodb.org/apt/ubuntu focal/mongodb-org/4.4 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-4.4.list
sudo apt-get update
sudo apt-get upgrade
sudo apt-get install -y mongodb-org
You'll get the same error from before. This is caused by mongodb-org package trying to use systemctl
to start the DB after install (source). Notice that the same error is thrown when running the command manually.
~ ❯ sudo systemctl start mongod
System has not been booted with systemd as init system (PID 1). Can't operate.
Failed to connect to bus: Host is down
However, running mongod
should still work!
If you run into errors when running mongod
, you might need to modify some permissions and create a data directory. I found the answer to these issues here, here, and here.
sudo chown $USER /tmp/mongodb-27017.sock
sudo mkdir -p /data/db
sudo chown -R $USER /data/db
Upvotes: 4
Reputation: 101
I have tried some of the answers above, it did not work for me. Then I followed a guide 1:
It worked in my case!
Upvotes: 1
Reputation: 3273
This worked for me.
Run sudo apt install mongodb
Then after that installs run sudo service mongodb start
It will say * Starting database mongodb
And after that I can connect using mongo
Upvotes: 7
Reputation: 444
@bonyem's solution works if you try on Ubuntu 18.04 (and apparently in 19.10) but not in 20.04. WSL version should be 2 (please see this to update WSL).
You also need to "fake" the bash binary by following the steps mentioned in https://github.com/shayne/wsl2-hacks to make systemctl work.
Upvotes: 7