Installing MongoDB in WSL - mongodb

I was trying to install MongoDB in WSL running Ubuntu 18.04 after seeing this documentation. But MongoDB says that:
IMPORTANTThe mongodb package provided by Ubuntu is not maintained by MongoDB Inc. and conflicts with the official mongodb-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) - UNSUPPORTEDMongoDB 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?

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.
Hope it helps.

#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.

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

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

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

I have tried some of the answers above, it did not work for me.
Then I followed a guide 1:
Update ubuntu packages
Install MongoDB on the command line
Set up your local database
Creating the folders and permissions
It worked in my case!

Related

MongoDB failed (result: core-dump)?

I'm attempting to setup MongoDB on Ubuntu 20.04.02 LTS by following the documentation. I have ensured this is a fresh install. However an error persists when I verify if the MongoDB install started succesfully by typing in:
sudo systemctl status mongod
● mongod.service - MongoDB Database Server
Loaded: loaded (/lib/systemd/system/mongod.service; disabled; vendor prese>
Active: failed (Result: core-dump) since Wed 2021-08-11 12:59:20 UTC; 49s >
Docs: https://docs.mongodb.org/manual
Process: 3190 ExecStart=/usr/bin/mongod --config /etc/mongod.conf (code=dum> Main PID: 3190 (code=dumped, signal=ILL)
Aug 11 12:59:19 discorddomagoj systemd1: Started MongoDB Database
Server. Aug 11 12:59:20 discorddomagoj systemd1: mongod.service:
Main process exited,> Aug 11 12:59:20 discorddomagoj systemd1:
mongod.service: Failed with result '>
Picture of problem for clarity:
1.Stop the mongod process by issuing the following command:-
sudo service mongod stop
2.Remove any MongoDB packages that you had previously installed:-
sudo apt-get purge mongodb-org*
3.Remove MongoDB databases and log files:-
sudo rm -r /var/log/mongodb
sudo rm -r /var/lib/mongodb
4.Then reinstall mangodb 4.4.8
5.Import the public key used by the package management system:-
wget -qO - https://www.mongodb.org/static/pgp/server-4.4.asc | sudo apt-key add -
6.The following instruction is for Ubuntu 20.04 (Focal):-
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
7.Update Apt
sudo apt-get update
8.Install mongodb
sudo apt-get install mongodb-org=4.4.8 mongodb-org-server=4.4.8 mongodb-org-shell=4.4.8 mongodb-org-mongos=4.4.8 mongodb-org-tools=4.4.8
9.Use mongod --version to check its succesfully installed
10.If u encounter any error while using mongod
sudo mkdir /data
cd /data
sudo mkdir db
sudo pkill -f mongod
11.Then use sudo mongod command.
This error show CPU incompatibly with mongodb. if use proxmox set cpu mode to "HOST".
stop the db
sudo service mongod stop
uninstall it:
sudo apt-get purge mongodb-org*
Import the public key used by the package management system
wget -qO - https://www.mongodb.org/static/pgp/server-5.0.asc | sudo apt-key add -
Create a list file for MongoDB.
echo "deb [ arch=amd64,arm64 ] https://repo.mongodb.org/apt/ubuntu focal/mongodb-org/5.0 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-5.0.list
Reload local package database
sudo apt-get update
Install a specific release of the MongoDB packages(this was the reason I had that similar error for not specifying the package)
sudo apt-get install -y mongodb-org=5.0.7 mongodb-org-database=5.0.7 mongodb-org-server=5.0.7 mongodb-org-shell=5.0.7 mongodb-org-mongos=5.0.7 mongodb-org-tools=5.0.7
lastly, on one tab of the terminal run:
sudo mongod
on another open the db:
mongo
I have same issue when installing mongodb on virtual machine with proxmox.
Changing processor from kvm to host or sandybridge(my actual pc) solved this problem. Reference link
This is an addition to #AbdulWahhab 's answer. I have recently seen many questions of same type: 'can't start mongodb#5.0.x server on ubuntu#20.0.4'. Having faced this issue myself, I found installing mongodb#4.4.8 version only way out. In addition consider that sudo apt-get update updates all packages including mongodb. Newer version of mongodb gets installed and server does not work anymore(as of 10th december 2021). To prevent unintended upgrades, you can pin the package at the currently installed version:
echo "mongodb-org hold" | sudo dpkg --set-selections
echo "mongodb-org-database hold" | sudo dpkg --set-selections
echo "mongodb-org-server hold" | sudo dpkg --set-selections
echo "mongodb-org-shell hold" | sudo dpkg --set-selections
echo "mongodb-org-mongos hold" | sudo dpkg --set-selections
echo "mongodb-org-tools hold" | sudo dpkg --set-selections
A possible reason for the dump is a change in the microarchitecture requirements of MongoDB introduced with version 5.0. This causes this exact issue on a Raspberry Pi for instance (see: MongoDB Forum).
This issue might be mitigated by building from source as described in the forum post. I haven't tried this though personally. Installing versions prior to 5.0 (as described by Abdul Wahhab) will solve the problem as well.
If you have tried all of the above solutions, and still can't help, please try to reboot the VM.
I am using 18.04 and initially installed 6.0 mongoDB and it just showed the core-dump error after issuing "sudo systemctl status mongod".
After doing some research, I tried to uninstall the 6.0 and then re-install 4.4, it still couldn't help.
Then I tried to re-install 4.2, it still couldn't help.
Finally, I tried to reboot the VM, then it works..
For MongoDB 5.0+
If you are running on VirtualBox with a Windows host, disable Hyper-V. The issue is a lack of AVX support and to get it you need to disable Hyper-V and be using VirtualBox 5.03 or later.
Using CMD as Administrator
bcdedit /set hypervisorlaunchtype off
DISM /Online /Disable-Feature:Microsoft-Hyper-V
Then hard shutdown and restart the host. After that, starting the guest from cold boot should work.
sudo systemctl start mongodb
sudo systemctl status mongodb
Source: https://forums.virtualbox.org/viewtopic.php?f=25&t=99390
I had the same problem on Ubuntu 22.10 and MongoDB 6.0.
I solved it by put it AVX to AUTO in BIOS.
Just for future help in case of someone else need help.
check if you have the java runtime environment (JRE), if not then install it:
sudo apt install default-jre
That' s all =)

How to upgrade to PostgreSQL 11 for Ubuntu 18.04?

As the title suggests I want to upgrade my postgresql-10 to postgresql-11.
I'm using ubuntu-18.04.
You can follow this blog setup Postgresql-11 on Ubuntu. I found it easy and simple.
Add the PostgreSQL package repository on your Ubuntu machine
echo "deb http://apt.postgresql.org/pub/repos/apt/ $(lsb_release -cs)-pgdg main 11" | sudo tee /etc/apt/sources.list.d/pgsql.list
Add the GPG key of the PostgreSQL package repository:
wget --quiet -O - https://www.postgresql.org/media/keys/ACCC4CF8.asc | sudo apt-key add -
Update APT and install postgresql-11
sudo apt update && sudo apt install postgresql-11
Upgrading Postgres to the latest version (currently 13), or to an intermediate version such as 11, should be done by running:
sudo apt install postgresql-common
sudo sh /usr/share/postgresql-common/pgdg/apt.postgresql.org.sh
This is documented on https://wiki.postgresql.org/wiki/Apt
This runs a bash script on your computer. See there if you wish to run the steps manually.
Once you have installed Postgres, the easiest way to upgrade on Ubuntu is with pg_upgradecluster.
Backup your data. You will dropping the database, so no games!
sudo -u postgres pg_dumpall > all.sql
Upgrade.
// Install latest Postgres. Use `postgresql-11` for v11 instead of `postgresql` for latest.
sudo apt-get install -y postgresql
// The install sets up a cluster, which needs then to be removed for the upgrade.
// Stop and remove the newly installed cluster. Use `11` instead of `13` for v11
sudo pg_dropcluster 13 main --stop
// Upgrade the db. Takes the OLD version and OLD schema as required arguments
sudo pg_upgradecluster 10 main
// Test. Once you are satisfied, remove OLD cluster.
sudo pg_dropcluster 10 main
Use this command:
sudo apt update && sudo apt install postgresql-11

Failed to start mongod.service: Unit mongod.service not found

I follow all the steps mention in MongoDB installation documents for Ubuntu 16.04.
Steps 1:
sudo apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv 2930ADAE8CAF5059EE73BB4B58712A2291FA4AD5
Steps 2:
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
Steps 3:
sudo apt-get update
Steps 4:
sudo apt-get install -y mongodb-org
Steps 5:
sudo service mongod start
when I started MongoDB got an error as:
Failed to start mongod.service: Unit mongod.service not found.
Most probably unit mongodb.service is masked. Use following command to unmask it.
sudo systemctl unmask mongod
and re-run
sudo service mongod start
Please follow the below steps, it should work.
1 - Uninstall current installation completely
Source - official instructions
sudo service mongod stop
Remove Packages
sudo apt-get purge mongodb-org*
Remove the folders
sudo rm -r /var/log/mongodb
sudo rm -r /var/lib/mongodb
2 - Reinstall as described on official site, I will just write down the all steps. enter link description here
Import the public key
sudo apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv 2930ADAE8CAF5059EE73BB4B58712A2291FA4AD5
Create a list file for Ubuntu 16.04
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
update the list
sudo apt-get update
Install the latest package
sudo apt-get install -y mongodb-org
3 - Now it should work, please try below command
sudo service mongod start
and check the status
mongo
it should appear the mongo shell
You are missing a 'b' I think?
sudo service mongod start
should be
sudo service mongodb start
I think this is the case?
Failed to start mongod.service: Unit mongod.service not found
If you are following the official doc and are coming across with the error above that means mongod.service is not enabled yet on you machine (I am talking about Ubuntu 16.04). You need to do that using following command
sudo systemctl enable mongod.service
Now you can start mongodb using the following command
sudo service mongod start
[Solution]
Just type
sudo mongod
It will work.
Case with me
Worked fine after the install; this problem occurred after I rebooted.
What worked for me is
sudo mkdir /var/lib/mongodb
sudo mkdir /var/log/mongodb
sudo chown -R mongodb:mongodb /var/lib/mongodb
sudo chown -R mongodb:mongodb /var/log/mongodb
After running the above commands open a new terminal and run:
sudo systemctl restart mongod
and run the mongo shell to check:
mongo
Note
If this problem occurs again (as with me every time I reboot), make it as a shell script to be run at startup.
Permanent solution (shell script at startup)
Step 1:
Make a file in the /etc/init.d directory
sudo gedit /etc/init.d/custom-mongo-fix.sh
Step 2:
Put below code in the file you just opened in Step 1
#!/bin/bash
# mongodb fix
sudo mkdir /var/lib/mongodb
sudo mkdir /var/log/mongodb
sudo chown -R mongodb:mongodb /var/lib/mongodb
sudo chown -R mongodb:mongodb /var/log/mongodb
Step 3:
After the next boot you can normally start the mongod server by:
sudo systemctl start mongod
Explanation: mongodb requires the above two folders with owner as mongodb. When we shutdown/reboot, one or both of them gets removed. So, we need to create them and change their ownership.
Edit: I was using Stacer's cleaning tool before every shutdown. This also removes log files and folders. And hence, the error at every startup. Now I unselect the mongodb option and then clean.
Namaste. 🙏
$service mongodb start
$service mongodb status
the status is active when I started using above command
For Ubuntu 16.04.5, it is noticed that MongoDB installations does not auto enable the mongod.service file after installation in my case after I have installed on several servers, so we need to enable it like below:
Issue below to check whether mongod is enabled
systemctl list-unit-files --type=service
If it shows as "disabled", then you need to enable it.
sudo systemctl enable mongod.service
If we want to see whether mongod.service file exists in case this file is missing, check in
ls /lib/systemd/system
You will see a file
/lib/systemd/system/mongod.service
This happened to me while adding users in admin db.
sudo systemctl daemon-reload
is recommended from the official mongo website, but did not help.
If you get
Job for mongod.service failed because the control process exited with error code. See "systemctl status mongod.service" and "journalctl -xe" for details.
While executing
sudo systemctl start mongod
run journalctl -xe and if the issue is
Failed to start MongoDB Database Server.
-- Subject: Unit mongod.service has failed
Then run sudo mongod --> mongo
This will fix the issue and if you want to run with systemctl, then terminate the windows for mongod and mongo and run
sudo systemctl start mongod
and then mongo
Just try with this command:
$ sudo systemctl enable mongod
You can repair
$ sudo rm /var/lib/mongodb/mongod.lock
$ mongod --repair
$ sudo service mongodb start
For Stop
$ sudo service mongodb stop
For Restart-
$ sudo service mongodb restart
For Status-
$ sudo service mongodb status
As per documentation:
Run this command to reload the daemon:
sudo systemctl daemon-reload
After this you need to restart the mongod service:
sudo systemctl start mongod
To verify that MongoDB has started, run:
sudo systemctl status mongod
To ensure that MongoDB will start following a system reboot, run:
sudo systemctl enable mongod
May be you are using a wrong version of mongodb list file. I faced this problem but it's my mistake when not selecting the right list file for Ubuntu 16.04. The default selected is for Ubuntu 14.04 and it's the reason for my error: "Failed to start mongod.service: Unit mongod.service not found."
The following steps helped me solve the problem of not being able to start mongodb on Ubuntu Server 18.04 LTS
Step 1: First, remove MongoDB from previous if installed:
sudo apt remove --autoremove mongodb-org
Step 2: Remove any mongodb repo list files:
sudo rm /etc/apt/sources.list.d/mongodb*.list
sudo apt update
Step 3: Import the public key used by the package management system:
sudo apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv 9DA31620334BD75D9DCB49F368818C72E52529D4
Step 4: Create a list file for MongoDB:
echo "deb [ arch=amd64 ] https://repo.mongodb.org/apt/ubuntu bionic/mongodb-org/4.0 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-4.0.list
Step 5: Reload local package database:
sudo apt-get update
Step 6: Install the MongoDB packages:
sudo apt-get install -y mongodb-org
Step 7: Start MongoDB:
sudo service mongod start
Step 8: Begin using MongoDB:
mongo
Hope it helps you.
In some cases for some security reasons the unit would be marked as masked. This state is much stronger than being disabled in which you cannot even start the service manually.
to check this, run the following command:
systemctl list-unit-files | grep mongod
if you find out something like this:
mongod.service masked
then you can unmask the unit by:
sudo systemctl unmask mongod
then you may want to start the service:
sudo systemctl start mongod
and to enable auto-start during system boot:
sudo systemctl enable mongod
However if mongodb did not start again or was not masked at all, you have the option to reinstall it this way:
sudo apt-get --purge mongo*
sudo apt-get install mongodb-org
thanks to #jehanzeb-malik
None of the 30+ existing answers helped me, because my service file was actually missing, not masked or disabled. Re-installing wasn't creating it either for some reason. Solution:
cd /lib/systemd/system
sudo touch mongodb.service
# Copy and paste text to go into service file from snippet below horizontal rule
sudo nano mongodb.service
sudo systemctl daemon-reload
sudo systemctl start mongodb
sudo systemctl status mongodb
[Unit]
Description=An object/document-oriented database
Documentation=man:mongod(1)
After=network.target
[Service]
User=mongodb
Group=mongodb
ExecStart=/usr/bin/mongod --quiet --config /etc/mongod.conf
[Install]
WantedBy=multi-user.target
You should check that /etc/mongod.conf is actually the location of your conf file, it may be /etc/mongodb.conf instead.
As for how your service file may have gone missing in the first place. I managed to break it by uninstalling 3.0.15, trying and failing to install 5.* due to ubuntu 16 not being able to run it since some dependencies clashed, then re-installing 3.0.15 again.
I got the same error for a long while, tried almost all installation isntruciton on google but could not find te answer...
I followed the instructions from https://docs.mongodb.com/manual/tutorial/install-mongodb-on-ubuntu/ but keep getting the error Failed to start mongod.service: Unit mongod.service not found
I firstly installed using: sudo apt-get install -y mongodb-org
Then noticed that wher I run:
$ sudo apt-get install -y mongodb-org
Reading package lists... Done
Building dependency tree
Reading state information... Done
The following additional packages will be installed:
mongodb-org-mongos mongodb-org-shell mongodb-org-tools
The following NEW packages will be installed
mongodb-org mongodb-org-mongos mongodb-org-shell mongodb-org-tools
0 to upgrade, 4 to newly install, 0 to remove and 46 not to upgrade.
Need to get 0 B/40.1 MB of archives.
After this operation, 178 MB of additional disk space will be used.
Selecting previously unselected package mongodb-org-shell.
(Reading database ... 379307 files and directories currently installed.)
Preparing to unpack .../mongodb-org-shell_4.0.3_amd64.deb ...
Unpacking mongodb-org-shell (4.0.3) ...
Selecting previously unselected package mongodb-org-mongos.
Preparing to unpack .../mongodb-org-mongos_4.0.3_amd64.deb ...
Unpacking mongodb-org-mongos (4.0.3) ...
Selecting previously unselected package mongodb-org-tools.
Preparing to unpack .../mongodb-org-tools_4.0.3_amd64.deb ...
Unpacking mongodb-org-tools (4.0.3) ...
Preparing to unpack .../mongodb-org_4.0.3_amd64.deb ...
Unpacking mongodb-org (4.0.3) ...
Setting up mongodb-org-shell (4.0.3) ...
Setting up mongodb-org-mongos (4.0.3) ...
Processing triggers for man-db (2.8.3-2ubuntu0.1) ...
Setting up mongodb-org-tools (4.0.3) ...
Setting up mongodb-org (4.0.3) ...
$ mongod --version
db version v2.6.12
2018-10-26T12:03:12.362+0200 git version: d73c92b1c85703828b55c2916a5dd4ad46535f6a
The db version was 2.6.12 instead of the latest one 4.0.3.
So make sure you dont have mongodb-org-server already installed, if yes, then make sure you uupdate it to your target version (as indicated #https://docs.mongodb.com/manual/tutorial/install-mongodb-on-ubuntu/ -> Step 4 - Install a specific release of MongoDB.)
target_version = 4.0.3
sudo apt-get install -y mongodb-org=target_version mongodb-org-server=target_version mongodb-org-shell=target_version mongodb-org-mongos=target_version mongodb-org-tools=target_version
$ sudo apt-get install -y mongodb-org=4.0.3 mongodb-org-server=4.0.3 mongodb-org-shell=4.0.3 mongodb-org-mongos=4.0.3 mongodb-org-tools=4.0.3
Reading package lists... Done
Building dependency tree
Reading state information... Done
The following NEW packages will be installed
mongodb-org mongodb-org-mongos mongodb-org-shell mongodb-org-tools
The following packages will be upgraded:
mongodb-org-server
1 to upgrade, 4 to newly install, 0 to remove and 45 not to upgrade.
Need to get 15.7 MB/55.8 MB of archives.
After this operation, 217 MB of additional disk space will be used.
Get:1 https://repo.mongodb.org/apt/ubuntu bionic/mongodb-org/4.0/multiverse amd64 mongodb-org-server amd64 4.0.3 [15.7 MB]
Fetched 15.7 MB in 2s (6,289 kB/s)
Selecting previously unselected package mongodb-org-shell.
(Reading database ... 379306 files and directories currently installed.)
Preparing to unpack .../mongodb-org-shell_4.0.3_amd64.deb ...
Unpacking mongodb-org-shell (4.0.3) ...
**Preparing to unpack .../mongodb-org-server_4.0.3_amd64.deb ...
Failed to stop mongod.service: Unit mongod.service not loaded.
invoke-rc.d: initscript mongod, action "stop" failed.
dpkg: warning: old mongodb-org-server package pre-removal script subprocess returned error exit status 5
dpkg: trying script from the new package instead ...
dpkg: ... it looks like that went OK
Unpacking mongodb-org-server (4.0.3) over (2.6.12) ...**
Selecting previously unselected package mongodb-org-mongos.
Preparing to unpack .../mongodb-org-mongos_4.0.3_amd64.deb ...
Unpacking mongodb-org-mongos (4.0.3) ...
Selecting previously unselected package mongodb-org-tools.
Preparing to unpack .../mongodb-org-tools_4.0.3_amd64.deb ...
Unpacking mongodb-org-tools (4.0.3) ...
Preparing to unpack .../mongodb-org_4.0.3_amd64.deb ...
Unpacking mongodb-org (4.0.3) ...
Processing triggers for ureadahead (0.100.0-20) ...
Setting up mongodb-org-shell (4.0.3) ...
Setting up mongodb-org-mongos (4.0.3) ...
Processing triggers for systemd (237-3ubuntu10.3) ...
Processing triggers for man-db (2.8.3-2ubuntu0.1) ...
Setting up mongodb-org-tools (4.0.3) ...
Setting up mongodb-org-server (4.0.3) ...
Installing new version of config file /etc/mongod.conf ...
Setting up mongodb-org (4.0.3) ...
and after doing this its all working for me :D
Well.... My answer may be considered naive but in fact it has been the only way MongoDB has work in my case, Ubuntu 19.10. I tried to run the commands from the most voted comments and none worked, when running:
mongod --repair
I got this alert:
With some research I found out that running the DB in another port could be a solution, then:
mongod --port 27018
And it works great for me every time. Long answer but wanted to give context before giving such a simple solution.
(If I'm doing it wrong or doesn't seem logical, plz tell me! Relevant for me)
Do not remove your db if you already have some data you found useful. Just run the command below and you're good.
sudo systemctl restart mongodb.service
To solve the problem of not being able to start mongodb on ubuntu 16.04
1) look at mongodb log file
2) we find that the error is due to "Failed to unlink socket file /tmp/mongodb-27017"
3) Look at the permission of file /tmp/mongdb-27017.lock
and find that the owner is root instead of mongodb
4) Delete the /tmp/mongodb-27017.sock file manually and use the command
"sudo chown mongodb:mongodb /tmp/mongodb*"
5) Start the service with systemcl and use netstat to check whther mongdob has been started on port 27017
Credit: https://www.mkyong.com/mongodb/mongodb-failed-to-unlink-socket-file-tmpmongodb-27017/
https://hevodata.com/blog/install-mongodb-on-ubuntu/
I just encountered this on my parrot os and this is how I solved it.
sudo service mongodb start
Just follow the below commands. This has worked for me.
Uninstall your mongo completely from your system:
sudo service mongod stop
sudo apt-get purge mongodb-org
sudo rm -r /var/log/mongodb
sudo rm -r /var/lib/mongodb
Now reinstall mongodb using following commands:
sudo apt update
sudo apt install -y mongodb**
Note: The database server is automatically started after installation.
Next, let's verify that the server is running and works correctly.
sudo systemctl status mongodb
You'll see this output:
mongodb.service - An object/document-oriented database
Loaded: loaded (/lib/systemd/system/mongodb.service; enabled; vendor preset: enabled)
Active: active (running) since Sat 2018-05-26 07:48:04 UTC; 2min 17s ago
Docs: man:mongod(1)
Main PID: 2312 (mongod)
Tasks: 23 (limit: 1153)
CGroup: /system.slice/mongodb.service
└─2312 /usr/bin/mongod --unixSocketPrefix=/run/mongodb --config /etc/mongodb.conf**
For those that run into this and end up on this answer, as I did, where they got this error during uninstall orupgrade and Ubuntu keeps failing to uninstall the previous because the service doesn't exist this one line will get you past that and allow the uninstall or upgrade to continue.
sudo touch /lib/systemd/system/mongod.service
It worked for me on ubuntu 20.04: In my case, mongod.service file was locked so it was giving me the same error.
To resolve the issue:-
Step 1: Use following command to check if the mongod.service is present there
cd /usr/lib/systemd/system
ls
Step 2: If the file is present there then Run the following command to unlock the file mongod.service
sudo chmod 777 /usr/lib/systemd/system/mongod.service -R
Step 3: Now run the following commands:
sudo systemctl daemon-reload
sudo systemctl start mongod
sudo systemctl enable mongod
For Ubuntu, if you have old mongodb installed (e.g 3.x).
Then you might need to:
Remove old mongodb first.
Remove old data, WARNING: this may cause data lose.
e.g sudo rm -r /var/lib/mongodb
Remove init script.
e.g sudo rm /etc/init.d/mongodb
Then re-install new mongodb, (e.g 4.x)
Refer: https://docs.mongodb.com/manual/tutorial/install-mongodb-on-ubuntu/
Then start with sudo service mongod start.
Then check status with sudo service mongod status.
Tips:
Don't use sudo service mongodb start, there is an extra b, that's for old mongodb, that's why we do sudo rm /etc/init.d/mongodb.
Backup your old data before remove it.
This is because Ubuntu or any other OS, won't work proparly for the system files if you have closed rootlogin. That is right. Even when you enter the root on filesystem. I know it seems not logical but it is all about 1 and 0's here.
Even I have su -
or
ssh root#934.2349.234243 (random IP, won't expose myself here:))
Still it didn't start!
"Failed to restart mongodb.service: Unit mongodb.service not found."
Then...
You need to activate root login. You can download cyberduck or any file explorer to quickly find the file .
go to:
/etc/ssh/
if you couldn't go with a software tool, you can use for the step above:
vi /etc/ssh/sshd_config
then press ctrl-c
write to console for saving it
:wq
Change this file called sshd_config:
/etc/ssh/sshd_config
Edit to line to yes:
PermitRootLogin yes
REBOOT VPS
then write
sudo systemctl restart mongodb
Voila!
if that didn't help either because your VPS initiates services just before on the root, you should reinstall the VPS maybe
Note that if using the Windows Subsystem for Linux, systemd isn't supported and therefore commands like systemctl won't work:
Failed to connect to bus: No such file or directory
See Blockers for systemd? #994 on GitHub, Microsoft/WSL.
The mongo server can still be started manual via mondgod for development of course.
The second step of mongo installation is
echo "deb [ arch=amd64,arm64 ] https://repo.mongodb.org/apt/ubuntu bionic/mongodb-org/4.4 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-4.4.list
Instead of this command do it manually
cd /etc/apt/
nano sources.list
Write it deb [ arch=amd64,arm64 ] https://repo.mongodb.org/apt/ubuntu bionic/mongodb-org/4.4 multiverse
And save the file,
Continue all process as in installation docs
It works for me:
Try this: sudo systemctl start mongod and to check whether its running use sudo systemctl status mongod
sudo systemctl unmask mongod
The above command worked for me!
sudo service mongod start
Try starting the service it after unmasking it
sudo systemctl status mongod
Check the status using the above command or simply start the mongodb server using mongo command, I hope it works.
Cheers

