💾

mongodb

8 notes  •  Databases

Install MongoDB on an AWS EC2 Instance (Ubuntu)

Install MongoDB Community Edition on an Ubuntu EC2 instance and configure security groups to allow the necessary connections.

Prerequisites

  • EC2 instance running Ubuntu 20.04/22.04
  • Security group with port 27017 open from trusted IPs only

Step 1 - Add MongoDB Repository

curl -fsSL https://www.mongodb.org/static/pgp/server-6.0.asc | \
    sudo gpg -o /usr/share/keyrings/mongodb-server-6.0.gpg --dearmor

echo "deb [ arch=amd64,arm64 signed-by=/usr/share/keyrings/mongodb-server-6.0.gpg ]" \
    "https://repo.mongodb.org/apt/ubuntu focal/mongodb-org/6.0 multiverse" | \
    sudo tee /etc/apt/sources.list.d/mongodb-org-6.0.list

Step 2 - Install MongoDB

sudo apt-get update
sudo apt-get install -y mongodb-org

Step 3 - Start and Enable MongoDB

sudo systemctl start mongod
sudo systemctl enable mongod
sudo systemctl status mongod

Step 4 - Secure the EC2 Security Group

In the AWS Console, add an inbound rule to the instance security group:

  • Type: Custom TCP | Port: 27017 | Source: your IP or VPC CIDR only

Verify

mongosh
db.runCommand({ connectionStatus: 1 })

Install MongoDB Community Edition on Amazon Linux

Install MongoDB Community Edition on Amazon Linux using the official MongoDB RPM repository.

Step 1 - Create the MongoDB Repo File

sudo tee /etc/yum.repos.d/mongodb-org-6.0.repo <<'EOF'
[mongodb-org-6.0]
name=MongoDB Repository
baseurl=https://repo.mongodb.org/yum/amazon/2/mongodb-org/6.0/x86_64/
gpgcheck=1
enabled=1
gpgkey=https://www.mongodb.org/static/pgp/server-6.0.asc
EOF

Step 2 - Install

sudo yum install -y mongodb-org

Step 3 - Start and Enable

sudo systemctl start mongod
sudo systemctl enable mongod

MongoDB Package Components

  • mongodb-org — meta-package that installs all components
  • mongodb-org-server — the mongod daemon
  • mongodb-org-mongos — the mongos sharding daemon
  • mongodb-org-shell — the mongo shell
  • mongodb-org-tools — import/export and backup utilities

Verify

mongod --version
mongosh --eval "db.runCommand({connectionStatus:1})"

Allow Remote Access to MongoDB

By default MongoDB only listens on 127.0.0.1 and has no authentication. Follow these steps to enable remote access securely.

Step 1 - Enable Authentication First

mongosh
use admin
db.createUser({
  user: "admin",
  pwd: "StrongPassword!",
  roles: [{ role: "userAdminAnyDatabase", db: "admin" }, "readWriteAnyDatabase"]
})

Step 2 - Configure mongod to Listen on All Interfaces

Edit /etc/mongod.conf:

net:
  port: 27017
  bindIp: 0.0.0.0   # or specify specific IPs

security:
  authorization: enabled
sudo systemctl restart mongod

Step 3 - Open Firewall Port

# UFW
sudo ufw allow from YOUR_IP to any port 27017

# firewalld
sudo firewall-cmd --permanent --add-rich-rule='rule family="ipv4" source address="YOUR_IP" port port=27017 protocol=tcp accept'
sudo firewall-cmd --reload

Verify Remote Connection

mongosh --host SERVER_IP --port 27017 -u admin -p --authenticationDatabase admin

Notes

  • Never expose MongoDB to 0.0.0.0 without authentication and a firewall rule. Unrestricted MongoDB instances are frequently compromised.

Install MongoDB PHP Extension in Plesk

Install the MongoDB PHP driver (ext-mongodb) on a Plesk server using PECL or the Plesk extension manager.

