Create a secure database in mongodb

I want to create a database in mongodb that will be protected.

Secure means that the application must pass the username / password to connect to my database in mongodb.

+7
source share
2 answers

From Mongo Java Tutorial

MongoDB can be run in safe mode when access to databases is controlled by authentication by name and password. When launched in this mode, any client application must specify a name and password before performing any operations. In the Java driver, you simply do the following with the mongo object attached:

boolean auth = db.authenticate(myUserName, myPassword); 

If the username and password are valid for the database, auth will be true. Otherwise, it will be wrong. You should look at the MongoDB log for more information, if any.

Most users run MongoDB without authentication in a trusted environment.


Configure Authentication and Security

Authentication is stored in each system.users database. For example, in the projectx project, the project projectx.system.users will contain information about the user.

First we need to configure an admin user for the entire db server process. This user is stored in a special admin database.

If no users are configured in admin.system.users, you can access the database from the localhost interface without authentication. Thus, from the server on which the database is running (and therefore on localhost), start the database shell and configure the administrative user:

 $ ./mongo > use admin > db.addUser("theadmin", "anadminpassword") 

Now the user has a database administrator. Please note that if we have not been authenticated yet, we must do this if we want to perform further operations, since there is a user in admin.system.users.

 > db.auth("theadmin", "anadminpassword") 

We can view existing users for the database using the command:

 > db.system.users.find() 

Now configure the "regular" user for a different database.

 > use projectx > db.addUser("joe", "passwordForJoe") 

Finally, add a read-only user. (only supported in 1.3.2 +)

 > use projectx > db.addUser("guest", "passwordForGuest", true) 
+18
source
  • Create an admin user for the mongo instance,

> use admin

> db.addUser("admin", "xyzxyz")

  • Switch to db that requires authentication

> use newdb

> db.addUser("newuser", "strongpwd")

  • Stop the mongo instance / service. If mongodb was installed via ppa , it is configured as a service.

sudo service mongodb stop

If it was installed from a source, stop the process using:

/etc/init.d/mongodb stop

  • Modify the configuration file to use default authentication.

vim /etc/mongodb.conf

auth = true

  • Run mongodb. If this is a service

sudo service mongodb restart

still

mongod --config /etc/mongodb.conf

  • Check if auth is enabled:

> show collections on newdb should give an error

 "$err" : "not authorized for query on newdb.system.namespaces", "code" : 16550 

and should work after

> db.auth("newuser", "strongpwd")

Now db newdb is protected.

+3
source

All Articles