ANS Documentation

Improve This Doc
  • Cloud
  • Domains and DNS management
  • Backup and High Availability
  • eCommerce Stacks
  • Security
  • Email
  • Monitoring and usage management
  • Networking
  • Operating systems
    • Linux
      • Basics
      • Apache
      • Control Panels
      • FTP
      • Magento
      • Miscellaneous
      • MySQL
      • NGINX
      • PHP
      • PHP-FPM
      • Setting up Websites
      • NFS
      • VPN
      • Percona
      • SSH
      • SSL
      • Git
      • Redis
      • Elasticsearch
      • MongoDB
      • Ncdu
    • VMware ESXi
    • Windows
  • Webcelerator
  • MyUKFast
  • Home >
  • Operating systems >
  • Linux >
  • MongoDB >
  • How to Secure MongoDB on CentOS 7

How to Secure MongoDB on CentOS 7¶

MongoDB allows you to restrict database actions by specifying roles for users. The default installation does not include an admin user, so in this tutorial we will create one.

Creating an Admin User¶

To start, you will need to enable access control on your MongoDB instance.

  • Ensure your MongoDB is not currently running

[root@ ~]# systemctl status mongod
● mongod.service - MongoDB Database Server
   Loaded: loaded (/usr/lib/systemd/system/mongod.service; enabled; vendor preset: disabled)
   Active: inactive (dead) since Mon 2020-09-14 16:31:20 BST; 6s ago
     Docs: https://docs.mongodb.org/manual
  • Start up an instance of MongoDB with no authentication.

mongod --port 27017 --dbpath /var/lib/mongo
  • Create your admin user with a strong password, and then exit.

use admin
db.createUser(
  {
    user: 'admin',
    pwd: 'Some1ncrediblystrongpassword!',
    roles: [ { role: 'root', db: 'admin' } ]
  }
)

db.adminCommand( { shutdown: 1 } )
  • As a sudo user, edit the MongoDB configuration file to specify that authentication is to be enabled. Un-comment the security directive and amend as below.

[root@ ~]# vi /etc/mongod.conf
...
security:
  authorization: "enabled"
  • Ensure the directory is owned by mongod

[root@ ~]# chown -R mongod: /var/lib/mongo
  • Start the service

[root@ ~]# systemctl start mongod
  • Test your new admin user

[root@~]# mongo -u admin -p --authenticationDatabase admin
...
> show dbs
admin    0.000GB
config   0.000GB
local    0.000GB

Next Article > How to Perform Common Administrative Tasks in MongoDB

  • Useful Links
  • SMB
  • Enterprise
  • Channel
  • Public Sector
  • ANS Data Centres
  • About ANS
  • Careers
  • Blog
  • Get in touch
  •  
  • Sales 0800 458 4545
  • Support 0800 230 0032
  • Get in touch

© ANS Group Limited | Terms and Conditions | Corporate Guidance | Sitemap
ANS Group Limited, registered in England and Wales, company registration number 03176761, registered office 1 Archway, Birley Fields, Manchester M15 5QJ