MongoDB uses a flexible JSON-like documents data store where fields are not unique and can vary from one document to another. It also doesn’t require a predefined schema, and the data structure can be changed anytime during data modifications. The steps below will show you how to install and configure MongoDB Community Edition on Ubuntu Linux. The installation is pretty straightforward. MongoDB packages are included in Ubuntu default repositories, however, the versions in Ubuntu repositories aren’t the latest. To install the latest, you will have to install the MongoDB package repository on Ubuntu Linux, and this tutorial will show you how. Also, for students and new users learning Linux, the easiest place to start learning is Ubuntu Linux. Ubuntu is the modern, open-source Linux operating system for desktops, servers, and other devices. To get started with installing MongoDB on Ubuntu Linux, follow the steps below.
How to add MongoDB repository on Ubuntu Linux
As mentioned above, one can simply run the apt-get install command on Ubuntu to download and install MongoDB. However, the versions in Ubuntu repositories are typically not the latest. To install the latest, we’ll have to add the MongoDB repository to Ubuntu Linux. But first, run the commands below to install the required packages and dependencies. Then import the MongoDB repository key and create a repository file using the commands below. At the time of this writing, the latest version of MongoDB is version 5.0. You can visit the link below to get details on the future version number, then replace the commands below. MongoDB repositories Next, run the commands below to create a repository file for MongoDB version 5.0. Also, the repository above is for Ubuntu 20.04 (focal). If you’re using another version of Ubuntu Linux, make sure to replace focal with that version code name. Ubuntu 18.04 will be (bionic).
How to install MongoDB on Ubuntu Linux
Once the repository is created and enabled, run the commands below to update the Ubuntu package index and install MongoDB. The commands above will install the following packages along with MongoDB core. The following packages will be installed on your system:
mongodb-org-server – MongoDB server mongodb-org-mongos – MongoDB daemon mongodb-org-shell – The mongo shell, an interactive JavaScript interface to MongoDB. mongodb-org-tools – MongoDB tools for importing and exporting data
After installing MongoDB, the commands below can be used to stop, start and enable MongoDB to automatically startup when the systems boots up. By default, MongoDB listens on port 27017. After installing, the local server should be able to communicate with MongoDB. To verify whether MongoDB is running and active, run the commands below: You should see something like the lines below: To connect to the MongoDB shell, run the commands below: You should see something like the lines below:
Configuring MongoDB on Ubuntu Linux
The MongoDB configuration file is named mongod.conf and is located in the /etc directory. The file is in YAML format. The default configurations are sufficient for most environments. However, if want to enable authentication and other settings, you will have to modify its configuration file. For example, you can enable authentication by changing the line in the configuration file to match the one below. Open the configuration file as root: Then change the line below to enable authentication. Save and restart MongoDB.
How to add MongoDB administrator
As mentioned above, authentication is not enabled for MongoDB and the admin account isn’t activated by default. In a production environment, it may be required to secure your server and enable user authentication. If you want to enable authentication, run the commands to create a new admin user after you’ve logged into the MongoDB server. Access the MongoDB shell by typing the commands below: From the shell, type the commands below to connect to the admin database. Then run the commands below to create a new admin user called admin and password. Replace strong_password_here with password you want to use. You should see a successful message that the admin user was created. Exit and continue below to enable MongoDB login authentication. Now to log in with the admin account, run the commands below. That should do it! Conclusion: This post showed you how to install and configure MongoDB on Ubuntu Linux. If you find any error above or have something to add, please use the comment form below.