MongoDB 1: Installation and Configuration
MongoDB is a powerful, open-source NoSQL database known for its scalability, flexibility, and ease of use. Unlike traditional relational databases, MongoDB stores data in a flexible, JSON-like format called BSON (Binary JSON). This post will guide you through the installation and configuration process for MongoDB on a Linux server, and introduce key concepts like collections and documents.
Prerequisites
Before starting, ensure you have:
- A Linux server (Ubuntu, CentOS, or another major distribution).
- Sudo or root access to the server.
- Internet access to download MongoDB packages.
Step 1: Install MongoDB
On Ubuntu
-
Import the MongoDB public GPG Key:
wget -qO - https://www.mongodb.org/static/pgp/server-4.4.asc | sudo apt-key add -
-
Create the MongoDB source list file:
echo "deb [ arch=amd64,arm64 ] https://repo.mongodb.org/apt/ubuntu $(lsb_release -cs)/mongodb-org/4.4 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-4.4.list
-
Update the package list:
sudo apt update
-
Install MongoDB:
sudo apt install -y mongodb-org
-
Start and enable MongoDB service:
sudo systemctl start mongod sudo systemctl enable mongod
On CentOS
-
Create the MongoDB repository file:
sudo tee /etc/yum.repos.d/mongodb-org-4.4.repo <<EOF [mongodb-org-4.4] name=MongoDB Repository baseurl=https://repo.mongodb.org/yum/amazon/2/mongodb-org/4.4/x86_64/ gpgcheck=1 enabled=1 gpgkey=https://www.mongodb.org/static/pgp/server-4.4.asc EOF
-
Install MongoDB:
sudo yum install -y mongodb-org
-
Start and enable MongoDB service:
sudo systemctl start mongod sudo systemctl enable mongod
Step 2: Verify the Installation
To verify that MongoDB has been installed and is running, check the status of the MongoDB service:
sudo systemctl status mongod
You should see output indicating that the MongoDB service is active and running.
Step 3: Basic MongoDB Configuration
Access the MongoDB Shell
To perform administrative tasks and interact with your database, access the MongoDB shell:
mongo
Create a New Database
-
Create a new database:
use mydatabase
Create a Collection
Collections in MongoDB are analogous to tables in relational databases. Here’s how to create a collection:
-
Create a collection:
db.createCollection("mycollection")
Insert a Document
Documents in MongoDB are JSON-like objects that are stored in collections. Here’s how to insert a document:
-
Insert a document into the collection:
db.mycollection.insertOne({ name: "John Doe", age: 30, position: "Software Engineer" })
Query a Collection
To retrieve data from a collection, use the find
method:
-
Query the collection:
db.mycollection.find()
Update a Document
To update an existing document, use the updateOne
method:
-
Update a document in the collection:
db.mycollection.updateOne({ name: "John Doe" }, { $set: { age: 31 } })
Delete a Document
To delete a document from the collection, use the deleteOne
method:
-
Delete a document from the collection:
db.mycollection.deleteOne({ name: "John Doe" })
Step 4: Implementing Security Best Practices
Enable Authentication
By default, MongoDB does not require authentication. For a production environment, it’s essential to enable authentication.
-
Create an administrative user:
use admin db.createUser({ user: "admin", pwd: "securepassword", roles: [{ role: "root", db: "admin" }] })
-
Enable authentication in the MongoDB configuration file:
Open the MongoDB configuration file (
/etc/mongod.conf
or/etc/mongodb.conf
depending on your distribution) and add the following line:security: authorization: enabled
-
Restart MongoDB:
sudo systemctl restart mongod
Configure a Firewall
Ensure that MongoDB is only accessible from trusted IP addresses. Use firewall rules to restrict access:
-
On Ubuntu:
sudo ufw allow from <trusted_ip> to any port 27017 sudo ufw enable
-
On CentOS:
sudo firewall-cmd --permanent --add-source=<trusted_ip> sudo firewall-cmd --permanent --add-port=27017/tcp sudo firewall-cmd --reload
Key MongoDB Concepts
Collections
Collections in MongoDB are similar to tables in relational databases. They store documents and do not enforce a schema, allowing flexibility in data storage.
Documents
Documents are the basic unit of data in MongoDB, represented in a JSON-like format called BSON. Each document contains key-value pairs and can have varying fields, making MongoDB schema-less and highly flexible.
Conclusion
Setting up MongoDB on a Linux server involves installing the necessary packages, configuring the database system, and implementing basic security measures. By understanding key concepts like collections and documents, you can start leveraging MongoDB’s powerful features for your data storage needs. Regularly update and monitor your MongoDB server to maintain optimal performance and security.
With these foundational skills, you are well on your way to becoming proficient in MongoDB. Continue exploring advanced features and best practices to further enhance your database management capabilities. Happy data managing!