Mongodb not create the process [Ubuntu machine]

I created a Ubuntu machine on a koding.com environment (like AWS EC2 Ubuntu).
That environment comes with a Mongodb 2.6 preinstalled but i need Mongo 3.0 at least, I uninstalled the 2.6 and installed the 3.2. This is What happens when i try to stop the service.
stop: Unknown instance:
myUser: ~/Web/project $
Before stop the service i started, this is what i found:
myUser: ~/Web/project $ sudo service mongod start
mongod start/running, process 5538
Also, i searched here Stop: Unknown instance mongodb (Ubuntu) without help.
Solved!
Remove files from:
/etc/apt/sources.list.d
Then:
sudo apt-get purge mongodb-org
sudo apt-get autoremove
sudo apt-get update
echo "deb http://repo.mongodb.org/apt/ubuntu trusty/mongodb-org/3.2 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-3.2.list
sudo apt-get update
sudo apt-get install -y mongodb-org

Uninstall mongodb correctly and install mongodb again

Please teach me how to uninstall my mongodb in my virtualbox(ubuntu).
I tried these command and nothing was removed :
sudo apt-get purge mongodb mongodb-clients mongodb-server mongodb-dev
sudo apt-get purge mongodb-10gen
sudo apt-get autoremove
It still exists.
I type mongod --version it shows db version v2.6.1
Update:
I want to remove it and then install it again:
I tried the following 4 commands -
sudo apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv 7F0CEB10
echo 'deb http://downloads-distro.mongodb.org/repo/ubuntu-upstart dist 10gen' | sudo tee /etc/apt/sources.list.d/mongodb.list
sudo apt-get update
sudo apt-get install mongodb-org
then
sudo service mongod start
and type mongo there is error:
MongoDB shell version: 2.6.4
connecting to: test
2014-09-02T21:27:10.390+0800 warning: Failed to connect to 127.0.0.1:27017, reason: errno:111 Connection refused
2014-09-02T21:27:10.392+0800 Error: couldn't connect to server 127.0.0.1:27017 (127.0.0.1), connection attempt failed at src/mongo/shell/mongo.js:146
exception: connect failed
Did I miss something?
Please help me. Thank you!!
Normally, with apt command, you can launch autoremove with all mongoDB packages :
sudo apt-get autoremove mongodb-*
Ok on the mongodb documentation, the packages are changed. To have an up-to-date package follow instructions :
sudo apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv 7F0CEB10
Create the /etc/apt/sources.list.d/mongodb.list list file using the following command:
echo 'deb http://downloads-distro.mongodb.org/repo/ubuntu-upstart dist 10gen' | sudo tee /etc/apt/sources.list.d/mongodb.list
Issue the following command to reload the local package database:
sudo apt-get update
And install desired packages :
sudo apt-get install mongodb-org
After that you have server, shell, mongos, ... To launch mongodb-server youcan launch the mongodb service like this :
sudo service mongod start
To connect on your local server just enter this command :
mongo
To remove Mongo run these commands below
sudo apt-get purge mongodb
sudo rm -r /var/log/mongodb
sudo rm -r /var/lib/mongodb
If you have different versions of mongo already installed, Run the command below
sudo apt-get autoremove
To install mongo safely in Ubuntu 18.04 first update all packages by running this command
sudo apt update
Now install mongo
sudo apt install -y mongodb
Start mongo by running command below
sudo systemctl start mongodb
Now check mongodb status by running
sudo systemctl status mongodb
It should show a status Active
I hope this works for you thanks