Open In App

Connect MongoDB (AWS) from Local Machine using WinSCP and MongoDB Compass

Last Updated : 01 Mar, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

Pre-requisite: AWS and MongoDB

In this article, we are going to connect to the Mongo database of the AWS Ubuntu Server instance using WinSCP and learn how to get connected to the server from your local machine using MongoDB Compass. 

If you haven’t installed a MongoDB server in your AWS EC2 instance then follow the steps present in this article and create a mongo dB server in your AWS account. Before launching the AWS instance download the key pair of the instance for future authentication purposes. If you haven’t created one, go to services and search for Key pair and create a Key pair and download the .ppk file.

creating a key-pair

 

Download and Keep the file in a secure place because it is a one-time downloadable file. This file can be used as a secure password file to connect to the server.

Tools Required:

WinSCP:

WinSCP is a free and open-source SFTP, FTP, WebDAV, and SCP client for Microsoft Windows. It allows users to transfer files between a local and a remote computer using various protocols, such as FTP, FTPS, SCP, SFTP, or WebDAV.

WinSCP is a popular choice for transferring files between a local computer and a remote server, especially when the server is running the Linux operating system. It has a user-friendly graphical interface and supports various advanced features, such as file synchronization and script execution.

In addition to file transfer, WinSCP can also be used to edit files directly on the remote server, or to browse and manage the file system on the server. It also includes a built-in text editor that allows users to edit files directly on the server without the need to download and upload them.

WinSCP is available as a standalone application and as a plugin for other applications, such as Microsoft Visual Studio. It is licensed under the GNU General Public License.

Connecting To Server:

Step 1: Installing WinSCP to get connected to the files present in the server we will be making use of WinSCP.  We can transfer files and data to our server using WinSCP.

Step 2: Create a new Session in WinSCP for this open WinSCP and click on create a new session with protocol as SFTP. In the Hostname field get the Public IPv4 address of your AWS Instance and paste it there. The Port number will be 22 by default. In the User name field enter the username of your choice. Leave the password empty.

creating a new session at WinSCP

 

Now browse to advanced settings and go to authentication and upload the .ppk file which we downloaded earlier while creating Key Pair in AWS and login into the system.

uploading a pvt key pair file

 

Now our WinSCP is connected to our AWS Server.

Connect to the Server Database Using MongoDB Compass:

Step 1: Download and Install MongoDB compass. Select the latest version and Download the MSI file instead of the zip file. If we try to connect to our AWS instance from MongoDB compass we will get a timeout error because before accessing the instance we need to make the server globally available.

Step 2: Open WinSCP and click on “Open Session in PuTTY“. Now a PuTTY terminal will be opened. Set Mongo to access globally. Use the command below and modify the file /etc/mongod.conf details as below.

$ sudo vim /etc/mongod.conf

net:
port: 27017
bindIp: 0.0.0.0 

file configuration

 

Step 3: Now restart the server using the below command

$ sudo service mongod restart

Step 4: Now go to the amazon instance and go to security details in AWS and click on Edit Inbound rules and one more rule for MongoDB port. Add an inbound rule with the following configurations.

  • Type: All TCP
  • Port range: 0 – 65535 (Because mongodb uses 27017 port)
  • Source: Anywhere IPv4

inbound rules

Step 5: Now open Mongodb compass and click on the new connection and edit the connection string as follows “mongodb://Your_IPV4_IPAddress:27017/” and click on connect. Now you can access your AWS MongoDB server from your local machine.

connecting AWS from local machine


Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads