{"id":3416,"date":"2018-04-27T18:17:38","date_gmt":"2018-04-27T14:17:38","guid":{"rendered":"https:\/\/www.howtoforge.com\/tutorial\/install-mongodb-on-ubuntu\/"},"modified":"2018-04-27T18:17:38","modified_gmt":"2018-04-27T14:17:38","slug":"how-to-install-and-configure-mongodb-on-ubuntu-18-04-lts","status":"publish","type":"post","link":"https:\/\/afaghhosting.net\/blog\/how-to-install-and-configure-mongodb-on-ubuntu-18-04-lts\/","title":{"rendered":"How to Install and Configure MongoDB on Ubuntu 18.04 LTS"},"content":{"rendered":"<p>MongoDB is a NoSQL database that offers a high performance, high availability, and automatic scaling enterprise database. MongoDB is a NoSQL database, so you can&#8217;t use SQL (Structured Query Language) to insert and retrieve data, and it does not store data in tables like MySQL or Postgres. Data is stored in a &#8220;document&#8221; structure in JSON format (in MongoDB called BSON). MongoDB was first introduced in 2009 and is currently developed by the company MongoDB Inc.<\/p>\n<p>MongoDB only provides packages for 64-bit LTS (long-term support) Ubuntu releases. For example, 14.04 LTS (trusty), 16.04 LTS (xenial), 18.04 LTS (bionic) and so on.<\/p>\n<p><span>In this tutorial, I will install MongoDB 3.6 on Ubuntu 18.04 LTS.<\/span><\/p>\n<h2 id=\"prerequisites\">Prerequisites<\/h2>\n<ul>\n<li>Ubuntu Server 18.04 &#8211; 64 bit<\/li>\n<li>Root privileges<\/li>\n<\/ul>\n<p><strong>What we will do in this tutorial:<\/strong><\/p>\n<ol>\n<li>Install MongoDB<\/li>\n<li>Configure MongoDB<\/li>\n<li>Conclusion<\/li>\n<\/ol>\n<h2 id=\"install-mongodb-on-ubuntu-\">Install MongoDB on Ubuntu 18.04<\/h2>\n<h3 id=\"step-importing-the-public-key\">Step 1 &#8211; Importing the Public Key<\/h3>\n<p>GPG keys of the software distributor are required by the Ubuntu package manager apt (Advanced Package Tool) to ensure package consistency and authenticity. Run this command to import MongoDB keys to your server.<\/p>\n<p class=\"command\"><span>sudo apt-key adv &#8211;keyserver hkp:\/\/keyserver.ubuntu.com:80 &#8211;recv 2930ADAE8CAF5059EE73BB4B58712A2291FA4AD5<\/span><\/p>\n<h3 id=\"step-create-source-list-file-mongodb\">Step 2 &#8211; Create source list file MongoDB<\/h3>\n<p>Create a MongoDB list file in <span class=\"system\">\/etc\/apt\/sources.list.d\/<\/span>\u00a0with this command:<\/p>\n<p class=\"command\"><span>echo &#8220;deb http:\/\/repo.mongodb.org\/apt\/ubuntu xenial\/mongodb-org\/3.6 multiverse&#8221; | sudo tee \/etc\/apt\/sources.list.d\/mongodb-org-3.6.list<\/span><\/p>\n<p>MongoDB has not released packages for Bionic Beaver yet, but the Xenial packages are working fine on Ubuntu 18.04 LTS. You might check out the URL\u00a0<a href=\"http:\/\/repo.mongodb.org\/apt\/ubuntu\/dists\/\" target=\"_blank\" rel=\"noopener\">http:\/\/repo.mongodb.org\/apt\/ubuntu\/dists\/<\/a>\u00a0though before you execute the above command. If you see a directory &#8216;bionic&#8217; on that web page, then replace the word &#8216;xenial&#8217; in the above command with the word &#8216;bionic&#8217;.<\/p>\n<h3 id=\"step-update-the-repository\">Step 3 &#8211; Update the repository<\/h3>\n<p>update the repository with the apt command:<\/p>\n<p class=\"command\">sudo apt-get update<\/p>\n<h3 id=\"step-install-mongodb\">Step 4 &#8211; Install MongoDB<\/h3>\n<p>Now you can install MongoDB by typing this command:<\/p>\n<p class=\"command\">sudo apt-get install -y mongodb-org<\/p>\n<p>The MongoDB apt installer created a mongod.service file for Systemd automatically, so there is no need to create it manually anymore.<\/p>\n<p>Start MongoDB and add it as\u00a0a service to be started at boot time:<\/p>\n<p class=\"system command\">systemctl start mongod<br \/>systemctl enable mongod<\/p>\n<p>Now check that\u00a0MongoDB\u00a0has been started on port 27017 with the\u00a0netstat command.<\/p>\n<p class=\"command\">netstat -plntu<\/p>\n<p><a class=\"fancybox\" id=\"img-mongodb-service-started\" href=\"https:\/\/www.howtoforge.com\/images\/install-mongodb-on-ubuntu-16.04\/big\/mongodb-service-started.png\" target=\"_blank\" rel=\"noopener\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/afaghhosting.net\/blog\/wp-content\/uploads\/2018\/04\/how-to-install-and-configure-mongodb-on-ubuntu-18-04-lts.png\" alt=\"Check if MongoDB is running.\" width=\"550\" height=\"145\" title=\"\"><\/a><\/p>\n<h2 id=\"configure-mongodb-username-and-password\">Configure MongoDB username and password<\/h2>\n<p>When the MongoDB packages are installed you can configure username and password for the database server:<\/p>\n<h3 id=\"step-open-mongo-shell\">Step 1 &#8211; Open mongo shell<\/h3>\n<p>Before you set up a username and password for MongoDB, you need to open the\u00a0MongoDB shell on your server. You can login by typing:<\/p>\n<p class=\"command\">mongo<\/p>\n<p>If you get error <span class=\"system\">Failed global initialization: BadValue Invalid or no user locale set. Please ensure LANG and\/or LC_* environment variables are set correctly<\/span>, try the command:<\/p>\n<p class=\"command\">export LC_ALL=C<br \/>mongo<\/p>\n<h3 id=\"step-switch-to-the-database-admin\">Step 2 &#8211; Switch to the database admin<\/h3>\n<p>Once you`re in the MongoDB shell, switch to the database named <em>admin<\/em>:<\/p>\n<p class=\"command\">use admin<\/p>\n<h3 id=\"step-create-the-root-user\">Step 3 &#8211; Create the root user<\/h3>\n<p>Create the root user with this command :<\/p>\n<p class=\"command\">db.createUser({user:&#8221;admin&#8221;, pwd:&#8221;admin123&#8243;, roles:[{role:&#8221;root&#8221;, db:&#8221;admin&#8221;}]})<\/p>\n<p><strong>Desc:<\/strong> Create user <span class=\"system\">admin<\/span> with password <span class=\"system\">admin123<\/span> and have the permission\/role as root and the database is <em>admin<\/em>.<\/p>\n<p><a class=\"fancybox\" id=\"img-create-mongodb-admin-user\" href=\"https:\/\/www.howtoforge.com\/images\/install-mongodb-on-ubuntu-16.04\/big\/create-mongodb-admin-user.png\" target=\"_blank\" rel=\"noopener\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/afaghhosting.net\/blog\/wp-content\/uploads\/2018\/04\/how-to-install-and-configure-mongodb-on-ubuntu-18-04-lts-1.png\" alt=\"Create a admin user in MongoDB\" width=\"550\" height=\"175\" title=\"\"><\/a><\/p>\n<p>Now type <span class=\"system\">exit<\/span> to exit from MongoDB shell.<\/p>\n<h3 id=\"step-enable-mongodb-authentication\">Step 4 &#8211; Enable mongodb authentication<\/h3>\n<p>Edit the mongodb service file <span class=\"system\">&#8216;\/lib\/systemd\/system\/mongod.service&#8217;<\/span>\u00a0with your editor.<\/p>\n<p class=\"command\">nano \/lib\/systemd\/system\/mongod.service<\/p>\n<p>On the &#8216;<em>ExecStart<\/em>&#8216; line 9, add the new option &#8216;<em>&#8211;auth<\/em>&#8216;.<\/p>\n<p class=\"system\">ExecStart=\/usr\/bin\/mongod &#8211;auth &#8211;config \/etc\/mongod.conf<\/p>\n<p>Save the service file and exit nano.<\/p>\n<p>Reload the systemd service:<\/p>\n<p class=\"command\">systemctl daemon-reload<\/p>\n<h3 id=\"step-restart-mongodb-and-try-to-connect\">Step 5 &#8211; Restart MongoDB and try to connect<\/h3>\n<p>Now restart MongoDB and connect with the user created.<\/p>\n<p class=\"command\">sudo service mongod restart<\/p>\n<p>and connect to the MongoDB shell with this command:<\/p>\n<p class=\"command\">mongo -u admin -p admin123 &#8211;authenticationDatabase admin<\/p>\n<p>and you will see the output like this:<\/p>\n<p><a class=\"fancybox\" id=\"img-mongodb-with-authentication-on-ubuntu\" href=\"https:\/\/www.howtoforge.com\/images\/install-mongodb-on-ubuntu-16.04\/big\/mongodb-with-authentication-on-ubuntu.png\" target=\"_blank\" rel=\"noopener\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/afaghhosting.net\/blog\/wp-content\/uploads\/2018\/04\/how-to-install-and-configure-mongodb-on-ubuntu-18-04-lts-2.png\" alt=\"Test MongoDB Authentication.\" width=\"550\" height=\"131\" title=\"\"><\/a><\/p>\n<h2 id=\"nbspvirtual-machine-image\">Virtual Machine image<\/h2>\n<p>This tutorial is available as ready to use virtual machine in OVA \/ OVF format for Howtoforge subscribers. The VM format is compatible with VMWare and Virtualbox and other tools that can import the ova or\u00a0ovf format. You can find the download link in the right menu on the top. Click on the filename to start the download.<\/p>\n<p>The login details of the VM are:<\/p>\n<h3 id=\"ssh-login\">SSH Login<\/h3>\n<p>Username:\u00a0administrator<br \/>Password: howtoforge<\/p>\n<p>The administrator user has sudo permissions.<\/p>\n<p><span>Please change the passwords after the first boot.<\/span><\/p>\n<p><span>The VM is configured for the static IP 192.168.1.100. Instructions on how to change the static IP can be found <a href=\"https:\/\/www.howtoforge.com\/linux-basics-set-a-static-ip-on-ubuntu\" target=\"_blank\" rel=\"noopener\">here<\/a>.<\/span><\/p>\n<h2 id=\"conclusion\">Conclusion<\/h2>\n<p>A well-known NoSQL database that offers high performance, high availability, and automatic scaling is <strong>MongoDB<\/strong>. It is different from the RDBMS such as MySQL, PostgreSQL, and SQLite as it does not use SQL to set and retrieve data. MongoDB stores data in <strong>`documents`<\/strong> called <strong>BSON<\/strong> (binary representation of JSON with additional types of information). MongoDB\u00a0is only available for <strong>64-bit<\/strong> Long Term Support Ubuntu Release.<\/p>\n<div>\n<p><b>Share this page:<\/b><\/p>\n<p>\n<a href=\"https:\/\/www.facebook.com\/sharer.php?u=https%3A%2F%2Fwww.howtoforge.com%2Ftutorial%2Finstall-mongodb-on-ubuntu%2F\" target=\"_blank\" rel=\"nofollow noopener noreferrer\"><img decoding=\"async\" src=\"https:\/\/afaghhosting.net\/blog\/wp-content\/uploads\/2018\/04\/how-to-install-and-configure-mongodb-on-ubuntu-18-04-lts-3.png\" height=\"20\" alt=\"\" title=\"\"><\/a><br \/>\n<a href=\"https:\/\/twitter.com\/intent\/tweet?url=https%3A%2F%2Fwww.howtoforge.com%2Ftutorial%2Finstall-mongodb-on-ubuntu%2F&amp;text=How+to+Install+and+Configure+MongoDB+on+Ubuntu+18.04+LTS&amp;via=howtoforgecom&amp;related=howtoforgecom\" target=\"_blank\" rel=\"nofollow noopener noreferrer\"><img decoding=\"async\" src=\"https:\/\/afaghhosting.net\/blog\/wp-content\/uploads\/2018\/04\/how-to-install-and-configure-mongodb-on-ubuntu-18-04-lts-4.png\" height=\"20\" alt=\"\" title=\"\"><\/a><br \/>\n<a href=\"https:\/\/twitter.com\/howtoforgecom\/\" target=\"_blank\" rel=\"nofollow noopener noreferrer\"><img decoding=\"async\" src=\"https:\/\/afaghhosting.net\/blog\/wp-content\/uploads\/2018\/04\/how-to-install-and-configure-mongodb-on-ubuntu-18-04-lts-5.png\" height=\"20\" alt=\"\" title=\"\"><\/a><br \/>\n<a href=\"https:\/\/plus.google.com\/share?url=https%3A%2F%2Fwww.howtoforge.com%2Ftutorial%2Finstall-mongodb-on-ubuntu%2F\" target=\"_blank\" rel=\"nofollow noopener noreferrer\"><img decoding=\"async\" src=\"https:\/\/afaghhosting.net\/blog\/wp-content\/uploads\/2018\/04\/how-to-install-and-configure-mongodb-on-ubuntu-18-04-lts-6.png\" height=\"20\" alt=\"\" title=\"\"><\/a>\n<\/p>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>MongoDB is a NoSQL database that offers a high performance, high availability, and automatic scaling enterprise database. MongoDB is a NoSQL database, so you can&#8217;t use SQL (Structured Query Language) to insert and retrieve data, and it does not store data in tables like MySQL or Postgres. Data is stored in a &#8220;document&#8221; structure in &hellip;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[36],"tags":[],"class_list":["post-3416","post","type-post","status-publish","format-standard","hentry","category-36"],"_links":{"self":[{"href":"https:\/\/afaghhosting.net\/blog\/wp-json\/wp\/v2\/posts\/3416","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/afaghhosting.net\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/afaghhosting.net\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/afaghhosting.net\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/afaghhosting.net\/blog\/wp-json\/wp\/v2\/comments?post=3416"}],"version-history":[{"count":0,"href":"https:\/\/afaghhosting.net\/blog\/wp-json\/wp\/v2\/posts\/3416\/revisions"}],"wp:attachment":[{"href":"https:\/\/afaghhosting.net\/blog\/wp-json\/wp\/v2\/media?parent=3416"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/afaghhosting.net\/blog\/wp-json\/wp\/v2\/categories?post=3416"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/afaghhosting.net\/blog\/wp-json\/wp\/v2\/tags?post=3416"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}