Mongodb installation Ubuntu 12.04 LTS - mongodb

My mongodb installation isn't working very well. I added a place in /etc/apt/sources.list, but I get an error message that there is a duplicate in an entry between the i386 packages and amd64 packages? I tried an update, but it doesn't work.

In this how-to-install-mongodb-on-ubuntu, each step was well explained. I tried and it went well as expected. Good luck! :)

Its easy to install mongodb in ubuntu.
Step 1: Search for the mongodb package avaliable but initially it may not be present
sudo apt-cache search mongodb
Step 2: Add the package to /etc/apt/sources.list.d because its the the latest stable MongoDB version
vim /etc/apt/sources.list.d/mongodb.list
Add the following link into the mongodb.list file
deb http://downloads-distro.mongodb.org/repo/ubuntu-upstart dist 10gen
Step 3: Import the GPG key
apt-key adv --keyserver keyserver.ubuntu.com --recv 7F0CEB10
Step 4 : Update the debian package using the command:
apt-get update
Step 5: Now perform search to view the package
apt-cache search mongodb
Step 6: Now the package will be present so install it using the below command:
apt-get install mongodb-10gen
Step 7: Check mongodb has started using
ps -ef | grep mongo
ls -ls /usr/bin | grep mongo
Step 8: The mongodb control script is generated at /etc/init.d/mongodb,the MongoDB config file is at /etc/mongodb.conf add the below details:
verbose = true
dbpath = /data/db
logpath = /var/log/mongodb.log
logappend = true
port = 27017
Step 9: Start the mongodb as a service
service mongodb start
Step 10 :Check mongodb service has started using
ps -ef | grep mongodb
Step 11 : To stop/restart perform the below commands:
service mongodb stop
service mongodb restart
This is taken from my blog post, which I have explained in further detail.

Download and Unzip the .tar .zip and invoke in terminal $.\mongod (after unzip, it runs server instance on default port), Use another terminal and try $.\mongo (show dbs, use dbname, show collections,...)
Depending on the 32bit/64bit version depending on your system capability use 32/64 bit.

first you have to configure Ubuntu Package Management System (APT)
to be able to get the public GPG key
sudo apt-key adv --keyserver keyserver.ubuntu.com --recv 7F0CEB10
or type
sudo apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv 7F0CEB10
another issue you may face it's when you want to connect
to the internet through proxy, export proxy settings to you bash configuration
- add this line with your proxy address (at the end of file /etc/bash.bashrc):
export http_proxy=http://username:password#proxyserver:port/
now Create a /etc/apt/sources.list.d/10gen.list file and include
the following line in the 10gen.list for the 10gen repository:
deb http://downloads-distro.mongodb.org/repo/ubuntu-upstart dist 10gen
if you get that went right you have to update the packages
sudo apt-get update
now install mongodb package:
sudo apt-get install mongodb-10gen
and you good !!!

Related

Installing MongoDB in WSL

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!

MongoDB launching on restart?

I just installed mongodb on my cloud machine following instructions here
sudo apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv EA312927
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
One of the problems I am facing is that, on restart of machine, mongodb is getting launched. I want to disable that operations, I want to launch mongodb manually.
I read that I have to modify /etc/init/mongod.conf file to achieve that, but I am not sure what I have to change. Could somebody please point out what I have to do?
No, the mongod is being started by the /etc/init.d/mongod script.
This is a standard *nix convention for Debian and Redhat amongst other distros.
There are a number of /etc/ directories named to indicate which run level they correspond to. As *nix boots up it changes the run level and will either start or stop processes using the scripts in the /etc/rc#.d directory.
There are some helper scripts that essentially create or remove symlinks from the /etc/init.d directory script to rc#.d directories.
This is talked about in detail here: https://unix.stackexchange.com/questions/106656/how-do-services-in-debian-work-and-how-can-i-manage-them
Needless to say this is not a programming question, but rather a system administration question better suited for the unix board or serveradmin.

Mongo DB as service on Ubuntu

I installed MongoDB on Ubuntu 14.04 using this tutorial. Everything works fine, but how can I run it as service not only manualy?
To make it more clear: I want that MongoDB keeps running after I close "ssh shell".
The easiest way is to install it via APT
Step 1: Import public key
sudo apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv 7F0CEB10
Step 2: Add the MongoDB repository details so APT
echo "deb http://repo.mongodb.org/apt/ubuntu "$(lsb_release -sc)"/mongodb-org/3.0 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-3.0.list
Then update APT packages:
sudo apt-get update
Step 3: Installing and Verifying MongoDB
sudo apt-get install -y mongodb-org
Verify it status
service mongod status
It should show: mongod start/running, process 1611
You can also stop, start, and restart MongoDB using the service command (e.g. service mongod stop, server mongod start).
References: https://www.digitalocean.com/community/tutorials/how-to-install-mongodb-on-ubuntu-14-04

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

upgrade mongodb

