Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

How to Install MongoDB With Authentication on EC2 AMI Linux

DZone's Guide to

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.

· Integration Zone ·
Free Resource

The Future of Enterprise Integration: Learn how organizations are re-architecting their integration strategy with data-driven app integration for true digital transformation.

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.

Make your mark on the industry’s leading annual report. Fill out the State of API Integration 2019 Survey and receive $25 to the Cloud Elements store.

Topics:
mongodb ,tutorial ,integration ,ec2 ,ami ,linux ,authentication

Published at DZone with permission of

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}