Securing MongoDB

By default, MongoDB is not safe; people can write, read, modify or delete data on your server, without the need for authorization in any case. Protecting the database is not a complicated procedure; it is performed in just a few steps.  

Start your Mongo client first. On Linux, this is the mongo command. Enter this block of text, of course, changing the parts of the placeholder to your own information.

The protection procedure begins with the fact that you need to start your Mongo client. On Linux, this is the mongo command. Enter this block of text, of course, changing the parts of the placeholder to your own information.

db.createUser({
  user: "USERNAME", 
  pwd: "PASSWORD", 
  roles: [
    {
      role: "readWrite",
      db: "YOUR_DATABASE"
    }
  ]
});

Next, you need to exit the mongo client and edit the MongoDB configuration file. Depending on your OS and distribution, you will find it in one of these places.

/etc/mongodb.conf
/etc/mongod.conf

Change the following #security: line to the following:

security:
  authorization: enabled

You should consider changing the bind port to localhost (127.0.0.1) or bind it to a private IP address that is not available on the Internet.

# network interfaces
net:
  port: 27017
  bindIp: 634.234.102.6

You should always watch your spaces! Always two by one, never tabs. After that, restart the MongoDB database.

systemctl restart mongod
systemctl restart mongodb

Was this article helpful?

Related Articles

Leave A Comment?