DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • Why SAP S/4HANA Landscape Design Impacts Cloud TCO More Than Compute Costs
  • Understanding Custom Authorization Mechanisms in Amazon API Gateway and AWS AppSync
  • Terraform Best Practices: The 24 Practices You Should Adopt
  • Exploring the Architecture of Amazon SQS

Trending

  • Event-Driven Pipelines With Apache Pulsar and Go
  • Building AI-Powered Java Applications With Jakarta EE and LangChain4j
  • Building a Spring AI Assistant With MCP Servers: A Step-by-Step Tutorial
  • Slopsquatting: Building a Scanner That Catches AI-Hallucinated Packages Before They Reach Production
  1. DZone
  2. Data Engineering
  3. Databases
  4. How to Install MongoDB With Authentication on EC2 AMI Linux

How to Install MongoDB With Authentication on EC2 AMI Linux

If you have an EC2 instance running and you have root access to this EC2 instance, then you can install MongoDB with authentication on EC2 AMI Linux in eight easy steps.

By 
Cray Styris user avatar
Cray Styris
·
Feb. 10, 17 · Tutorial
Likes (1)
Comment
Save
Tweet
Share
44.0K Views

Join the DZone community and get the full member experience.

Join For Free

In this tutorial, we will show you how to install MongoDB with authentication on EC2 AMI Linux.

Prerequisites:

  • You have an EC2 instance running.
  • You have root access to this EC2 instance.

1. Connect to the EC2 Instance Using a PEN/PPK File

For MongoDB 3.0, create the below file using VI or any other editor:

vi /etc/yum.repos.d/mongodb-org-3.0.repo 

Add the content below in the above-created file:

[mongodb-org-3.0]
name=MongoDB Repository
baseurl=https://repo.mongodb.org/yum/amazon/2013.03/mongodb-org/3.0/x86_64/
gpgcheck=0
enabled=1

2. Install MongoDB Using the Below Command

sudo yum install -y mongodb-org

3. Start MongoDB Service Using the Below Command

sudo service mongod start

4. Start MongoDB on Reboot

You can optionally ensure that MongoDB will start following a system reboot by issuing the following command:

sudo chkconfig mongod on

Once the service is started, you need to connect to the Mongo shell for creating a user. To connect to the Mongo shell, use the below command:

mongo

If you find this error when using Mongo command:

Failed global initialization: BadValue Invalid or no user locale set. Please ensure LANG and/or LC_* environment variables are set correctly.

...then add the export below:

export LC_ALL=C

6. Select Admin

Once connected successfully to mongo, select admin

use admin

7. Create User

Create user as per below:

db.createUser(
{
user: "myUserAdmin",
pwd: "abc123",
roles: [ { role: "userAdminAnyDatabase", db: "admin" } ]
}
)

7a. Create user for specific database

To create a user for a particular database, repeat Step 6 with the below command:

use

To create a user for the above database:

db.createUser(
{
user: "",
pwd: "mypassword",
roles: [ { role: "readWrite", db: "muddle" }]
}
)

7b. Edit /etc/mongod.conf 

For Mongo 3.x, add this to the config:

security:
authorization: "enabled"

Then, run the below command:

service mongod restart

8. Connect to MongoDB Remotely (Optional)

If you want to connect to MongoDB remotely, edit the below file with VI or any other editor:

vi /etc/mongod.conf

Add IP in bindIp as seen below and restart the MongoDB service.

bindIp: 127.0.0.1,8.8.8.8

And that's it! You've now installed MongoDB with authentication on EC2 AMI Linux.

AWS Amazon Machine Image MongoDB Linux (operating system) authentication

Published at DZone with permission of Cray Styris. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • Why SAP S/4HANA Landscape Design Impacts Cloud TCO More Than Compute Costs
  • Understanding Custom Authorization Mechanisms in Amazon API Gateway and AWS AppSync
  • Terraform Best Practices: The 24 Practices You Should Adopt
  • Exploring the Architecture of Amazon SQS

Partner Resources

×

Comments

The likes didn't load as expected. Please refresh the page and try again.

  • RSS
  • X
  • Facebook

ABOUT US

  • About DZone
  • Support and feedback
  • Community research

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 215
  • Nashville, TN 37211
  • [email protected]

Let's be friends:

  • RSS
  • X
  • Facebook