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 Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
  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.

Cray Styris user avatar by
Cray Styris
·
Feb. 10, 17 · Tutorial
Like (1)
Save
Tweet
Share
42.64K 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.

Popular on DZone

  • Implementing Adaptive Concurrency Limits
  • The Key Assumption of Modern Work Culture
  • Pipes And Filters Pattern
  • Connecting Your Devs' Work to the Business

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

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

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: