In my previous article we learn how to install MongoDB on the local machine and how to connect to your local MongoDB instance. In this article I am going to write about MongoDB installation on the “Amazon EC2” and “MongoDB Atlas Server” hosting environment.
If you have missed out my previous article on “How to connect to MongoDB” please visit it to get to know basic before you start reading this article.
Setup your MongoDB on AWS EC2 Instance
To setup MongoDB on Amazon Web Services (AWS) EC2 instance. You need to register first on AWS console site.
For this tutorial I am going to create a new EC2 instance using Bitnami MongoDB Stack with name “bitnami-mongodb-3.4.6-0-linux-debian-8-x86_64-hvm-ebs”.
So lets start, first login to your Amazon console and then go to EC2 dashboard. Your screen will look like the below screen.
Change your default region, according to your preferences and data plan. To change your region, check right corner of your screen in between your name and support label, there is the region. In my case, I have selected “Asia Pacific Mumbai” region.
Now on your EC2 dashboard you have section to create a new Instance. You will find “Launch Instance” button. Click on it to create a new Instance. Creating new instance will create new server for you in the selected data center region.
Once you click on the “Launch Instance” you will see the below screen.
I will explain the above screen, here you have many options to select different images from different vendor.
Now search for “bitnami-mongodb-3.4.6-0-linux-debian-8-x86_64-hvm-ebs” image. You will find the image which we are going to use in this tutorial. Select this image, you will navigate to the next screen.
Here you have to select the instance type “t2.micro” which is available for free. Check the below screen.
Click on the next, you will see the below screen. Keep everything as it on the below screen and click on the next.
Now on 4th steps it will ask for storage space, keep it as 10 GB which is within free storage limits. The screen will look like below.
On the next screen, it will ask for your tags. Add a one tag as a “name” and the value will be your server name. After that move to next. This is the final screen where you setup group security for your instance, the screen will look like below.
On this screen we are given access to this server from all IP address on TCP port 22 for SSH access.
Give name to your security group and click on the “Review and Launch”. Your next screen will look like below. Review all your details.
If you want to change anything, go back and change it. Now click on the “Launch” to launch this new instance. Once you click on “Launch” button you will get pop-up to select an existing key pair or to create a new key pair. This is a very important step. Select a drop-down Value as a ” Create a new Key pair” and type your key name in “key pair name” textbox. I have given name “Mongo-tutorial”.
Download this key pair file, This file will require to access server. After a successful download, click on the launch instances to launch the server. You will get launch status screen. It will take less than a minute to launch new server on this screen, click on the view instance button.
Now our instance is running on AWS services. Now I will access Linux server from my windows machine using Putty. If you have different configuration, then check the AWS documentation “How to connect to EC2 instance here“.
I am logged in on the server using Putty for this instance of Bitnami image and login user name is “Bitnami”.
Once you successfully login, your screen will look like the below.
Now if you type “Mongo” in your root, you will able to see the installed version of MongoDB. Now run the below command on your console to test the server. Your root user password is “bitnami”.
So let’s first change the root password using the below command.
db.changeUserPassword("root", "NEWPASSWORD")
After that I just added one collection to test database to check the new server like below.
bitnami@ip-172-31-22-127:~$ mongo admin --username root -p MongoDB shell version v3.4.6 Enter password: connecting to: mongodb:///opt/bitnami/mongodb/tmp/mongodb-27017.sock/admin MongoDB server version: 3.4.6 Server has startup warnings: 2017-08-12T11:33:06.761+0000 I STORAGE [initandlisten] 2017-08-12T11:33:06.761+0000 I STORAGE [initandlisten] ** WARNING: Using the XFS filesystem is strongly recommended with the WiredTiger storage engine 2017-08-12T11:33:06.761+0000 I STORAGE [initandlisten] ** See http://dochub.mongodb.org/core/prodnotes-filesystem > show dbs admin 0.000GB local 0.000GB > use test switched to db test > db.bloguser.insert({"name":"user1"}) WriteResult({ "nInserted" : 1 }) > db.bloguser.findOne() { "_id" : ObjectId("598f061d0a82edd3b0d0ef11"), "name" : "user1" } >
If up to here everything is working for you then congrats! I will move to the next step.
When we installed EC2 instance, we allowed access only on the TCP port 22 for SSH. Now when we want to connect our remote MongoDB instance from our local machine we need to open MongoDB port 27017 on the server. So let’s do it.
Go to your AWS console, select the security group which you created while creating EC2 instance. Right click on it and click on the “Edit Inbound rules” your screen will look like below.
Here to add a new rule, select type as a “Custom TCP”, protocol as “TCP”, port number as “27017” and in source field select “Anywhere”.
Your screen will look like the below. Save the rule.
Now we are ready to connect our remote server from the local machine.
Open command prompt on your local machine. Go to your MongoDB installation path and type below command.
mongo ec2-13-126-191-187.ap-south-1.compute.amazonaws.com:27017/admin -u root -p mypassword
mongo "your hostname":27017/admin -u root -p
Your screen output will look like below
Till here we have learned, how to connect to a remote MongoDB server, which is installed on AWS services using your local command prompt also called as “Mongo shell”.
In all our previous examples we connected to MongoDB using command prompt, but there is an easier way to connect MongoDB, explore the MongoDB data and run hoc queries on MongoDB data using MongoDB tool name “Compass”.
Connect Server using MongoDB Compass
As the GUI for MongoDB, MongoDB Compass allows you to make smarter decisions about indexing, document validation, and more. Currently Compass is available as part of the MongoDB Professional and MongoDB Enterprise Advanced subscriptions. Download Compass here.
Download your installation file and execute it. Once you open the compass program it will look like below.
Type your hostname, select authentication username/password, type your username as “root”, enter your password and then click on connect. Your screen will look like below.
MongoDB compass is a whole new experience of MongoDB. Here you will able to see all your databases, their collections and you will able to dig into your collections data.
Import Local Database File on Remote MongoDB Server
Most of the time when we the create database we want to import existing data. In MongoDB to import existing data file, “mongoimport” utility is available.
In this example, I am going to import JSON data file on my remote server. Below is a command which I will type on my command prompt.
mongoimport -h ec2-35-154-149-108.ap-south-1.compute.amazonaws.com --port 27017 -d test -c movieDetails -u root -p mypassword --type JSON C:\data\mangodb_tuto\loadMovieDetailsDataset.json --jsonArray --authenticationDatabase "admin"
In the above command I am using “loadMovieDetailsDataset.json” which has 2295 movies records. I imported this file to my remote test database with collection name “moviedetails”.
Below is the output on my screen.
Now lets see how MongoDB compass shows this data. You can see below screen how beautifully MongoDB compass is showing my data schema. You can download my JSON data file to play more with mongoDB database.
Till now in this article we learn how to Install Mongodb on remote server and how to connect it. Also, we learn new GUI tool MongoDB compass. Now it’s time to look at MongoDB Atlas.
What is MongoDB Atlas?
MongoDB Atlas is database as a service platform provided by MongoDB. Here you will install MongoDB instance with few clicks and you don’t need to do much administrative work.
You can register on the Mongodb Atlas site without your credit card details. Select service type “MO” which is free with shared RAM and 512 MB storage, which is perfect for beginner and not less for startups who want to develop their MVP.
Once you sign up on the MongoDB Atlas site, you will navigate to page where it will ask you to “Choose a New Group Name”, type your group name in the textbox.
Now on the next screen, you will get the option to select your cluster name, cloud provider name, region and instance size. Cluster name will appear in the your instance hostname url so select your cluster name accordingly.
I selected instance size M0 which is free. At the bottom of the page you will get the option to select a username and password. Because I selected M0 plan I am getting option to select only 3 nodes by default. For other plans you can select more than 3 nodes. Once you click on the “Confirm & Deploy”, it will install new instance and will navigate to dashboard which will look like below.
On the dashboard screen you will see the server status and other security details. To connect this instance from our local machine, click on the connect button on your dashboard. You will get a pop-up screen like below where you will see different option to connect to this instance.
In our example, we want to connect through the Mongo shell so click on the Mongo shell option on-screen you will get connection string like below.
mongo "mongodb://yogeshtutorial-shard-00-00-ne8em.mongodb.net:27017,yogeshtutorial-shard-00-01-ne8em.mongodb.net:27017,yogeshtutorial-shard-00-02-ne8em.mongodb.net:27017/test?replicaSet=yogeshtutorial-shard-0" --authenticationDatabase admin --ssl --username yogesh --password <PASSWORD>
Type this connection string as it is on your Mongo shell and replace password text with your password. You will be able to connect to your cloud MongoDB instance.
I will stop here. In this article we learn how to install MongoDB on the remote cloud server, how to connect remote server with Mongo shell and using MongoDB compass. Also we learn how to import local data on the remote server. In my next article I will write about how to use MongoDB in PHP and .NET applications.
Thank you for detail article. I tried steps on EC2 as you said in this article. they are working perfect. But how to check log of mongodb on EC2 server?