Run mongodb on ec2
Install mongodb
Launch an ubuntu EC2 instance on AWS.
Change its security groups as
(Make TCP port 27017 in Inbound port range).
Install MongoDB Community Edition.
Install mongodb follow install-mongodb-on-ubuntu.
Run mongodb
Create a file mongod.service under /etc/systemd/system/.
Without –bind_ip_all, you cannot access mongodb cross internet.
Restart=always restarts mongod whenever it be killed or exit with an exception.
Next, start MongoDB with systemctl.
sudo systemctl start mongod
You can also use systemctl to check that the service has started properly.
sudo systemctl status mongod
Output
mongodb.service - High-performance, schema-free document-oriented database
Loaded: loaded (/etc/systemd/system/mongodb.service; enabled; vendor preset: enabled)
Active: active (running) since Mon 2016-04-25 14:57:20 EDT; 1min 30s ago
Main PID: 4093 (mongod)
Tasks: 16 (limit: 512)
Memory: 47.1M
CPU: 1.224s
CGroup: /system.slice/mongodb.service
└─4093 /usr/bin/mongod --quiet --bind_ip_all --dbpath /home/ubuntu/data/db
The last step is to enable automatically starting MongoDB when the system starts.
sudo systemctl enable mongod
The MongoDB server is now configured and running, and you can manage the MongoDB service using the systemctl command (e.g. sudo systemctl stop mongod, sudo systemctl start mongod).
Connect mongod
You can now connect mongod service on EC2 from terminal on your PC with command: mongo --host {ec2 public IP}:{port}
.