Popular Posts

Sunday 26 November 2017

How to use Mongo Atlas with SpringBoot and Mongo Shell


Objective :  In this post we will learn how to use Mongo Atlas with SprigBoot and Mongo shell.

Problem : When writing web application or desktop application one thing we certainly use is Database. 
But running database all the time on the local machine eats RAM continuously and slows down the machine. Being a programmer we always try to make our machine/laptop faster to use. 

So is there any way that we need not to run database on the local machine yet we can use it?

YES, THERE IS ALWAYS A WAY. 

I often use MongoDB and I love it, so tried to find out some free MongoDB hosting. I found MongoDB Atlas which gives you 512MB free hosting and real time stats. 

So let's start. 

  1. Go to MongoDB Atlas and select Get Started Free, it will take you to a form. Fill the form, tick  T&C and select Get Started Free.
After processing it will show you form like below :- 


  • Enter Cluster Name whatever you want but remember you cannot change it once saved. 
  • Cloud provider choose what you like from drop down, I choose default provided.
  • Region choose what you like from drop down, I choose default provided.
  • Instance size : M0 (which is free 512MB cloud storage)
  • Admin Username & Password : Enter these credentials and remember for future use.
  • Now  select  the big green button Confirm and Deploy.
Now let the Atlas configure for the first time. 


 When the MongoDB Atlas is configured it will give you some stats like below :



  • click Connect; it will show you a red box saying you don't have any IP address configured which will you use to connect from. So simply select Add Current IP Address and it will add your machine's IP address as trusted IP address. After some time it will show you that IP is active and ready to connect.


Note : You can only connect from IP addresses you have configured here.


  1. Let's first connect our MongoDB Atlas cloud from Monogo Shell
Note : To connect from Mongo Shell you first must have installed Mongo shell on your machine. 

  • Select Connect with the Mongo Shell and will show you a URL, copy that.
  • Open a CMD and paste the copied URL here. Now as mentioned in the URL replace <PASSWORD> with the password you entered at the time of cluster creation. 

As we can see in the CMD the version of installed mongo shelll and below that we can see that it is connected to our newly deployed MongoDB Atlas cloud... Wooohhh hooooo.

Now we can use our Atlas MongoDB cloud. 


Now let's connect the our MongoDB cloud from SpringBoot. 

  • Select Connect Your Application option and it  will show a URL copy that.
  • Open application.properties file of the project in which you want to use this MongoDB cloud.
spring.data.mongodb.uri=<paste the copied URL here and replace <PASSWORD> with actual passwrord>
Note : When you are connecting Atlast cloud from SpringBoot application your password must be URL ENCODED else spring will generate a warning for the same and stop the start of application.
To encode your password simply go 




Generate a secure password and use it.


spring.data.mongodb.database=<DataBase Name>
Note : When you give Database Name and if the DB does not exist already it will create it for you. 
Note : If you don't provide any database name by default it will create and select test 

Now run your SpringBoot application.

Below are my SpringBoot logs :- 


  INFO  [restartedMain] o.m.d.cluster - Cluster created with settings {hosts=[cluster0-shard-00-00-x3s7a.mongodb.net:27017, cluster0-shard-00-01-x3s7a.mongodb.net:27017, cluster0-shard-00-02-x3s7a.mongodb.net:27017], mode=MULTIPLE, requiredClusterType=REPLICA_SET, serverSelectionTimeout='30000 ms', maxWaitQueueSize=500, requiredReplicaSetName='Cluster0-shard-0'}
  INFO  [restartedMain] o.m.d.cluster - Adding discovered server cluster0-shard-00-00-x3s7a.mongodb.net:27017 to client view of cluster
  INFO  [restartedMain] o.m.d.cluster - Adding discovered server cluster0-shard-00-01-x3s7a.mongodb.net:27017 to client view of cluster
  INFO  [restartedMain] o.m.d.cluster - Adding discovered server cluster0-shard-00-02-x3s7a.mongodb.net:27017 to client view of cluster
  INFO  [restartedMain] o.m.d.cluster - No server chosen by WritableServerSelector from cluster description ClusterDescription{type=REPLICA_SET, connectionMode=MULTIPLE, serverDescriptions=[ServerDescription{address=cluster0-shard-00-00-x3s7a.mongodb.net:27017, type=UNKNOWN, state=CONNECTING}, ServerDescription{address=cluster0-shard-00-01-x3s7a.mongodb.net:27017, type=UNKNOWN, state=CONNECTING}, ServerDescription{address=cluster0-shard-00-02-x3s7a.mongodb.net:27017, type=UNKNOWN, state=CONNECTING}]}. Waiting for 30000 ms before timing out
  INFO  [cluster-ClusterId{value='5a1a70aefb750d12442936cd', description='null'}-cluster0-shard-00-01-x3s7a.mongodb.net:27017] o.m.d.cluster - Monitor thread successfully connected to server with description ServerDescription{address=cluster0-shard-00-01-x3s7a.mongodb.net:27017, type=REPLICA_SET_SECONDARY, state=CONNECTED, ok=true,
  INFO  [restartedMain] o.m.d.connection - Opened connection [connectionId{localValue:4, serverValue:13788}] to cluster0-shard-00-00-x3s7a.mongodb.net:27017
 ]
  INFO  [restartedMain] o.s.j.e.a.AnnotationMBeanExporter - Registering beans for JMX exposure on startup
  INFO  [restartedMain] o.s.b.c.e.t.TomcatEmbeddedServletContainer - Tomcat started on port(s): 8080 (http)
  INFO  [restartedMain] c.d.MongoWithBootApplication - Started MongoWithBootApplication in 19.803 seconds (JVM running for 20.842)
  

As we can see in above logs SpringBoot is connected to our MongoDB cloud and ready to use.
Booommmm!!!













No comments:

Post a Comment