Follow us

MongoDB Setup with AWS Linux EC2

 

Use case : Launch ec2 instance, download putty convert .pem file(which is your private key file provided by ec2 at last stage of ec2 launch to connect with instance) to .ppk file(Putty support to connect with instance) via Putty generator and with Putty connect to your Linux instance by using generated .ppk file and then install mongo db and work with mongo db.

 

Here are all the steps-

  1. Launch EC2 instance( Amazon Linux 2 - 64 bit).
  2. Select your instance and from the action option, select connect and download the key pair (.pem) file.
  3. Make sure your selected security group has inbound rule for port "22" which is SSH port to connect with linux machine using Putty.

     

  1. Now to connect with your launched Linux machine, you need putty. So download the Putty https://putty.org/  - download putty from here like (putty-64bit-0.73-installer.msi) and install to your laptop to connect with Linux machine.
  2. On successful installation it will give you two app one is Putty and another one is Putty Gen.
  3. Open Putty Gen from your laptop window search to generate (.ppk) file to connect with Linux instance. Your downloaded .pem file does not supported by Putty, hence you need to convert your .pem file into .ppk.

            Click on load, select your .pem and save private file.

       

       

 

  1. Login to aws console and copy your public dns or public ip of you ec2 linux machine.

     

  1. Now open Putty app from your laptop window search put copied public dns and also put your converted .ppk file and open. It will open putty cmd and there it will ask you login as :, so put there as "ec2-user" and it will connect to your linux instance.

     

       

      

  1. Now run below command one by one for different purpose -

More details about installation (https://docs.mongodb.com/manual/tutorial/install-mongodb-on-amazon/)

  • sudo su   = The sudo su command allows you to run programs as another user, by default the root user
  • sudo yum update -y   = This command will update all the pending patch updates on you ec2
  • sudo vi /etc/yum.repos.d/mongodb-org-4.2.repo  : Create file so that you can install MongoDB directly using yum, it will give you option to write content, so copy below content and paste it-

 

           [mongodb-org-4.2]

           name=MongoDB Repository

           baseurl=https://repo.mongodb.org/yum/amazon/2/mongodb-org/4.2/x86_64/

           gpgcheck=1

           enabled=1

           gpgkey=https://www.mongodb.org/static/pgp/server-4.2.asc

 

  • :w   = This command will write the above content to created file
  • :quit  = This command will close the open file created above.
  • sudo yum install -y mongodb-org   = This will install mongo db to your ec2. In case there is some error and you want to skip those error then you can use --skip-borken like complete installation command is "sudo yum install -y mongodb-org --skip-broken". Similarly alternatively, to install a specific release of MongoDB "sudo yum install -y mongodb-org-4.2.6 mongodb-org-server-4.2.6 mongodb-org-shell-4.2.6 mongodb-org-mongos-4.2.6 mongodb-org-tools-4.2.6"
  • sudo service mongod start  = This command will start your installed mongo db service.
  • sudo chkconfig mongod on  = This command will on your mongo db and respective settings.
  • sudo vi /etc/mongod.conf   = This command will open your mongo db config file, so that you can change the respective config like if you want to access your mongo db outside the local machine(your linux ec2) then change net bind ip to 0.0.0.0 and also here you can change the default mongo port 27017

     

  • Now again ruse ":w" and ":quit" command to save and close above config file.
  • sudo service mongod restart   = This command will restart the mongo db service, so that the changes you made like port change or public ip will take effect immediately.
  • clear   = This command will clear/clean your screen, similar to cls on cmd command.
  • mongo  = This command will connect to your mongo db on local ip, so you are already connected with your instance via Putty and it will open Mongo on localhost.
  • db  = Use this command to see which mongo db you are in/using.
  • Use below command to create admin user with read/write access on mongo.

        More you can find at https://docs.mongodb.com/manual/reference/method/db.createUser/

        db.createUser( {

        user: "TestUser1",

        pwd: passwordPrompt(),  // Or  "<cleartext password>"

        customData: { employeeId: 12345 },

        roles: [ { role: "clusterAdmin", db: "admin" }]

        })

  • db.getUsers( { showCredentials: true } )    = This command will give you all the available users of mongo db.
  • cls  = This command will clear/clean your command prompt (this will work when you are connected with mongodb).
  • exit  =This command will exist from your logged in mongo instance or even putty user.
  1. Now to connect with mongo db from any mongo client like robo 3t or nosql booster or even other client like . You can use the created user credential and the ec2 public ip or dns.
  2. You can also use other mongo clients like win scp, cygwin etc.. and there you can use ssh commands and even to connect with ec2 you can use the command like "ssh -i LinuxKeyPair.pem ec2-user@[linux-ec2-public-ip]".
  3. If you are using mongo cmd client then you can use below command to connect with -

mongo --username User2 --password 123321 --host 3.14.255.99 --port 27017

 

FAQ:-

  1. During mongo db installation if you get error something "Error: Package: mongodb-org-shell-4.2.1-1.amzn2.x86_64 (mongodb-org-4.2) Requires: libc.so.6(GLIBC_2.18)(64bit)" then may be below URL will help you to resolve.

           https://forums.aws.amazon.com/thread.jspa?messageID=924703

  1. Yum upgrades the packages when a newer version becomes available. To prevent unintended upgrades, pin the package. To pin a package, add the following exclude directive to your /etc/yum.conf file:

           exclude=mongodb-org,mongodb-org-server,mongodb-org-shell,mongodb-org-mongos,mongodb-org-tools

  1. To delete file from Linux machine using putty, connect machine using Putty and run below command where "mongodb-org-4.2.repo" is your file name under "/etc/yum.repos.d" directory.

          rm -f /etc/yum.repos.d/mongodb-org-4.2.repo

Categories/Tags: mongodb~linux

Recent Articles

1

AWS Saving Plan - Cost optimization tips

2
3

AWS RDS Key Concepts & Why you should use it?

4
5

Open-Search/Kibana - Multi Tenancy Setup

See All Articles