Wednesday 15 July 2015

Connect To Mongo DB Instance


In my previous blog I have created the instance. If you are not having any instance running then please create one. You can follow my previous blogs for that.

Assuming, you have a server running without authentication mode. So in that case it becomes very easy, we don’t need any password or any user name. There are many ways we can connect to the server.
  •     Using cmd prompt
  •     Using mongo.exe, which is client tool for mongo DB.
  •     Using third party tool like mongochef, Robomongo

Using cmd prompt:
You open cmd prompt and change the directory to bin folder of mongo DB and then type below commands.

Default connection and without authentication:
{mongo} --> Then press enter.



But if you are connecting to a secure server then you should use below command:
{mongo localhost:27017/admin –u sa –p }, Then press enter.
  • –u sa –p are required only when you are connecting server instance in authenticated mode.
  • localhost:27017/admin --> This is the location and port where server is hosted.

Using mongo.exe:
Just double click on this executable to open in default mode.

But if you are connecting a particular server, then use command
{db=connect(“localhost:27017/admin”);}

This will connect to the database and if there is authentication in the server then you should use authentication command   soon after the connections are established as below
{db=connect(“localhost:27017/admin”);}
{db.auth (“user”, “Password”);}




References:

MongoDB Server Instance As Service


From my last blog as i discussed about how can we run an Instance using cmd. As you might have noticed the instance have started but a command prompt get engaged with this and a window remains open always which looks little odd. It’s not good practice to run your server like this because any one  can close server may be by mistake and restarting will have to go through all pain using cmd.


So we can run the instance as windows service, which become easy to manage. You can start, stop and restart easily. Otherwise you have to follow steps and type all info again if you are restarting. Here is the screenshot of service I have created.



Now unlike any other service you can start, stop or restart.


In order to do so, we have to open command prompt again and change the directory to bin folder follow step 2 as mentioned in my previous blog. Now type command mentioned below.


Make sure you have created a directory and log file: C:\data\log\MongoDB.log. You can create according to your choice, I have chosen C:\data\log\MongoDB.log.


{mongod --auth --bind_ip  localhost --logpath  "C:\data\log\MongoDB.log"  --logappend  --dbpath  "C:\data\db"  --port 27017 --serviceName "MongoDB3.0" --serviceDisplayName "MongoDB3.0" --install}


--auth :if you want to run your server in authentication mode, which at this point not required rest is self-explanatory. So without authentication the command would be


{mongod  --bind_ip  localhost --logpath  "C:\data\log\MongoDB.log"  --logappend  --dbpath  "C:\data\db"  --port 27017 --serviceName "MongoDB3.0" --serviceDisplayName "MongoDB3.0" --install}

 

 References:

http://docs.mongodb.org/v2.2/reference/mongod/