Installation of MongoDb  on Ubuntu 18.04
Published: 2020-08-22 19:01:42

<!-- wp:image {"id":237,"sizeSlug":"large"} -->
<figure class="wp-block-image size-large"><img src="https://iffishells.files.wordpress.com/2020/08/mogno-cover.png?w=770" alt="" class="wp-image-237"/></figure>
<!-- /wp:image -->

<!-- wp:paragraph -->
<p></p>
<!-- /wp:paragraph -->

<!-- wp:heading -->
<h2>Introduction</h2>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>ManngoDb is a free and open-source NOsql Document database used commonly in the modern web application on the internet.Classified as a NoSQL database program,&nbsp;<strong>MongoDB</strong>&nbsp;uses JSON-like documents with optional schemas.<strong>MongoDB</strong>&nbsp;<strong>is</strong>&nbsp;developed by&nbsp;<strong>MongoDB</strong>&nbsp;.</p>
<!-- /wp:paragraph -->

<!-- wp:heading -->
<h2>Pre-requisites</h2>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>To follow this tutorial , you will need : </p>
<!-- /wp:paragraph -->

<!-- wp:list -->
<ul><li>One Ubuntu 18.04 </li><li>sudo non-root and firewall  </li></ul>
<!-- /wp:list -->

<!-- wp:heading -->
<h2>step 1 :</h2>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>Ubuntu official package repositories include an up to  date version of mangodb ,which mean can install the necessary packages using </p>
<!-- /wp:paragraph -->

<!-- wp:code -->
<pre class="wp-block-code"><code>apt
</code></pre>
<!-- /wp:code -->

<!-- wp:paragraph -->
<p>first , update the packages list to have the most latest version of the repository listinggs : </p>
<!-- /wp:paragraph -->

<!-- wp:code -->
<pre class="wp-block-code"><code>sudo apt update -y</code></pre>
<!-- /wp:code -->

<!-- wp:paragraph -->
<p>Now can install the Mangodb packages itself </p>
<!-- /wp:paragraph -->

<!-- wp:code -->
<pre class="wp-block-code"><code>sudo apt install -y mangodb</code></pre>
<!-- /wp:code -->

<!-- wp:image {"id":224,"sizeSlug":"large"} -->
<figure class="wp-block-image size-large"><img src="https://iffishells.files.wordpress.com/2020/08/install-mangodb-.png?w=846" alt="" class="wp-image-224"/></figure>
<!-- /wp:image -->

<!-- wp:paragraph -->
<p>this command install several packages containing the latest stable version of MongoDb ,along with the helpfull tools.The Databse server is automatically started after installation</p>
<!-- /wp:paragraph -->

<!-- wp:heading -->
<h2>step 2 : checking service and DB</h2>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>The installation process started MongoDb automatically,but lets verify the service is started and database is working or not. </p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>first check the service status : </p>
<!-- /wp:paragraph -->

<!-- wp:code -->
<pre class="wp-block-code"><code>sudo systemctl status mongodb </code></pre>
<!-- /wp:code -->

<!-- wp:paragraph -->
<p>you will see th output like this : </p>
<!-- /wp:paragraph -->

<!-- wp:image {"id":225,"sizeSlug":"large"} -->
<figure class="wp-block-image size-large"><img src="https://iffishells.files.wordpress.com/2020/08/systemctl.png?w=871" alt="" class="wp-image-225"/></figure>
<!-- /wp:image -->

<!-- wp:paragraph -->
<p>According to systemd , the Mongodb server is up and running .</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>we can verify this further by actually connecting to the dataabase server and executing diagnostic command . </p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>Execute this command </p>
<!-- /wp:paragraph -->

<!-- wp:code -->
<pre class="wp-block-code"><code>mongo --eval 'db.runCommand(&#91;connnectionStatus:1})' </code></pre>
<!-- /wp:code -->

<!-- wp:paragraph -->
<p>This will output the current database version , the server address and port , and the output of the status command : </p>
<!-- /wp:paragraph -->

<!-- wp:image {"id":228,"sizeSlug":"large"} -->
<figure class="wp-block-image size-large"><img src="https://iffishells.files.wordpress.com/2020/08/mango-eval.png?w=703" alt="" class="wp-image-228"/></figure>
<!-- /wp:image -->

<!-- wp:paragraph -->
<p>A Value of  1 for the ok field in the response indicates that the server is working properly ,Next we 'll look how to manage the server is instance .</p>
<!-- /wp:paragraph -->

<!-- wp:heading -->
<h2>step 3 : Managin the MongoDb Service</h2>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>MongoDb  install as a systemd service , which means you that can manage it using standard systemd commands algonside all other system services in Ubuntu .</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>To verify status of the service type : </p>
<!-- /wp:paragraph -->

<!-- wp:code -->
<pre class="wp-block-code"><code>sudo systemctl status mongodb </code></pre>
<!-- /wp:code -->

<!-- wp:image {"id":230,"sizeSlug":"large"} -->
<figure class="wp-block-image size-large"><img src="https://iffishells.files.wordpress.com/2020/08/systemctl-1.png?w=871" alt="" class="wp-image-230"/></figure>
<!-- /wp:image -->

