{"id":16054,"date":"2019-03-18T13:07:27","date_gmt":"2019-03-18T13:07:27","guid":{"rendered":"https:\/\/www.hostinger.com\/tutorials\/?p=16054"},"modified":"2026-01-13T12:10:19","modified_gmt":"2026-01-13T12:10:19","slug":"how-to-install-mongodb-on-ubuntu","status":"publish","type":"post","link":"\/tutorials\/how-to-install-mongodb-on-ubuntu","title":{"rendered":"How to install MongoDB on Ubuntu"},"content":{"rendered":"<p>MongoDB is one of the most popular open-source NoSQL database managers. It stores data in a formatted document instead of a tabular format. Since it doesn&rsquo;t have a rigid schema, MongoDB offers a higher scalability and flexibility than SQL.<\/p><p>This database management system (DBMS) is popular among developers due to its performance and compatibility with various programming languages. However, to install MongoDB, Ubuntu users need various commands, making the process challenging.<\/p><p>With this in mind, we&rsquo;ll explain how to install MongoDB on Ubuntu virtual private server (VPS) hosting. We&rsquo;ll also cover the steps to create a new database, set up a user, and enable remote authentication.<\/p><div class=\"wp-block-image\">\n<figure class=\"aligncenter size-large\"><a href=\"https:\/\/assets.hostinger.com\/content\/tutorials\/pdf\/Linux-Commands-Cheat-Sheet.pdf\" target=\"_blank\" rel=\"noopener\"><img decoding=\"async\" width=\"2048\" height=\"566\" src=\"https:\/\/imagedelivery.net\/LqiWLm-3MGbYHtFuUbcBtA\/wp-content\/uploads\/sites\/2\/2022\/11\/Linux-cheat-sheet.png\/public\" alt=\"\" class=\"wp-image-69262\" srcset=\"https:\/\/imagedelivery.net\/LqiWLm-3MGbYHtFuUbcBtA\/wp-content\/uploads\/sites\/2\/2022\/11\/Linux-cheat-sheet.png\/w=2048,fit=scale-down 2048w, https:\/\/imagedelivery.net\/LqiWLm-3MGbYHtFuUbcBtA\/wp-content\/uploads\/sites\/2\/2022\/11\/Linux-cheat-sheet.png\/w=300,fit=scale-down 300w, https:\/\/imagedelivery.net\/LqiWLm-3MGbYHtFuUbcBtA\/wp-content\/uploads\/sites\/2\/2022\/11\/Linux-cheat-sheet.png\/w=1024,fit=scale-down 1024w, https:\/\/imagedelivery.net\/LqiWLm-3MGbYHtFuUbcBtA\/wp-content\/uploads\/sites\/2\/2022\/11\/Linux-cheat-sheet.png\/w=150,fit=scale-down 150w, https:\/\/imagedelivery.net\/LqiWLm-3MGbYHtFuUbcBtA\/wp-content\/uploads\/sites\/2\/2022\/11\/Linux-cheat-sheet.png\/w=768,fit=scale-down 768w\" sizes=\"(max-width: 2048px) 100vw, 2048px\" \/><\/a><\/figure><\/div><p>\n\n\n\n\n<div class=\"protip\">\n                    <h2 class=\"featured-snippet title\">What Is MongoDB?<\/h2>\n                    <p>MongoDB is one of the most popular open-source NoSQL database management systems. It is widely used for large-scale applications or websites.<br>\nUnlike SQL, MongoDB stores data in a BSON document with a flexible schema, resulting in higher scalability. It also supports various operating systems, programming languages, and frameworks.<\/p>\n                <\/div>\n\n\n\n<\/p><h2 class=\"wp-block-heading\" id=\"h-prerequisites-for-the-mongodb-installation-on-ubuntu\">Prerequisites for the MongoDB Installation on Ubuntu<\/h2><p>Before installing the official MongoDB package, check your hardware and software compatibility. Your VPS hosting plan must support Ubuntu, offer an SSH connection, and provide full root access.<\/p><p>Hostinger&rsquo;s VPS hosting offers various Linux distributions, including <a href=\"\/vps\/ubuntu-hosting\">Ubuntu<\/a>. You can install it easily by going to the <strong>VPS Dashboard <\/strong>&rarr; <strong>OS &amp; Panel <\/strong>&rarr; <strong>Operating System<\/strong>.<\/p><div class=\"wp-block-image\">\n<figure data-wp-context='{\"imageId\":\"69dfa4204e35c\"}' data-wp-interactive=\"core\/image\" class=\"aligncenter size-full wp-lightbox-container\"><img decoding=\"async\" width=\"1460\" height=\"323\" data-wp-class--hide=\"state.isContentHidden\" data-wp-class--show=\"state.isContentVisible\" data-wp-init=\"callbacks.setButtonStyles\" data-wp-on-async--click=\"actions.showLightbox\" data-wp-on-async--load=\"callbacks.setButtonStyles\" data-wp-on-async-window--resize=\"callbacks.setButtonStyles\" src=\"https:\/\/imagedelivery.net\/LqiWLm-3MGbYHtFuUbcBtA\/wp-content\/uploads\/sites\/2\/2019\/03\/ubuntu-in-hpanel-os-configuration-menu.png\/public\" alt=\"Ubuntu on hPanel's VPS operating system configuration page\" class=\"wp-image-125844\" srcset=\"https:\/\/imagedelivery.net\/LqiWLm-3MGbYHtFuUbcBtA\/wp-content\/uploads\/sites\/2\/2019\/03\/ubuntu-in-hpanel-os-configuration-menu.png\/w=1460,fit=scale-down 1460w, https:\/\/imagedelivery.net\/LqiWLm-3MGbYHtFuUbcBtA\/wp-content\/uploads\/sites\/2\/2019\/03\/ubuntu-in-hpanel-os-configuration-menu.png\/w=300,fit=scale-down 300w, https:\/\/imagedelivery.net\/LqiWLm-3MGbYHtFuUbcBtA\/wp-content\/uploads\/sites\/2\/2019\/03\/ubuntu-in-hpanel-os-configuration-menu.png\/w=1024,fit=scale-down 1024w, https:\/\/imagedelivery.net\/LqiWLm-3MGbYHtFuUbcBtA\/wp-content\/uploads\/sites\/2\/2019\/03\/ubuntu-in-hpanel-os-configuration-menu.png\/w=150,fit=scale-down 150w, https:\/\/imagedelivery.net\/LqiWLm-3MGbYHtFuUbcBtA\/wp-content\/uploads\/sites\/2\/2019\/03\/ubuntu-in-hpanel-os-configuration-menu.png\/w=768,fit=scale-down 768w\" sizes=\"(max-width: 1460px) 100vw, 1460px\" \/><button class=\"lightbox-trigger\" type=\"button\" aria-haspopup=\"dialog\" aria-label=\"Enlarge\" data-wp-init=\"callbacks.initTriggerButton\" data-wp-on-async--click=\"actions.showLightbox\" data-wp-style--right=\"state.imageButtonRight\" data-wp-style--top=\"state.imageButtonTop\">\n\t\t\t<svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"12\" height=\"12\" fill=\"none\" viewbox=\"0 0 12 12\">\n\t\t\t\t<path fill=\"#fff\" d=\"M2 0a2 2 0 0 0-2 2v2h1.5V2a.5.5 0 0 1 .5-.5h2V0H2Zm2 10.5H2a.5.5 0 0 1-.5-.5V8H0v2a2 2 0 0 0 2 2h2v-1.5ZM8 12v-1.5h2a.5.5 0 0 0 .5-.5V8H12v2a2 2 0 0 1-2 2H8Zm2-12a2 2 0 0 1 2 2v2h-1.5V2a.5.5 0 0 0-.5-.5H8V0h2Z\"><\/path>\n\t\t\t<\/svg>\n\t\t<\/button><\/figure><\/div><p>We recommend using the newest operating system to ensure compatibility and security. However, the steps to install MongoDB on <strong>Ubuntu 20.04<\/strong> or other versions are similar.<\/p><p>Our VPS hosting plans also enable you to connect remotely via SSH. In addition to using an application like PuTTY and Terminal, you can run <a href=\"\/tutorials\/linux-commands\">Linux commands<\/a> directly from your web browser using our <strong>Browser terminal<\/strong>.<\/p><p>Hostinger&rsquo;s VPS also provides full root access, allowing users to run MongoDB installation commands without permission issues.<\/p><figure class=\"wp-block-image size-large\"><a class=\"hgr-tutorials-cta hgr-tutorials-cta-vps-hosting\" href=\"\/vps-hosting\" target=\"_blank\" rel=\"noreferrer noopener\"><img decoding=\"async\" width=\"1024\" height=\"300\" src=\"https:\/\/imagedelivery.net\/LqiWLm-3MGbYHtFuUbcBtA\/wp-content\/uploads\/sites\/2\/2023\/02\/VPS-hosting-banner.png\/public\" alt=\"\" class=\"wp-image-77934\" srcset=\"https:\/\/imagedelivery.net\/LqiWLm-3MGbYHtFuUbcBtA\/wp-content\/uploads\/sites\/2\/2023\/02\/VPS-hosting-banner.png\/w=1024,fit=scale-down 1024w, https:\/\/imagedelivery.net\/LqiWLm-3MGbYHtFuUbcBtA\/wp-content\/uploads\/sites\/2\/2023\/02\/VPS-hosting-banner.png\/w=300,fit=scale-down 300w, https:\/\/imagedelivery.net\/LqiWLm-3MGbYHtFuUbcBtA\/wp-content\/uploads\/sites\/2\/2023\/02\/VPS-hosting-banner.png\/w=150,fit=scale-down 150w, https:\/\/imagedelivery.net\/LqiWLm-3MGbYHtFuUbcBtA\/wp-content\/uploads\/sites\/2\/2023\/02\/VPS-hosting-banner.png\/w=768,fit=scale-down 768w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/a><\/figure><h2 class=\"wp-block-heading\" id=\"h-how-to-install-mongodb-on-ubuntu\">How to Install MongoDB on Ubuntu<\/h2><p>In this section, we will explain how to install MongoDB on <strong>Ubuntu 20.04<\/strong> or other versions. Before proceeding, ensure you have connected to your VPS via SSH using a root or superuser account<\/p><h3 class=\"wp-block-heading\" id=\"h-1-install-mongodb\">1. Install MongoDB<\/h3><p>Before installing the MongoDB package, download the <strong>GnuPG<\/strong> and <strong>cURL<\/strong> utility by running this command in your command-line interface:<\/p><pre class=\"wp-block-preformatted\">sudo apt-get install -y gnupg curl<\/pre><p>Use cURL and GnuPG to Import the MongoDB public GPG key to retrieve the installation package:<\/p><pre class=\"wp-block-preformatted\">curl -fsSL https:\/\/pgp.mongodb.com\/server-7.0.asc | sudo gpg -o \/usr\/share\/keyrings\/mongodb-server-7.0.gpg --dearmor<\/pre><p>Note that the URL varies depending on the MongoDB packages. In this tutorial, we will install MongoDB Community Edition 7.0, the latest stable version at the time of writing.<\/p><p>After importing the official MongoDB packages, create a list file for installation. The command differs depending on your Ubuntu version. For <strong>Ubuntu 22.04 <\/strong>or later, run:<\/p><pre class=\"wp-block-preformatted\">echo \"deb [ arch=amd64,arm64 signed-by=\/usr\/share\/keyrings\/mongodb-server-7.0.gpg ] https:\/\/repo.mongodb.org\/apt\/ubuntu jammy\/mongodb-org\/7.0 multiverse\" | sudo tee \/etc\/apt\/sources.list.d\/mongodb-org-7.0.list<\/pre><p>Meanwhile, run the following to configure the list file on <strong>Ubuntu 20.04:<\/strong><\/p><pre class=\"wp-block-preformatted\">echo \"deb [ arch=amd64,arm64 signed-by=\/usr\/share\/keyrings\/mongodb-server-7.0.gpg ] https:\/\/repo.mongodb.org\/apt\/ubuntu focal\/mongodb-org\/7.0 multiverse\" | sudo tee \/etc\/apt\/sources.list.d\/mongodb-org-7.0.list<\/pre><p>Refresh the APT repository to synchronize the local package database:<\/p><pre class=\"wp-block-preformatted\">sudo apt-get update<\/pre><p>Run the following command to install the latest stable version of MongoDB using the APT package management system:<\/p><pre class=\"wp-block-preformatted\">sudo apt-get install -y mongodb-org<\/pre><p>Optionally, choose a specific version of the official mongodb-org package. For example, run the following command to install <strong>MongoDB version 7.0.2<\/strong>:<\/p><pre class=\"wp-block-preformatted\">sudo apt-get install -y mongodb-org=7.0.2 mongodb-org-database=7.0.2 mongodb-org-server=7.0.2 mongodb-mongosh=7.0.2 mongodb-org-mongos=7.0.2 mongodb-org-tools=7.0.2<\/pre><p>If you specify only the MongoDB version and not the other components, APT will install the newest package.<\/p><p>Since APT automatically updates the component package, pin the installation to keep the current version. Here are the command examples:<\/p><pre class=\"wp-block-preformatted\">echo \"mongodb-org hold\" | sudo dpkg --set-selections<\/pre><pre class=\"wp-block-preformatted\">echo \"mongodb-org-server hold\" | sudo dpkg --set-selections<\/pre><pre class=\"wp-block-preformatted\">echo \"mongodb-mongosh hold\" | sudo dpkg --set-selections<\/pre><p>\n\n\n<div class=\"protip\">\n                    <h4 class=\"title\">Pro Tip<\/h4>\n                    <p>If you don&rsquo;t import the public GPG key to your local package database, APT will install the unofficial MongoDB package. This might lead to compatibility, security, and legal issues, as well as a lack of support.<\/p>\n                <\/div>\n\n\n\n<\/p><h3 class=\"wp-block-heading\" id=\"h-2-start-your-mongodb-service\">2. Start Your MongoDB Service<\/h3><p>After the installation, run MongoDB Community Edition by entering the following:<\/p><pre class=\"wp-block-preformatted\">sudo systemctl start mongod<\/pre><p>The <strong>systemctl<\/strong> command is essential for MongoDB service management. For example, if you encounter an error, run the following to reboot running services and try starting the DBMS again:<\/p><pre class=\"wp-block-preformatted\">sudo systemctl daemon-reload<\/pre><p>To find out whether MongoDB database server loaded properly, check its status with this command:<\/p><pre class=\"wp-block-preformatted\">sudo systemctl status mongod<\/pre><p>You should see <strong>active (running) <\/strong>if the MongoDB service is working, similar to the following output.<\/p><div class=\"wp-block-image\">\n<figure class=\"aligncenter size-large\"><a href=\"\/tutorials\/wp-content\/uploads\/sites\/2\/2019\/03\/mongod-service-status-in-terminal.png\"><img decoding=\"async\" width=\"1024\" height=\"213\" src=\"https:\/\/imagedelivery.net\/LqiWLm-3MGbYHtFuUbcBtA\/wp-content\/uploads\/sites\/2\/2019\/03\/mongod-service-status-in-terminal.png\/public\" alt=\"The Mongod service status in Terminal\" class=\"wp-image-99394\" srcset=\"https:\/\/imagedelivery.net\/LqiWLm-3MGbYHtFuUbcBtA\/wp-content\/uploads\/sites\/2\/2019\/03\/mongod-service-status-in-terminal.png\/w=1024,fit=scale-down 1024w, https:\/\/imagedelivery.net\/LqiWLm-3MGbYHtFuUbcBtA\/wp-content\/uploads\/sites\/2\/2019\/03\/mongod-service-status-in-terminal.png\/w=300,fit=scale-down 300w, https:\/\/imagedelivery.net\/LqiWLm-3MGbYHtFuUbcBtA\/wp-content\/uploads\/sites\/2\/2019\/03\/mongod-service-status-in-terminal.png\/w=150,fit=scale-down 150w, https:\/\/imagedelivery.net\/LqiWLm-3MGbYHtFuUbcBtA\/wp-content\/uploads\/sites\/2\/2019\/03\/mongod-service-status-in-terminal.png\/w=768,fit=scale-down 768w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/a><\/figure><\/div><p>By default, the service doesn&rsquo;t start at boot. To automatically load it at startup, enable MongoDB using the following command. :<\/p><pre class=\"wp-block-preformatted\">sudo systemctl enable mongod<\/pre><p>Check the service status using <strong>systemctl<\/strong>. If it shows <strong>Enabled <\/strong>instead of <strong>Loaded<\/strong>, MongoDB is ready to use.<\/p><p>\n\n\n<div><p class=\"important\">MongoDB&rsquo;s primary daemon process is called <strong>mongod<\/strong>. It handles data requests, manages access, and runs background management operations.<\/p><\/div>\n\n\n\n<\/p><h3 class=\"wp-block-heading\" id=\"h-3-configure-mongodb\">3. Configure MongoDB<\/h3><p>Change the MongoDB database setting by modifying the main configuration file. It is created automatically during the installation process and accessible through <strong>\/etc\/mongod.conf<\/strong>.<\/p><p>Use a text editor like <a href=\"\/tutorials\/how-to-install-and-use-nano-text-editor\">nano<\/a> to open and modify the file. Here&rsquo;s the command:<\/p><pre class=\"wp-block-preformatted\">sudo nano \/etc\/mongod.conf<\/pre><p>There are several configuration options that determine the MongoDB server behavior. For example, <strong>systemLog<\/strong> defines your database logging settings, while <strong>net <\/strong>lets you modify network-related settings.<\/p><p>\n\n\n    <p class=\"warning\">\n        <strong>Warning!<\/strong> Be careful when editing the <strong>mongod<\/strong> configuration file as you may break the service or expose your server to security risk.    <\/p>\n    \n\n\n\n<\/p><p>All options have several parameters. For instance, the <strong>systemLog <\/strong>option has the <strong>path <\/strong>parameter determining the default log storage location, namely <strong>\/var\/log\/mongodb\/mongod.log<\/strong>.<\/p><div class=\"wp-block-image\">\n<figure class=\"aligncenter size-large\"><a href=\"\/tutorials\/wp-content\/uploads\/sites\/2\/2019\/03\/mongod.conf-content-in-nano.png\"><img decoding=\"async\" width=\"1024\" height=\"479\" src=\"https:\/\/imagedelivery.net\/LqiWLm-3MGbYHtFuUbcBtA\/wp-content\/uploads\/sites\/2\/2019\/03\/mongod.conf-content-in-nano.png\/public\" alt=\"The mongod.conf file content in nano\" class=\"wp-image-99395\" srcset=\"https:\/\/imagedelivery.net\/LqiWLm-3MGbYHtFuUbcBtA\/wp-content\/uploads\/sites\/2\/2019\/03\/mongod.conf-content-in-nano.png\/w=1024,fit=scale-down 1024w, https:\/\/imagedelivery.net\/LqiWLm-3MGbYHtFuUbcBtA\/wp-content\/uploads\/sites\/2\/2019\/03\/mongod.conf-content-in-nano.png\/w=300,fit=scale-down 300w, https:\/\/imagedelivery.net\/LqiWLm-3MGbYHtFuUbcBtA\/wp-content\/uploads\/sites\/2\/2019\/03\/mongod.conf-content-in-nano.png\/w=150,fit=scale-down 150w, https:\/\/imagedelivery.net\/LqiWLm-3MGbYHtFuUbcBtA\/wp-content\/uploads\/sites\/2\/2019\/03\/mongod.conf-content-in-nano.png\/w=768,fit=scale-down 768w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/a><\/figure><\/div><p>Instead of deleting the parameters and options, add a hash symbol (<strong>#<\/strong>) to disable them. It is a good practice since you can easily reactivate them by removing the character.<\/p><p>After editing the configuration file, restart MongoDB using this command to apply the changes:<\/p><pre class=\"wp-block-preformatted\">sudo systemctl restart mongod<\/pre><h3 class=\"wp-block-heading\" id=\"h-4-create-a-new-database\">4. Create a New Database<\/h3><p>The MongoDB installation process automatically creates the <strong>admin <\/strong>database. Since it is usually used for administrative purposes like storing authentication data, we recommended creating a new one.<\/p><p>To do so, enter the Mongo shell by running the following command:<\/p><pre class=\"wp-block-preformatted\">mongosh<\/pre><div class=\"wp-block-image\">\n<figure class=\"aligncenter size-large\"><a href=\"\/tutorials\/wp-content\/uploads\/sites\/2\/2019\/03\/mongosh-shell-welcome-message.png\"><img decoding=\"async\" width=\"1024\" height=\"123\" src=\"https:\/\/imagedelivery.net\/LqiWLm-3MGbYHtFuUbcBtA\/wp-content\/uploads\/sites\/2\/2019\/03\/mongosh-shell-welcome-message.png\/public\" alt=\"The mongosh shell welcome message\" class=\"wp-image-99396\" srcset=\"https:\/\/imagedelivery.net\/LqiWLm-3MGbYHtFuUbcBtA\/wp-content\/uploads\/sites\/2\/2019\/03\/mongosh-shell-welcome-message.png\/w=1024,fit=scale-down 1024w, https:\/\/imagedelivery.net\/LqiWLm-3MGbYHtFuUbcBtA\/wp-content\/uploads\/sites\/2\/2019\/03\/mongosh-shell-welcome-message.png\/w=300,fit=scale-down 300w, https:\/\/imagedelivery.net\/LqiWLm-3MGbYHtFuUbcBtA\/wp-content\/uploads\/sites\/2\/2019\/03\/mongosh-shell-welcome-message.png\/w=150,fit=scale-down 150w, https:\/\/imagedelivery.net\/LqiWLm-3MGbYHtFuUbcBtA\/wp-content\/uploads\/sites\/2\/2019\/03\/mongosh-shell-welcome-message.png\/w=768,fit=scale-down 768w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/a><\/figure><\/div><p>MongoDB has the same command for creating and accessing databases. If one with the specified name exists, the shell will switch to it.<\/p><p>For example, the following will switch you to the default database since the name exists:<\/p><pre class=\"wp-block-preformatted\">use admin<\/pre><p>Meanwhile, run this command in the MongoDB shell to create the<strong> customers<\/strong> database:<\/p><pre class=\"wp-block-preformatted\">use customers<\/pre><p>To check all the MongoDB databases in your system, run the following:<\/p><pre class=\"wp-block-preformatted\">show dbs<\/pre><div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><a href=\"\/tutorials\/wp-content\/uploads\/sites\/2\/2019\/03\/terminal-lists-all-mongodb-databases.png\"><img decoding=\"async\" width=\"429\" height=\"96\" src=\"https:\/\/imagedelivery.net\/LqiWLm-3MGbYHtFuUbcBtA\/wp-content\/uploads\/sites\/2\/2019\/03\/terminal-lists-all-mongodb-databases.png\/public\" alt=\"Terminal lists all MongoDB databases\" class=\"wp-image-99397\" srcset=\"https:\/\/imagedelivery.net\/LqiWLm-3MGbYHtFuUbcBtA\/wp-content\/uploads\/sites\/2\/2019\/03\/terminal-lists-all-mongodb-databases.png\/w=429,fit=scale-down 429w, https:\/\/imagedelivery.net\/LqiWLm-3MGbYHtFuUbcBtA\/wp-content\/uploads\/sites\/2\/2019\/03\/terminal-lists-all-mongodb-databases.png\/w=300,fit=scale-down 300w, https:\/\/imagedelivery.net\/LqiWLm-3MGbYHtFuUbcBtA\/wp-content\/uploads\/sites\/2\/2019\/03\/terminal-lists-all-mongodb-databases.png\/w=150,fit=scale-down 150w\" sizes=\"(max-width: 429px) 100vw, 429px\" \/><\/a><\/figure><\/div><h3 class=\"wp-block-heading\" id=\"h-5-create-a-new-user\">5. Create a New User<\/h3><p>Since MongoDB has no default account, you must create a new user for each database and set their privileges. You can do so using the <strong>db.createUser() <\/strong>function.<\/p><p>For example, we will create a new<strong> root<\/strong> MongoDB<strong> <\/strong>user account with full permissions. After switching to the database <strong>admin<\/strong> mode using the <strong>use <\/strong>command, enter:<\/p><pre class=\"wp-block-preformatted\">db.createUser(\n  {\n    user: \"root\",\n    pwd: \"$tr0ngPa$$w0rD\",   \n    roles: [ { role: \"root\", db: \"admin\" } ]\n  }\n)<\/pre><p>You can write the function in one or multiple lines. Remember to pay attention to the capitalization since it is case-sensitive.<\/p><p><a href=\"https:\/\/www.mongodb.com\/docs\/manual\/reference\/built-in-roles\/\" target=\"_blank\" rel=\"noreferrer noopener\">MongoDB has various roles<\/a>, like <strong>dbAdmin<\/strong>, <strong>dbOwner<\/strong>, and <strong>readWrite<\/strong>. We recommend granting a user account minimal permissions to help avoid unauthorized modifications and data access.<\/p><p>\n\n\n<div><p class=\"important\"><strong>Important!<\/strong> MongoDB user management relies on databases, meaning you must run the <strong>use<\/strong> command before entering a function.<\/p><\/div>\n\n\n\n<\/p><p>To show all users within your current database, use this command:<\/p><pre class=\"wp-block-preformatted\">show users<\/pre><div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><a href=\"\/tutorials\/wp-content\/uploads\/sites\/2\/2019\/03\/terminal-lists-users-in-the-current-mongodb-database.png\"><img decoding=\"async\" width=\"824\" height=\"267\" src=\"https:\/\/imagedelivery.net\/LqiWLm-3MGbYHtFuUbcBtA\/wp-content\/uploads\/sites\/2\/2019\/03\/terminal-lists-users-in-the-current-mongodb-database.png\/public\" alt=\"Terminal lists users in the current MongoDB database\" class=\"wp-image-99398\" srcset=\"https:\/\/imagedelivery.net\/LqiWLm-3MGbYHtFuUbcBtA\/wp-content\/uploads\/sites\/2\/2019\/03\/terminal-lists-users-in-the-current-mongodb-database.png\/w=824,fit=scale-down 824w, https:\/\/imagedelivery.net\/LqiWLm-3MGbYHtFuUbcBtA\/wp-content\/uploads\/sites\/2\/2019\/03\/terminal-lists-users-in-the-current-mongodb-database.png\/w=300,fit=scale-down 300w, https:\/\/imagedelivery.net\/LqiWLm-3MGbYHtFuUbcBtA\/wp-content\/uploads\/sites\/2\/2019\/03\/terminal-lists-users-in-the-current-mongodb-database.png\/w=150,fit=scale-down 150w, https:\/\/imagedelivery.net\/LqiWLm-3MGbYHtFuUbcBtA\/wp-content\/uploads\/sites\/2\/2019\/03\/terminal-lists-users-in-the-current-mongodb-database.png\/w=768,fit=scale-down 768w\" sizes=\"(max-width: 824px) 100vw, 824px\" \/><\/a><\/figure><\/div><p>To test the database connection, quit the MongoDB console by entering the <strong>exit<\/strong> command on Terminal and run the following:<\/p><pre class=\"wp-block-preformatted\">mongosh --port [port] -u [username] -p '[password]' '[database]'<\/pre><p>For example, we will connect to the <strong>root <\/strong>user using the default port of <strong>27017<\/strong>:<\/p><pre class=\"wp-block-preformatted\">mongosh --port 27017 -u root -p '$tr0ngPa$$w0rD' 'admin'<\/pre><h3 class=\"wp-block-heading\" id=\"h-6-enable-remote-authentication\">6. Enable Remote Authentication<\/h3><p>By default, MongoDB authorizes all logins from the host machine. While this is sufficient for local deployment, you may encounter problems when pushing an application from another device.<\/p><p>To access MongoDB from another system, modify the <strong>mongod.conf<\/strong> file&rsquo;s <strong>bindIP <\/strong>parameter. Its default value is the local IP address, meaning it denies access from other machines than the host server.<\/p><p>\n\n\n    <p class=\"warning\">\n        <strong>Warning!<\/strong> Be careful when changing IP addresses in your MongoDB configuration file as it can make your database public, exposing your service to serious security risks.    <\/p>\n    \n\n\n\n<\/p><p>Use<strong> nano <\/strong>to open the <strong>mongod<\/strong>.<strong>conf <\/strong>file and add your VPS public IP address to the <strong>bindIP<\/strong> parameter, like so:<\/p><pre class=\"wp-block-preformatted\">bindIP: 127.0.0.1,123.123.12.1<\/pre><p>We don&rsquo;t recommend changing the value to<strong> 0.0.0.0<\/strong> since allowing all systems&rsquo; IP addresses to connect will expose your server to cyber attacks.<\/p><p>To ensure only permissive users can access your database, enable authentication by adding the following line under the <strong>security <\/strong>section:<\/p><pre class=\"wp-block-preformatted\">authorization: enabled<\/pre><p>Ensure you have removed the hash symbol at the beginning of the <strong>security <\/strong>option. Press <strong>Ctrl + X<\/strong>, <strong>Y<\/strong>, and <strong>Enter<\/strong> to save the changes. Restart MongoDB using the following command:<\/p><pre class=\"wp-block-preformatted\">sudo systemctl restart mongod<\/pre><p>After installing the Mongo utility in your local system, you should be able to connect to the database server by running this command syntax:<\/p><pre class=\"wp-block-preformatted\">mongo \"mongodb:\/\/user:password@ip:port\/?authSource=database_name\"<\/pre><p>For example, we will connect to MongoDB using the <strong>root <\/strong>user, with the <strong>admin<\/strong> database as the authenticator. Here&rsquo;s the command:<\/p><pre class=\"wp-block-preformatted\">mongo \"mongodb:\/\/root:$tr0ngPa$$w0rD@123.123.1.1:27017\/?authSource=admin\"<\/pre><p>To improve remote access security, limit the MongoDB listening port only to your local system&rsquo;s IP address. In Ubuntu, you can do so using the <a href=\"\/tutorials\/how-to-configure-firewall-on-ubuntu-using-ufw\/\">Uncomplicated Firewall (UFW)<\/a> command:<\/p><pre class=\"wp-block-preformatted\">sudo ufw allow from your_local_machine_ip_address to any port 27017<\/pre><p>It means the firewall will only accept connections from the specified IP address and block others.<\/p><p>If you use Hostinger VPS, you can also ask our <a href=\"https:\/\/www.hostinger.com\/blog\/kodee\">AI assistant, Kodee<\/a>, to configure the firewall rules for your server. Simply ask it, &ldquo;Open port <strong>27017<\/strong> to <strong>your_local_machine_ip_address<\/strong> and block connections from other IP addresses.&rdquo;<\/p><p>\n\n\n<div class=\"protip\">\n                    <h4 class=\"title\">Learn More About VPS and Databases<\/h4>\n                    <p><br>\n<a href=\"\/tutorials\/mysql-show-users\/\">How to Show Users in MySQL Using Linux Commands<\/a><br>\n<a href=\"\/tutorials\/how-to-install-postgresql-on-ubuntu\">How to Install PostgreSQL on Ubuntu<\/a><br>\n<a href=\"\/tutorials\/how-to-install-and-setup-phpmyadmin-on-ubuntu\">How to Install phpMyAdmin on Ubuntu.<\/a><\/p>\n                <\/div>\n\n\n\n<\/p><h2 class=\"wp-block-heading\" id=\"h-conclusion\">Conclusion<\/h2><p>MongoDB is a free, open-source NoSQL database management system (DBMS) popular for large-scale websites or applications. Since it doesn&rsquo;t use a fixed schematic structure to store data, it is more flexible and scalable than SQL.<\/p><p>In this article, we have provided the MongoDB installation guide for Ubuntu. Here&rsquo;s the recap:<\/p><ol class=\"wp-block-list\">\n<li><strong>Install MongoDB<\/strong>. Import the official MongoDB repository key using <strong>GnuPG<\/strong> and <strong>cURL<\/strong>. Create the list file and install it using the APT package manager.<\/li>\n\n\n\n<li><strong>Start MongoDB<\/strong>. Run the MongoDB instance using the <strong>systemctl <\/strong>command. Enable the daemon to ensure it starts automatically during the system startup.<\/li>\n\n\n\n<li><strong>Set up MongoDB<\/strong>. Open the <strong>mongod.conf<\/strong> configuration file using a text editor. Modify your database settings and restart Mongod to apply the changes.<\/li>\n\n\n\n<li><strong>Create a new database<\/strong>. Enter the Mongo shell using the <strong>mongosh<\/strong> command. Run the <strong>use<\/strong> command to create a new MongoDB database or access an existing one if the specified name exists.<\/li>\n\n\n\n<li><strong>Create a new user<\/strong>. Access the MongoDB shell and enter the <strong>db.createUser() <\/strong>function to create a new user in your current database. Set its password and permissions.<\/li>\n\n\n\n<li><strong>Enable remote authentication<\/strong>. Change the<strong> mongod.conf <\/strong>file&rsquo;s <strong>bindIP<\/strong> settings to enable remote access to your database. To improve security, enable the MongoDB authentication setting and limit the permitted IP address using UFW.<\/li>\n<\/ol><p>When deploying MongoDB, use a VPS with full root access and vast software compatibility like Hostinger&rsquo;s to ensure a smooth configuration process. We also provide a one-click OS installer and the Browser terminal to streamline the task.<\/p><h2 class=\"wp-block-heading\" id=\"h-how-to-install-mongodb-on-ubuntu-faq\">How to Install MongoDB on Ubuntu FAQ<\/h2><p>In this section, we will answer several common questions about installing MongoDB on Ubuntu.<\/p><div class=\"schema-faq wp-block-yoast-faq-block\"><div class=\"schema-faq-section\" id=\"faq-question-1701749347917\"><h3 class=\"schema-faq-question\">How to Check if MongoDB Is Installed Correctly?<\/h3> <p class=\"schema-faq-answer\">To verify whether your Ubuntu system has MongoDB installed, check its version number using the <strong>mongod &ndash;version <\/strong>command.<br><br>If you see the MongoDB version, it means the database is configured properly. Alternatively, enter the <strong>mongosh <\/strong>command and check whether Terminal returns an error.<\/p> <\/div> <div class=\"schema-faq-section\" id=\"faq-question-1701749363972\"><h3 class=\"schema-faq-question\">How to Uninstall MongoDB?<\/h3> <p class=\"schema-faq-answer\">To uninstall MongoDB Community Edition, stop the instance by running <strong>sudo service mongod stop<\/strong>.<strong> <\/strong>Then, remove the package using the <strong>apt purge &ldquo;mongodb-org*&rdquo; <\/strong>command.<br><br>Delete databases and log files in <strong>\/var\/log\/mongodb<\/strong> and <strong>var\/lib\/mongodb<\/strong> directories using the <a href=\"\/tutorials\/how-to-remove-files-and-folders-using-linux-command-line\/\"><strong>rm <\/strong>command<\/a>.<\/p> <\/div> <div class=\"schema-faq-section\" id=\"faq-question-1701749364972\"><h3 class=\"schema-faq-question\">What&rsquo;s the Difference Between MongoDB and MongoDB Community Edition?<\/h3> <p class=\"schema-faq-answer\">MongoDB Community Edition is the free and open-source version of the database management solution. MongoDB may refer to the Enterprise Advanced edition or the cloud-based Atlas.<\/p> <\/div> <\/div>\n","protected":false},"excerpt":{"rendered":"<p>MongoDB is one of the most popular open-source NoSQL database managers. It stores data in a formatted document instead of [&#8230;]<\/p>\n<p><a class=\"btn btn-secondary understrap-read-more-link\" href=\"\/tutorials\/how-to-install-mongodb-on-ubuntu\">Read More&#8230;<\/a><\/p>\n","protected":false},"author":279,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"rank_math_title":"Install MongoDB on Ubuntu: A Step-by-Step Guide","rank_math_description":"Learn how to install MongoDB on Ubuntu: 1. Install MongoDB 2. Start MongoDB Service 3. Configure MongoDB 4. Create a New Database + more.","rank_math_focus_keyword":"install mongodb ubuntu","footnotes":""},"categories":[22646,22644],"tags":[],"class_list":["post-16054","post","type-post","status-publish","format-standard","hentry","category-pre-installed-applications","category-vps"],"hreflangs":[{"locale":"en-US","link":"https:\/\/www.hostinger.com\/tutorials\/how-to-install-mongodb-on-ubuntu","default":0},{"locale":"es-ES","link":"https:\/\/www.hostinger.com\/es\/tutoriales\/como-instalar-mongodb-ubuntu","default":0},{"locale":"id-ID","link":"https:\/\/www.hostinger.com\/id\/tutorial\/install-mongodb-ubuntu","default":0},{"locale":"en-UK","link":"https:\/\/www.hostinger.com\/uk\/tutorials\/how-to-install-mongodb-on-ubuntu","default":0},{"locale":"en-MY","link":"https:\/\/www.hostinger.com\/my\/tutorials\/how-to-install-mongodb-on-ubuntu\/","default":0},{"locale":"en-PH","link":"https:\/\/www.hostinger.com\/ph\/tutorials\/how-to-install-mongodb-on-ubuntu\/","default":0},{"locale":"es-MX","link":"https:\/\/www.hostinger.com\/mx\/tutoriales\/como-instalar-mongodb-ubuntu","default":0},{"locale":"es-CO","link":"https:\/\/www.hostinger.com\/co\/tutoriales\/como-instalar-mongodb-ubuntu","default":0},{"locale":"es-AR","link":"https:\/\/www.hostinger.com\/ar\/tutoriales\/como-instalar-mongodb-ubuntu","default":0},{"locale":"en-IN","link":"https:\/\/www.hostinger.com\/in\/tutorials\/how-to-install-mongodb-on-ubuntu","default":0},{"locale":"en-CA","link":"https:\/\/www.hostinger.com\/ca\/tutorials\/how-to-install-mongodb-on-ubuntu","default":0},{"locale":"en-AU","link":"https:\/\/www.hostinger.com\/au\/tutorials\/how-to-install-mongodb-on-ubuntu","default":0},{"locale":"en-NG","link":"https:\/\/www.hostinger.com\/ng\/tutorials\/how-to-install-mongodb-on-ubuntu","default":0}],"_links":{"self":[{"href":"https:\/\/www.hostinger.com\/tutorials\/wp-json\/wp\/v2\/posts\/16054","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.hostinger.com\/tutorials\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.hostinger.com\/tutorials\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.hostinger.com\/tutorials\/wp-json\/wp\/v2\/users\/279"}],"replies":[{"embeddable":true,"href":"https:\/\/www.hostinger.com\/tutorials\/wp-json\/wp\/v2\/comments?post=16054"}],"version-history":[{"count":26,"href":"https:\/\/www.hostinger.com\/tutorials\/wp-json\/wp\/v2\/posts\/16054\/revisions"}],"predecessor-version":[{"id":139030,"href":"https:\/\/www.hostinger.com\/tutorials\/wp-json\/wp\/v2\/posts\/16054\/revisions\/139030"}],"wp:attachment":[{"href":"https:\/\/www.hostinger.com\/tutorials\/wp-json\/wp\/v2\/media?parent=16054"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.hostinger.com\/tutorials\/wp-json\/wp\/v2\/categories?post=16054"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.hostinger.com\/tutorials\/wp-json\/wp\/v2\/tags?post=16054"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}