Tuesday, October 21, 2014

Mongodb Import and Export database -MONGODB

 Mongodb Import and Export database -MONGODB

This post is regarding how to import and export mongodb data dump. 
To export the database we need to run  mongodump.exe.
To import the database dump we need to run mongorestore.exe.
Consider and example Customer database need to be exported and imported


WINDOWS: 

     Export Dump:
           step 1: Make sure that mongodb is running in your machine if its not running
                      go to mongodb\bin folder in your mongodb installation and run mongod.exe file 
          step 2: Run mongodump.exe file which will export all schema present in db to data folder
                     inside  bin directory of mongodb installation
          Step 3: If you want to export a particular schema of your db to a desired location in your machine
                     then press windows+r  type cmd and then Enter button your command prompt will be                          opened Go to mongo installation bin directory 
          Command : mongodump.exe -d customer --out C:\

          This will export database customer to C:\ in your machine
    Restore Dump:
         command: mongorestore.exe -d customer C:\\customer
            This will import database customer to mongodb

UBUNTU :
      Export Dump:
           step 1: Make sure that mongodb is running in your machine if its not running
                      run command sudo service mongodb start and the run the below command to 
                      export the dump
    
          Command : mongodump -d customer --out /home

        This will export database customer to /home in your machine
    Restore Dump:
         command: mongorestore -d customer /home/customer
         This will import database customer to mongodb

Friday, October 17, 2014

How To Connect to a Remote MongoDB Server

How To Connect to a Remote MongoDB Server with MongoHub for Mac

There are three parts to this process: Changing the MongoDB Config to accept connections from remote hosts, Allowing port 27017 through your Firewall, and Configuring MongoHub.


Written: March 16, 2012
Applies to: Ubuntu 11.10, Mac OS X Lion
Here’s what you need to get started:
  1. Install MongoDB on your Server. There are a million tutorials out there, but here’s the one I used at mongodb.org.
    If you installed with apt-get install mongodb instead of  apt-get install mongodb-10gen here’s how to UNINSTALL the older version after shutting down gracefully so you don’t wind up with an infuriating mongod.lock file.

    Then go ahead and install the mongodb-10gen (latest stable) version.
  2. Download and install MongoHub for Mac OS X.

Secure MongoDB.

  1. Start the shell and add an administrative user for the server.

    Note: On my system, typing ./mongo doesn’t work, but plain mongo does.
  2. Authenticate, still in the shell.
  3. Configure a specific user for another database.
  4. Exit the MongoDB shell.
  5. Edit the mongodb.conf file to enable authentication.

  6. Uncomment this line.
  7. Restart MongoDB

Open up Port 27017 on your Firewall

This will be different for everyone. As an alternative, you can run MongoDB on a different port, maybe something that’s already open on your firewall. You can change the port by editing the mongodb.conf file and restarting the server.

Configure MongoHub

  1. Fire up MongoHub.
  2. Click the plus icon in the lower left to add a database.
  3. Fill out the information about your database.

That’s it! You can now browse your secure database!

Thursday, October 9, 2014

MongoDB security and user access control

MongoDB does not offer as fine grained a security model as traditional databases. Never the less what it provides is sufficient as long as best practices are followed and some care is taken in securing the system.
MongoDB offers two levels of access control – system (global) and per database. For each of these types, users can be assigned a series of roles which determine the actions they can perform. The model is pretty simple and straight forward and allows for a least-privilege approach. The only shortcoming (at the time of writing) is that the roles are a little broad. In the future I would really like to see the ability of setting more fine grained restrictions on actions or even the ability to create your own role groups with action rules.

Enabling authentication

The first thing to do before you can start making use of mongo’s UAC features it to enable authentication. Usually this is disabled by default.
This can be enabled either by editing your mongodb.conf file and adding the line
auth = true

You can also start mongod with the auth parameter.

Creating an admin user

Out of the box mongo has no admin user set. Hence once you enable authentication you will be able to gain privileged access via a local mongo shell until an admin user has been created.
Start the shell and type:
use admin
db.addUser({ user:"admin", pwd:"secretpassword", roles:["dbAdminAnyDatabase","clusterAdmin"]})

This will create a user admin with the password secretpassword and grant that user dbAdminAnyDatabase and clusterAdmin roles. I recommend granting the primary admin user both of these roles as they will enable you to perform any action you want on any database.
Note: If you do not set clusterAdmin you will get an unauthorised error when you try to drop a database.
Now that you have an admin account enabled you will be required to authenticate before you can perform any actions.

Creating a database user

You can now add a user to any database and set access restrictions.
First select the database you wish to add a user to.
use example
We are now using the example database. Adding a user is exactly the same to how we created an admin user:
db.addUser({ user:"rwUser", pwd:"password", roles:["readWrite"] })
This will create a user rwUser which will have read/write access to the current database.

Authenticating

Before you can perform an action either globally or within a given database you will need to authenticate.
You can authenticate as a global user or a database user by selecting the appropriate database. Then you can authenticate by typing:
db.auth("rwUser","password")
You will get a response code of 1 for success and 0 for fail. Once authenticated you will be able to execute any action your roles allow you.

User roles

At the time of writing Mongo (2.4) has the following roles:

Database User Roles

  • read
  • readWrite
  • dbAdmin
  • userAdmin

System User Roles

  • clusterAdmin
  • readAnyDatabase
  • readWriteAnyDatabase
  • userAdminAnyDatabase
  • dbAdminAnyDatabase
For detailed information regarding each role please refer to User Privilege Roles in MongoDB in the MongoDB Official Documentation.

Remove a user

You can remove both database and system users in the same way. First select either a specific logical database or the system admin database. Then type:
db.removeUser("testuser")
Which will remove the user testuser.
Note: You technically should not be able to remove the admin user…

Changing passwords

If you wish to change a user password select either a specific logical database or the system admin database. Then type:
db.changeUserPassword("testuser", "newpassword")
Which will update the user testuser with the new password newpassword.

Updating users

If you wish to update an existing user – for instance to change their username or update their roles, you can do so by performing an update against the system.users collection of either either a specific logical database or the system admin database. This works exactly like a standard update() and the same rules and parameters apply.
Here are some typical update scenarios you might wish to perform:

Update a username

use example
db.system.users.update({ user:"testUser" }, { $set:{ user:"tastyUser" } })

Add an additional role to a user

use example
db.system.users.update({ user:"testUser" }, { $push:{ roles:"userAdmin" } })

Remove a user role

use example
db.system.users.update({ user:"testUser" }, { $pull:{ roles:"dbAdmin" } })