<!-- wp:paragraph -->
<p>You can stop the server anytime by typing : </p>
<!-- /wp:paragraph -->

<!-- wp:code -->
<pre class="wp-block-code"><code>sudo systemctl stop mongodb </code></pre>
<!-- /wp:code -->

<!-- wp:paragraph -->
<p>To start the server when it is stopped , type : </p>
<!-- /wp:paragraph -->

<!-- wp:code -->
<pre class="wp-block-code"><code>sudo systemctl start mongodb </code></pre>
<!-- /wp:code -->

<!-- wp:paragraph -->
<p>you can also restart the server with single command : </p>
<!-- /wp:paragraph -->

<!-- wp:code -->
<pre class="wp-block-code"><code>sudo systemctl restart mognodb</code></pre>
<!-- /wp:code -->

<!-- wp:paragraph -->
<p>By default Mongodb is configured to start automatically with the server but if you can to disable it . </p>
<!-- /wp:paragraph -->

<!-- wp:code -->
<pre class="wp-block-code"><code>sudo systemctl disable mongodb </code></pre>
<!-- /wp:code -->

<!-- wp:paragraph -->
<p>its easy to enable it again </p>
<!-- /wp:paragraph -->

<!-- wp:code -->
<pre class="wp-block-code"><code>sudo systemctl enable mongodb</code></pre>
<!-- /wp:code -->

<!-- wp:paragraph -->
<p>Next , let's adjust the firewall setting for our Mongodb installation.</p>
<!-- /wp:paragraph -->

<!-- wp:heading -->
<h2>step 4 : adjusting the firewall (choice)</h2>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>Assumming you have follow the above information for the installation for mogodb . if You intend to use the Mongodb server only locally with application running on the same server .this is the recommended and secure setting .However if you would like to be connect to your Mongodb server from the internet , you have to allow the incoming connection in ufw.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>To Allow access to Mongodb on its default port 27017 from the everwhere , you could use . </p>
<!-- /wp:paragraph -->

<!-- wp:code -->
<pre class="wp-block-code"><code>sudo ufw allow 27017</code></pre>
<!-- /wp:code -->

<!-- wp:paragraph -->
<p>However enabling internet access to MongoDb server on a default installation gives anyone unrestricted access to the database server and its data .</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>In Most cases Mongodb should be accessed only from the certain trusted locations , such as another server hosting an application . To achieve this task, you can allow access on MongoDb default port while specifying the IP address of another server that will be explicity allow to connect .</p>
<!-- /wp:paragraph -->

<!-- wp:code -->
<pre class="wp-block-code"><code>sudo ufw allow from other_server_IP/32 to any port 27017</code></pre>
<!-- /wp:code -->

<!-- wp:paragraph -->
<p>you can verify the change in firewall setting with UFW : </p>
<!-- /wp:paragraph -->

<!-- wp:code -->
<pre class="wp-block-code"><code>sudo ufw status</code></pre>
<!-- /wp:code -->

<!-- wp:paragraph -->
<p>if you have decided to allow only a certain IP address to connect to MongoDb server , the address of the allowed location will be instead of anywhere in the output.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>you can find more advance firewall setting for restriction access to services in <a href="https://www.digitalocean.com/community/tutorials/ufw-essentials-common-firewall-rules-and-commands">this community.</a></p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>Even though the port is open MongoDb is currently only listening on the local address 127.0.0.1 . To allow remote connection , add your server publically-routable IP address to mongod.conf file.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>Open the MongoDB Configuration file in your editor . </p>
<!-- /wp:paragraph -->

<!-- wp:code -->
<pre class="wp-block-code"><code>sudo vim /etc/mongodb.conf
</code></pre>
<!-- /wp:code -->

<!-- wp:paragraph -->
<p>Add your server IP address to the <strong>bindip</strong> value : </p>
<!-- /wp:paragraph -->

<!-- wp:code -->
<pre class="wp-block-code"><code>...
logappend=true

bind_ip = 127.0.0.1,your_server_ip
#port = 27017

...</code></pre>
<!-- /wp:code -->

<!-- wp:social-links -->
<ul class="wp-block-social-links"><!-- wp:social-link {"url":"https://wordpress.org","service":"wordpress"} /-->

<!-- wp:social-link {"service":"facebook"} /-->

<!-- wp:social-link {"service":"twitter"} /-->

<!-- wp:social-link {"service":"instagram"} /-->

<!-- wp:social-link {"service":"linkedin"} /-->

<!-- wp:social-link {"service":"youtube"} /--></ul>
<!-- /wp:social-links -->

<!-- wp:paragraph -->
<p>Be sure to place a comma between the existing IP address and one you added  . save te file . if you are using <strong>vim . </strong></p>
<!-- /wp:paragraph -->

<!-- wp:code -->
<pre class="wp-block-code"><code>:wq!
</code></pre>
<!-- /wp:code -->

<!-- wp:heading -->
<h2>Conclusion</h2>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>This is all from my side if you are facing any problem you feel free to contact me.</p>
<!-- /wp:paragraph -->