You can follow the below steps to connect to MongoDB hosted on an AWS EC2 instance.
Step 1: Connect to your AWS EC2 instance over SSH.
Step 2: Open mongo shell by typing mongo in the EC2 terminal
1 2 |
## open mongo shell mongo |
Step 3: Create a new user and give the user access to your MongoDB database.
1 2 3 4 5 6 7 |
use db.createUser({ user: ' pwd: ' roles: [{ role: 'readWrite', db:' }) |
Step 4: Update the mongo configuration file to allow remote connection.
1 2 3 4 5 6 7 8 9 10 11 |
vi /etc/mongod.conf ## Look for the net line and comment out the bindIp line under it # network interfaces net: port: 27017 # bindIp: 127.0.0.1 <- comment out this line ## Scroll down to the #security: section and add the following line. security: authorization: 'enabled' |
Step 5: Open port 27017 on your EC2 instance security group inbound rule.
Step 6: Restart MongoDB service
1 2 |
## Restart mongod service sudo service mongod restart |
Step 7: Now from your local system, try to connect to MongoDB remotely.
1 2 |
## Connect to mongodb remotely mongo -u |