Getting to Know the New Star in NoSQL

When it comes to databases, besides the well-known relational databases, there is a rising star in the scene—NoSQL (which is short for Not Only SQL). Today, we’ll introduce MongoDB, a key member of this family. As a document-oriented database based on distributed file storage, MongoDB is especially suitable for handling flexible data structures. In this guide, we will walk you through the process of installing MongoDB 3.6 from source on RedHat 6.5.

Preparation Is Key

System Requirements

✅ Operating System: RedHat Enterprise Linux 6.5 x64
✅ Database Version: MongoDB 3.6.3 x64

System Configuration Checklist

  1. Turn off the firewall
    For beginners, it’s recommended to disable the firewall to avoid connection issues:
1
2
3
service iptables stop
# If you see "OK" messages, you're all set
iptables: Setting chains to policy ACCEPT: filter [ OK ]
  1. Disable SELINUX
    Temporarily disable SELinux for smoother operation:
1
setenforce 0

Complete Installation Guide

Download and Extract the Package

1
2
3
4
5
# Download the package from the official source
curl -O https://fastdl.mongodb.org/linux/mongodb-linux-x86_64-3.6.3.tgz

# Extract to the desired directory
tar -xzvf mongodb-linux-x86_64-rhel62-3.6.3.tgz -C /opt/

Set Environment Variables

Edit the ~/.bash_profile file and add this line to make the command available everywhere:

1
export PATH=/opt/mongodb-linux-x86_64-rhel62-3.6.3/bin:$PATH

Create a Dedicated User

1
useradd -M -s /sbin/nologin mongodb  # Create a system user without login rights

Starting the Database: Important Considerations

Set Up Data Storage

1
2
mkdir -p /data/db
chown -R mongodb:mongodb /data # Ensure proper permissions

Choose Your Startup Method

✅ Basic Start:

1
mongod --dbpath /data/db

✅ Advanced Mode (with logging):

1
mongod --auth --dbpath=/data/db --logpath=/data/mongod.log --logappend

When you see this log message, the startup is successful:

1
[initandlisten] waiting for connections on port 27017

Test the Connection

1
2
3
4
mongo --host 127.0.0.1:27017
# After entering, type the ping command to test
> db.runCommand({ping:1})
{ "ok" : 1 } # If you see this response, everything's fine

Security Setup: A Must-Know

Create an Admin Account

  1. Start MongoDB in non-authentication mode:
1
mongod --dbpath /data/db
  1. After connecting, create a superuser:
1
2
3
4
5
6
use admin
db.createUser({
user: "admin",
pwd: "yourStrongPassword",
roles: ["userAdminAnyDatabase"]
})

Creating Regular Users

1
2
3
4
5
6
use your_database
db.createUser({
user: "app_user",
pwd: "securePassword",
roles: ["readWrite"]
})

Enable Authentication

Add the –auth parameter to your startup command, or, preferably, use a configuration file:

1
2
3
4
5
# /etc/mongodb.conf 示例
dbpath = /data/db
logpath = /data/mongod.log
auth = true
bind_ip = 0.0.0.0 # Modify as needed

Avoid Common Pitfalls

Common Error Troubleshooting

🚨 Connection Refused:

1
exception: connect failed

✅ Things to Check:

  1. Is the mongod process running?
  2. Is the firewall disabled?
  3. Is the correct IP bound?

Authentication Tips

1
2
3
4
5
6
# Recommended syntax for newer versions
db.auth({
user: "username",
pwd: "password",
mechanism: "SCRAM-SHA-1"
})

Summary and Recommendations

Using a configuration file for startup is both professional and convenient, making it ideal for production environments. Remember to back up your /data/db directory regularly and protect important data with the –auth parameter. If you encounter issues, check the /data/mongod.log log file; it will usually point you in the right direction. Get started with MongoDB now and embark on your database journey!