Today I am going to try the MongoDB installation on my Linux machine which I installed in my VMware. I am not going to do the same installation on my Windows’ laptop. I am expected to learn and workaround with the MongoDB in my virtual machine only.
Let us get started, it is simple installation and just a small confusion on one of the configuration parts, not to worry about it too much at this point and the clear guide on how to install MongoDB can be found in this two links:
1. From the MongoDB website: https://docs.mongodb.com/manual/tutorial/
2. From a tutorial website: https://www.howtoforge.com/tutorial/install-mongodb-on-ubuntu-16.04/
Both are guiding us on how to install on the Ubuntu 16.04. Just follow the commands given in the website and I will just briefly mention what I do on each step.
1. Import the public key.
sudo apt-key adv –keyserver hkp://keyserver.ubuntu.com:80 –recv 9DA31620334BD75D9DCB49F368818C72E52529D4
2. Create source list file MongoDB. I am using below command because my Ubuntu version, Xenial,
echo "deb http://repo.mongodb.org/apt/ubuntu xenial/mongodb-org/3.2 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-3.2.list
Else, the default command is,
echo "deb http://repo.mongodb.org/apt/ubuntu "$(lsb_release -sc)"/mongodb-org/3.2 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-3.2.list
3. Reload local package database. Update the repository by running the command below,
sudo apt-get update
4. Install MongoDB.
Edited on 2019/01/31.
It is much cleaner and straight forward installation right now using command,
sudo apt-get install -y mongodb-org
At this step, it asks us to create a new mongodb systemd service file in the ‘/lib/systemd/system’ directory. Below is the instruction:
Go to that directory and create the new mongodb service file ‘mongod.service’ with vim.
cd /lib/systemd/system/ vim mongod.service
In the edit mode (command prompt), I am unable to edit anything in which I should paste the setting on it and save. I have no idea how the vim works. Maybe you can enlighten me on this part.
I tried to use the text editor and I am unable to save the file because of permission issue. I did not change the file, anyway, and proceed to update the systemd service using the given command line.
5. Start Mongodb and add it as service to be started at boot time using command,
sudo service mongod start
sudo systemctl enable mongod
It could be important to run the service at boot time especially when install on a server, while some of us would prefer to on the service when we run our database on local machine.
Upon run the second command I see,
Created symlink from /etc/systemd/system/multi-user.target.wants/mongod.service to /lib/systemd/system/mongod.service.
If you are into system files details, you can navigate to the path above to see more information.
6. Now check that Mongodb has been started on port 27017 with the netstat command.
You can check the MongoDB’s log at /var/log/mongodb/mongod.log and the port configured in /etc/mongod.conf, 27017 by default.
mongod.conf is a great file to check mongo database’s configuration.
The installation is completed. Few commands to be remember:
1. Start service – sudo service mongod start
2. Stop service – sudo service mongod stop (or control-C)
3. Restart service – sudo service mongod restart
4. Go to Mongo shell – mongo