This guide introduces MongoDB, covering installation, configuration, basic operations, and troubleshooting of a scalable NoSQL document database.
In this guide, we introduce MongoDB, a high-performance, scalable NoSQL document database. You will learn what MongoDB is, how to install and configure a MongoDB server, and how to perform basic operations and troubleshooting. MongoDB stores data in JSON-like documents gathered into collections, and multiple collections form a database. A single MongoDB server can host multiple databases. There are two main editions available: the free Community Edition and the commercial Enterprise Edition.Below are examples of JSON documents demonstrating how data can be structured in MongoDB:
MongoDB is available as a managed cloud service through MongoDB Atlas and as an on-premises server. This article focuses on installing a local (on-premises) MongoDB server.
The Community Edition is free and widely used for development and prototyping. The Enterprise Edition offers additional features and support for commercial deployments.
The first step is configuring your package management system with the MongoDB repository and installing the mongodb-org package. Although best practices for user creation and security are available in the official documentation, this guide uses a simplified approach.For instance, on a system using yum, install MongoDB with:
Copy
Ask AI
yum install mongodb-org
Create a repository file (e.g., /etc/yum.repos.d/mongodb-org-4.2.repo) with the content below to configure the MongoDB repository:
After installation, start the MongoDB system service named mongod and verify its status. The main log file is located at /var/log/mongodb/mongod.log.Start MongoDB using:
When MongoDB starts, the log file at /var/log/mongodb/mongod.log provides details about the server startup, including the MongoDB version, and indicates that it is listening on port 27017 of the loopback IP address (127.0.0.1). This default setting permits connections only from the local system—a secure default for development that should be adjusted for production environments.A sample log snippet is shown below:
Copy
Ask AI
cat /var/log/mongodb/mongod.log2020-03-21T18:43:52.820+0000 I CONTROL [main] ***** SERVER RESTARTED *****2020-03-21T18:43:52.823+0000 I CONTROL [main] Automatically disabling TLS 1.0, to force-enable TLS 1.0 specify --sslDisabledProtocols 'none'2020-03-21T18:43:52.982+0000 I CONTROL [initandlisten] MongoDB starting: pid=4227 port=270172020-03-21T18:43:52.982+0000 I CONTROL [initandlisten] db version v4.2.32020-03-21T18:43:52.982+0000 I CONTROL [initandlisten] git version: 6874650b362138d74be53d366bebfc321ea32d42020-03-21T18:43:52.982+0000 I CONTROL [initandlisten] OpenSSL version: OpenSSL 1.0.1e-fips 11 Feb 20132020-03-21T18:43:52.998+0000 I NETWORK [listener] Listening on /tmp/mongodb-27017.sock2020-03-21T18:43:53.521+0000 I NETWORK [listener] Listening on 127.0.0.12020-03-21T18:43:53.521+0000 I NETWORK [listener] waiting for connections on port 27017
The MongoDB configuration file (/etc/mongod.conf) allows you to modify settings such as port number, storage location, and network bind addresses. Here’s an example configuration:
Copy
Ask AI
# Where logging data is written.systemLog: destination: file logAppend: true path: /var/log/mongodb/mongod.log# Settings for data storage.storage: dbPath: /var/lib/mongo journal: enabled: true# Network interfaces and access settings.net: port: 27017 bindIp: 127.0.0.1
By default, MongoDB’s access control is not enabled. For production environments, ensure you enable authentication and implement proper security measures.
To interact with your MongoDB server, use the mongo shell, which provides a JavaScript interface. Launch the shell with:
Copy
Ask AI
mongo
You should see output similar to this:
Copy
Ask AI
MongoDB shell version v4.2.3connecting to: mongodb://127.0.0.1:27017/?compressors=disabled&gssapiServiceName=mongodbImplicit session: session { "id" : UUID("414e0b56-5125-4389-a47a-7046e1af9a60") }MongoDB server version: 4.2.3Server has startup warnings:2020-03-21T18:43:53.421000 I CONTROL [initandlisten] 2020-03-21T18:43:53.421000 I CONTROL [initandlisten] ** WARNING: Access control is not enabled for the database,2020-03-21T18:43:53.421000 I CONTROL [initandlisten] ** Read and write access to data and configuration is unrestricted.2020-03-21T18:43:53.421000 I CONTROL [initandlisten] mongo>
After connecting to the shell, you can explore and manage your databases:
This introduction to MongoDB covered the installation of a local MongoDB instance, basic configuration, and common database operations using the mongo shell. Practice these steps to build a solid foundation, then continue exploring advanced MongoDB features and capabilities. Happy coding!For further information and in-depth tutorials, check out the MongoDB Documentation.