Option 1 - Install via Plesk PHP Extensions

  1. Log in to the Plesk admin panel.
  2. Go to Tools & Settings → PHP Settings.
  3. Select the PHP version your site uses.
  4. Under Additional directives, look for PECL extension management, or use the CLI below.

Option 2 - Install via CLI

# Find the PECL binary for your PHP version
/opt/plesk/php/8.1/bin/pecl install mongodb

# Add to php.ini
echo "extension=mongodb.so" >> /opt/plesk/php/8.1/etc/php.ini

# Restart PHP-FPM
plesk bin php_handler --reread

Option 3 - Install via Composer (recommended)

composer require mongodb/mongodb

Verify

/opt/plesk/php/8.1/bin/php -m | grep mongodb

Install MongoDB on a cPanel Server (CentOS/RHEL)

Install MongoDB on a cPanel/WHM server running CentOS or RHEL using the official MongoDB yum repository.

Step 1 - Create the MongoDB Repo

cat > /etc/yum.repos.d/mongodb.repo <<'EOF'
[mongodb-org-6.0]
name=MongoDB Repository
baseurl=https://repo.mongodb.org/yum/redhat/$releasever/mongodb-org/6.0/x86_64/
gpgcheck=1
enabled=1
gpgkey=https://www.mongodb.org/static/pgp/server-6.0.asc
EOF

Step 2 - Install MongoDB

yum install -y mongodb-org

Step 3 - Start and Enable

chkconfig mongod on
service mongod start

Step 4 - Install the PHP Extension (Optional)

pecl install mongodb

Add extension=mongodb.so to the relevant php.ini for your PHP version.

Verify

service mongod status
mongosh --eval "db.runCommand({connectionStatus:1})"

Fix MongoDB Not Starting (Socket File Error)

If MongoDB fails to start after a crash or unclean shutdown, a stale socket file in /tmp may be blocking startup.

Symptom

MongoDB fails to start with an error like: Failed to unlink socket file /tmp/mongodb-27017.sock.

Fix

# Remove the stale socket file
sudo rm -f /tmp/mongodb-27017.sock

# Start MongoDB
sudo systemctl start mongod

Additional Checks

# Check MongoDB logs for errors
sudo tail -50 /var/log/mongodb/mongod.log

# Check if another process is using port 27017
sudo ss -tlnp | grep 27017

# Verify data directory permissions
ls -la /var/lib/mongodb/

Verify

sudo systemctl status mongod

Install MongoDB on Debian 11

Install MongoDB Community Edition on Debian 11 (Bullseye) from the official MongoDB repository.

Step 1 - Install Dependencies and Import GPG Key

sudo apt-get install -y gnupg2 wget curl

curl -fsSL https://www.mongodb.org/static/pgp/server-6.0.asc | \
    sudo gpg -o /usr/share/keyrings/mongodb-server-6.0.gpg --dearmor

Step 2 - Add the MongoDB Repository

echo "deb [ signed-by=/usr/share/keyrings/mongodb-server-6.0.gpg]" \
    "https://repo.mongodb.org/apt/debian bullseye/mongodb-org/6.0 main" | \
    sudo tee /etc/apt/sources.list.d/mongodb-org-6.0.list

sudo apt-get update

Step 3 - Install and Start MongoDB

sudo apt-get install -y mongodb-org
sudo systemctl start mongod
sudo systemctl enable mongod

Verify

mongod --version
sudo systemctl status mongod

Create a MongoDB User and Connect with mongosh

Create a MongoDB database user and connect to a specific database using mongosh with authentication.

Connect as Admin

mongosh -u admin -p --authenticationDatabase admin

Create a New User

use reportGen

db.createUser({
  user: "report",
  pwd: "report@123",
  roles: [{ role: "userAdmin", db: "reportGen" }]
})

Common Roles

  • read — read-only access to the database
  • readWrite — read and write access
  • dbAdmin — administrative tasks (index management, etc.)
  • userAdmin — manage users for the database
  • readWriteAnyDatabase — read/write on all databases (admin only)

Connect as the New User

mongosh -u report -p --authenticationDatabase reportGen