<\/span><\/h2>\nUbuntu official software package repositories come with version 3.6.3 of MongoDB, but in this article, we will install MongoDB\u00a04.0 which is the latest available version. However, you can always check if a new version of MongoDB is available on their official website.<\/p>\n
In order to install the MongoDB 4.0 Community Edition on Ubuntu, we need to import the public key used by the package management system. We can do that with the following command:<\/p>\n
sudo apt-key adv --keyserver hkp:\/\/keyserver.ubuntu.com:80 --recv 9DA31620334BD75D9DCB49F368818C72E52529D4<\/pre>\nSince we have the key imported now, we can add the MongoDB repository with this next line:<\/p>\n
sudo add-apt-repository 'deb [arch=amd64] https:\/\/repo.mongodb.org\/apt\/ubuntu bionic\/mongodb-org\/4.0 multiverse'<\/pre>\nWe then update the packages list:<\/p>\n
sudo apt update<\/pre>\nAt this point, the repository is enabled and packages list is updated – we can now continue with installing the MongoDB CE package by entering the following command:<\/p>\n
sudo apt install mongodb-org<\/pre>\nThe mongodb-org-server, mongodb-org-mongos, mongodb-org-shell and mongodb-org-tools packages will be installed on your system as a part of the mongodb-org package.<\/p>\n
The MongoDB 4.0 Community Edition is now installed on the server. We then need to start the MongoDB service and enable it to start on boot.<\/p>\n
sudo systemctl start mongod\nsudo systemctl enable mongod<\/pre>\nTo verify the MongoDB installation, we can check the connection status by typing the command:<\/p>\n
mongo --eval 'db.runCommand({ connectionStatus: 1 })'<\/pre>\nYou should have an output similar to this:<\/p>\n
MongoDB shell version v4.0.6\nconnecting to: mongodb:\/\/127.0.0.1:27017\/?gssapiServiceName=mongodb\nImplicit session: session { \"id\" : UUID(\"c806e0e1-ab30-4d41-b882-026c09d2893f\") }\nMongoDB server version: 4.0.6\n{\n\"authInfo\" : {\n\"authenticatedUsers\" : [ ],\n\"authenticatedUserRoles\" : [ ]\n},\n\"ok\" : 1\n}\n<\/pre>\nCongratulations, you have successfully installed MongoDB on your server. The next step will go over configuring your MongoDB server.<\/p>\n
<\/span>Step 3: Configuring MongoDB<\/span><\/h2>\nWe can configure the MongoDB instance by modifying the mongod.conf<\/code> file, located in \/etc\/<\/code>. We’ll use Nano to edit the file, but you can use your preferred text editor if you like.<\/p>\nsudo nano \/etc\/mongod.conf<\/pre>\nIn order to allow remote connections and better secure MongoDB, we will make some changes in the configuration file. MongoDB by default listens for connections on port 27017 on localhost (IP 127.0.0.1) only. In order to allow a remote MongoDB connection, you need to add your server IP address to the MongoDB configuration file. This is shown as an example below:<\/p>\n
bind_ip = 127.0.0.1, your_server_ip<\/span>\n#port = 27017\n\nsecurity:\nauthorization: enabled<\/pre>\nWe also included an authorization option that will regulate user access to MongoDB databases. If the authorization option is not enabled, then each user will have access to all the MongoDB databases and can perform any action, which is a security risk. After we save the changes in the MongoDB configuration file, we need to restart the MongoDB service with the following command for the changes to take effect:<\/p>\n
sudo systemctl restart mongod<\/pre>\n<\/span>Step 4: Creating Administrative MongoDB User<\/span><\/h2>\nSince we configured the MongoDB server and enabled the authentication option, we now need to create an administrative MongoDB user account which will be used to manage the MongoDB instance.<\/p>\n
To access the mongo shell, type:<\/p>\n
mongo<\/pre>\nNext, we will use the following command to connect to the admin database:<\/p>\n
use admin<\/pre>\nOutput:<\/p>\n
switched to db admin<\/pre>\nWe will perform the following command to create a new administrative user called mongo_admin with the userAdminAnyDatabase role:<\/p>\n
db.createUser(\n{\nuser: \"mongo_admin\", \npwd: \"Strong_Pas$w0rd<\/span>\", \nroles: [ { role: \"userAdminAnyDatabase\", db: \"admin\" } ]\n}\n)<\/pre>\nAnd we will get this as output:<\/p>\n
Successfully added user: {\n\"user\" : \"mongo_admin\",\n\"roles\" : [\n{\n\"role\" : \"userAdminAnyDatabase\",\n\"db\" : \"admin\"\n}\n]\n}<\/pre>\nDon’t forget to replace ‘Strong_Pas$w0rd’ <\/span>with an actual strong password. We can now exit the mongo shell:<\/p>\nquit()<\/pre>\n<\/span>Step 5: Verifying the Administrative User’s Access<\/span><\/h2>\nTo confirm the changes, we will access the mongo shell using the mongo_admin user we created:<\/p>\n
mongo -u mongo_admin -p --authenticationDatabase admin\n\nuse admin\n\nshow users<\/pre>\nAnd if you followed all of the steps so far, you should have the following output:<\/p>\n
Output:<\/p>\n
> use admin\nswitched to db admin\n> show users\n{\n\"_id\" : \"admin.mongo_admin\",\n\"user\" : \"mongo_admin\",\n\"db\" : \"admin\",\n\"roles\" : [\n{\n\"role\" : \"userAdminAnyDatabase\",\n\"db\" : \"admin\"\n}\n],\n\"mechanisms\" : [\n\"SCRAM-SHA-1\",\n\"SCRAM-SHA-256\"\n]\n}<\/pre>\nYou can also try to list the users when you access the mongo shell (without arguments) and use the same commands as we used before.<\/p>\n
<\/span>Step 6: Allow Remote MongoDB Access in the Firewall<\/span><\/h2>\nIn this step, we will provide remote access to our MongoDB by allowing it in our firewall. If you do not use any firewall on your server, you can skip this step. The default port of MongoDB is 27017, so in our examples, we’ll show you how to allow this port through your firewall.<\/p>\n
If you are using the UFW (Uncomplicated Firewall) then you can use the following tips on how to allow MongoDB to be remotely accessible.<\/p>\n
To allow access to MongoDB on its default port (27017) from everywhere, you can use the following command:<\/p>\n
sudo ufw allow 27017<\/pre>\nTo allow access to MongoDB on its default port 27017 from a specific IP address only, you can use the command:<\/p>\n
sudo ufw allow from allowed_IP_address<\/span>\/32 to any port 27017<\/pre>\nYou will need to change allowed_IP_address<\/span> with the actual IP address that you want to use.<\/p>\nYou can confirm the change to the firewall settings with:<\/p>\n
sudo ufw status<\/pre>\nOutput:<\/p>\n
Status: active\n\nTo Action From\n-- ------ ----\n27017 ALLOW Anywhere\nOpenSSH ALLOW Anywhere\n27017 (v6) ALLOW Anywhere (v6)\nOpenSSH (v6) ALLOW Anywhere (v6)\n<\/pre>\n