I'm looking for the easiest and the most recommended way to upgrade mongodb version (standalone server and mongodb shell).
Are there any terminal (for mac) commands that i can follow in order to upgrade?
I tried to stop the server by
$ mongo
use admin
db.shutdownServer()
i get :
Sat Mar 17 20:38:15 DBClientCursor::init call() failed
Sat Mar 17 20:38:15 query failed : admin.$cmd { shutdown: 1.0 } to: 127.0.0.1:27017
server should be down...
Sat Mar 17 20:38:15 trying reconnect to 127.0.0.1:27017
Sat Mar 17 20:38:15 reconnect 127.0.0.1:27017 failed couldn't connect to server 127.0.0.1:27017
I understand that after stopping the server i need to change the bin folder of mongodb. (which i'm failing to do because mongo is in use)
If someone knows an easier way to only upgrade the mongodb version i would really thank him/her!
thank you.
To Upgrade MongoDB on Ubuntu
First, Backup your data using Mongoexport
e.g:
mongoexport --db sales --collection contacts --out contacts.json --journal
Then check which version of MongoDB you are running by issuing the command:
$ mongod --version
db version v1.8.2, pdfile version 4.5 -
Wed Oct 24 15:43:13 git version: nogitversion
Confirm if mongo is currently running:
$ ps -deaf | grep mongod
mongodb 15408 1 0 Jun06 ? 13:50:00 /usr/bin/mongod --config /etc/mongodb.conf
To Shutdown MongoDB
$ ./mongo
> use admin
> db.shutdownServer()
server should be down...
Then upgraded following these instructions: How to Install MongoDB on Ubuntu
Add the 10 Gen (creators of MongoDB) public key to apt-get so you trust their package:
$ sudo apt-key adv --keyserver keyserver.ubuntu.com --recv 7F0CEB10
$ echo "deb http://downloads-distro.mongodb.org/repo/ubuntu-upstart dist 10gen" >> /etc/apt/sources.list.d/10gen.list
Update your packages:
$ sudo apt-get update
install 10gen's MongoDB Debian/Ubuntu package:
$ sudo apt-get install mongodb-10gen
The following packages will be REMOVED:
mongodb
The following NEW packages will be installed:
mongodb-10gen
If you get an error, try:
$ apt-get autoremove
Confirm that the mongodb user/group has permission to write to the data directory:
$ sudo chown -R mongodb:mongodb /var/lib/mongodb/.
Start up MongoDB as a Daemon (background process) using the following command:
$ mongod --fork --dbpath /var/lib/mongodb/ --smallfiles --logpath
/var/log/mongodb.log --logappend
To Shut Down MongoDB enter the Mongo CLI, access the admin and issue the shutdown command:
$ ./mongo
> use admin
> db.shutdownServer()
shutdown mongodb with:
/usr/bin/mongodb --config $PATH-TO-CONFIG --shutdown
(replace /usr/bin with the right directory)
copy new mongodb and restart mongodb afterwards.
Depends on the version upgrade, but usually if it's a major upgrade (example from 1.8.xx to 2.0.XX) you should do at least the following steps:
backup your data (mongoexport or check this MongoDB backup documentation)
stop the server (use admin db.shutdownServer() )
copy or symbolic link the new mongodb binaries
run mongod with the --upgrade flag (mongod - f --upgrade)
when upgrade done, restart the mongod server
After that make sure that everything is working properly. If not switch back to the old version, import the data from the backup and check mongoDB jira :)
1.Import the public key used by the package management system
sudo apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv EA312927
2. Create a list file for MongoDB.
echo 'deb http://downloads-distro.mongodb.org/repo/ubuntu-upstart dist 10gen' | sudo tee /etc/apt/sources.list.d/mongodb.list
3. Reload local package database.
sudo apt-get update
4. stop the existing service
sudo service mongodb stop
5. first migrate to 2.4 to 2.6
Install a specific release of MongoDB
sudo apt-get install -y mongodb-org=2.6.12 mongodb-org-server=2.6.12 mongodb-org-shell=2.6.12 mongodb-org-mongos=2.6.12 mongodb-org-tools=2.6.12
6.unlink the temp links
remove the lock file.
rm /tmp/mongodb-27017.sock
7.restart mongo service
sudo service mongodb restart
8. Now finally migrate from 2.6 to 3.0
sudo apt-get install -y mongodb-org=3.0.12 mongodb-org-server=3.0.12 mongodb-org-shell=3.0.12 mongodb-org-mongos=3.0.12 mongodb-org-tools=3.0.12
restart again and voila !!!!
For more details follow the official documentation:
mongo doc
To upgrade from version 2 to version 3
I was unsuccessful using the methods in the other answers here, and was finally successful when following the offical document's "Upgrade Binaries" approach, like so:
Stop mongo from running :
$ sudo service mongod stop
Download the correct binaries
(from here, and for help getting the correct version see this
answer)
Copy the downloaded binaries over :
$ sudo cp downloaded_binaries/bin/* /usr/bin
Restart mongo :
$ sudo service mongod start