In this tutorial, we’ll walk through the process of installing MongoDB on Amazon Linux 2023 without using Docker. MongoDB is a popular NoSQL database used for building modern applications, and Amazon Linux is a stable, secure, and high-performance environment. Installing MongoDB directly on the OS (as opposed to using Docker) can offer performance advantages and fewer layers to manage. This is a complete guide to installing MongoDB on AWS Amazon Linux 2023. Includes fixing ‘command not found’ errors, configuring the yum repo, and enabling authentication.
Let’s dive in!
Table of Contents
Prerequisites
- An Amazon EC2 instance is running Amazon Linux 2023.
- Root or sudo access to the instance.
Step-by-step Installation Guide
Connect to Your Instance
Using SSH, connect to your Amazon EC2 instance.
ssh ec2-user@your-instance-ip-address
Update the System
Before installing any new software, it’s always a good idea to update the existing packages:
sudo dnf update -y
Configure the MongoDB Repository
To install MongoDB, you’ll first need to create a .repo file under /etc/yum.repos.d/:
nano /etc/yum.repos.d/mongo.repo
[mongodb-org-8.0]
name=MongoDB Repository
baseurl=https://repo.mongodb.org/yum/amazon/2023/mongodb-org/8.0/x86_64/
gpgcheck=1
enabled=1
gpgkey=https://pgp.mongodb.com/server-8.0.asc
Note: This example uses the MongoDB 8.0 repository. For the latest version, you may want to check MongoDB’s official site.
Install MongoDB
Install the Mongosh with OpenSSL first
sudo dnf remove -y mongodb-mongosh
sudo dnf install -y mongodb-mongosh-shared-openssl3
sudo dnf install -y mongodb-mongosh
Now that you’ve set up the repository, you can install MongoDB:
sudo dnf install -y mongodb-org
This command will install several packages, including:
mongodb-org-server: The MongoDB server itself.
mongodb-org-mongos: The MongoDB Shard service.
mongodb-org-shell: The MongoDB shell is useful for administrative tasks.
mongodb-org-tools: Essential MongoDB tools.
Start MongoDB
With MongoDB installed, you can now start the service:
sudo systemctl start mongod
If you want MongoDB to start automatically at boot:
sudo systemctl enable mongod
Verify the Installation
Check the MongoDB service status:
sudo systemctl status mongod
You can also connect to your MongoDB server using the MongoDB shell:
mongosh
The MongoDB shell prompt should greet you.
Use the admin DB and create a root user
use admin
db.createUser(
{
user: "adminUser",
pwd: passwordPrompt(),
roles: [
{ role: "root", db: "admin" }
]
}
)
After the user is created, exit the shell to proceed with enabling authentication.
You must now edit the MongoDB configuration file, typically located at /etc/mongod.conf, to enforce authentication.
sudo nano /etc/mongod.conf
Add or uncomment the following lines in the file
security:
authorization: enabled
Restart the MongoDB service
sudo systemctl restart mongod
After the service has restarted, you can no longer connect without authenticating. Use your new adminUser to log in.
mongosh --port 27017 --authenticationDatabase "admin" -u "adminUser" -p
Troubleshooting Common MongoDB Errors on Amazon Linux 2023
Even with a smooth installation, you might run into environment-specific hurdles. Here is how to resolve the most frequent issues developers face on this OS.
Error: “error while loading shared libraries: libcrypto.so.1.1”
This is the most common error on Amazon Linux 2023 because the OS uses OpenSSL 3 by default, while older MongoDB tools often look for OpenSSL 1.1.
- The Fix: You likely skipped the shared OpenSSL package installation. Run the following to install the compatibility layer.
sudo dnf install -y mongodb-mongosh-shared-openssl3
Error: “Failed to start mongod.service: Unit mongod.service not found.”
If you see this immediately after installation, the system daemon hasn’t registered the new service file yet.
- The Fix: Reload the daemon and try starting again
sudo systemctl daemon-reload sudo systemctl start mongod
Error: “Job for mongod.service failed because the control process exited with error code.”
This generic error usually means MongoDB cannot write to its data or log directories, often due to permission issues.
- The Fix: Ensure the
mongoduser owns the correct directories
sudo chown -R mongod:mongod /var/lib/mongo sudo chown -R mongod:mongod /var/log/mongodb sudo systemctl restart mongod
Tip: You can see the exact error log by running cat /var/log/mongodb/mongod.log
Securing Your MongoDB Deployment (Production Checklist)
Running a database on a public cloud instance like AWS EC2 requires strict security layers. While enabling authentication (as done above) is the first step, you must also secure the network layer.
Step 1: Configure AWS Security Groups (Crucial)
Since we modified the configuration to bind to 0.0.0.0 (allowing external connections), Your database is theoretically exposed to the entire internet. You must restrict this using the AWS Console.
- Go to the EC2 Dashboard and select your instance.
- Click on the Security tab and click the Security Group ID.
- Edit Inbound Rules.
- Add a rule for Custom TCP on port
27017. - Important: For “Source”, do NOT select “Anywhere (0.0.0.0/0)”. Select “My IP” (to allow only your current location) or the specific IP address of your application server.
Step 2: Verify Authentication Status
Always double-check that your authentication enforcement is active. Try to log in without credentials:
Bash
mongosh --port 27017
If configured correctly, you should be able to connect, but running any command like show dbs should return an authorization error:
Command failed with error 13 (Unauthorized): “command listDatabases requires authentication”
Step 3: Disable Direct IP Access (Optional)
If your application runs on the same EC2 instance as the database, you should revert the bindIp setting in /etc/mongod.conf Back to localhost for maximum security:
YAML
net:
port: 27017
bindIp: 127.0.0.1 # Only allow local connections
Connect with MongoDB Compass
Edit the “/etc/mongod.conf” file and allow
sudo nano /etc/mongod.conf
Add the following minimal configuration to allow remote connections
net:
port: 27017
bindIp: 0.0.0.0
Once you update the configuration file, you must allow inbound traffic to your EC2 instance on the MongoDB port (default: 27017) from your local machine.
After that, you can connect using MongoDB Compass by creating a connection using the following connection string
mongodb://<EC2_Public_IP>:27017
Here is the video tutorial available for you to view
Conclusion
Congratulations! You’ve successfully installed MongoDB on Amazon Linux 2023 without using Docker. You can now proceed to configure and use your MongoDB instance for application development, testing, or production purposes. Remember to consult the official MongoDB documentation for best practices, especially regarding security and optimization.
Now that you have installed it, learn how to securely connect to your MongoDB instance from a remote client.

its to good but we also want how we can connect mongoDB compass to it
Edit the /etc/mongod.conf file and allow
sudo nano /etc/mongod.conf
Add the following minimal configuration to allow remote connections
net:
port: 27017
bindIp: 0.0.0.0