RHEL7: How to install a MongoDB service.
Presentation of MongoDB
MongoDB Server is an open-source, document database designed for ease of development and scaling.
Prerequisites
As the default MongoDB installation uses the /var/lib/mongo directory to store your databases, keep in mind that the partition or logical volume associated with /var needs adequate space.
Installation Procedure
To install, apply the following steps:
Create a file called /etc/yum.repos.d/mongodb-org-3.2.repo and paste the lines below into:
[mongodb-org-3.2]
name=MongoDB Repository
baseurl=https://repo.mongodb.org/yum/redhat/$releasever/mongodb-org/3.2/x86_64/
gpgcheck=1
enabled=1
gpgkey=https://www.mongodb.org/static/pgp/server-3.2.asc
Then, install the MongoDB packages:
# yum install -y mongodb-org
Note: To prevent unintended upgrades, the official MongoDB documentation advises to pin the various packages by adding the following directive in the /etc/yum.conf file:
exclude=mongodb-org,mongodb-org-server,mongodb-org-shell,mongodb-org-mongos,mongodb-org-tools
Configuration Procedure
Limit Change
The number of processes available for MongoDBin the system is not enough, you need to change the default settings by editing the /etc/security/limits.d/20-nproc.conf file and paste the following lines at the end (source: MongoDB website):
mongod soft nproc 64000
mongod hard nproc 64000
mongod soft nofile 64000
mongod hard nofile 64000
Transparent Hugepages Deactivation
To get better performances, it is also recommended to disable transparent hugepages by creating a tuned profile (it is perfectly possible to run MongoDB without the tuned daemon but, in this case, to get good performances will require much more efforts from you) (source: MongoDB website) (if you use an other method, be careful about this bug).
Create the /etc/tuned/no-thp directory:
# mkdir /etc/tuned/no-thp
Create the /etc/tuned/no-thp/tuned.conf file and paste the following lines into:
[main]
include=virtual-guest
[vm]
transparent_hugepages=never
Note1: virtual-guest is supposed to be your current active profile, change it if needed. Note2: Additional tips are available here.
Enable the new profile:
# tuned-adm profile no-thp
Check the configuration:
# cat /sys/kernel/mm/transparent_hugepage/enabled
always madvise [never]
Start and enable at boot time the MongoDB service:
# systemctl start mongod && systemctl enable mongod
Check that MongoDB is correctly working:
# tail /var/log/mongodb/mongod.log | grep "waiting for connections"
2016-12-06T23:02:59.853+0100 I NETWORK [initandlisten] waiting for connections on port 27017
Access From Outside
Note: By default, the MongoDB service only listens to the local network interface (127.0.0.1). To be able to interact with any other server, comment the BindIP directive in the /etc/mongod.conf configuration file and update the Firewalld configuration with:
# firewall-cmd --permanent --add-port=27017/tcp
# firewall-cmd --reload
Authentication Mode
Then, you can create the root user (you could call it differently) for the mydbdatabase:
# mongo 127.0.0.1/mydb
MongoDB shell version: 3.2.11
connecting to: 127.0.0.1/mydb
...
> db.createUser({ user: "root", pwd: "myPassword", roles: [{role: "root", db: "admin"}] })
Successfully added user: {
"user" : "root",
"roles" : [
{
"role" : "root",
"db" : "admin"
}
]
}
> exit
bye
To enable the authentication mode, paste the following lines into the /etc/mongod.conf file:
security:
authorization: enabled
Restart the MongoDB service:
# systemctl restart mongod
Test the new authentication mechanism:
# mongo mydb --username root --password myPassword --authenticationDatabase mydb
MongoDB shell version: 3.2.11
connecting to: mydb
> exit
bye
Source: MongoDB website.
Advanced Optimizations
Besides the standard configuration previously described, they are additional steps to follow to get the best performances.
Kernel Configuration
Edit the /etc/tuned/no-thp/tuned.conf file and paste the following lines:
[sysctl]
vm.swappiness = 1 # almost no swap to disk (0=never swap, 100=always swap)
zone_reclaim_mode = 0 # no memory reclaimed (normally the default value)
Note: According to Percona, assigning 0 to the wm.swappiness kernel parameter doesn’t bring the expected behavior.
In case you are dealing with large-memory (64GB+) database servers, you can also add these recommended settings:
[sysctl]
vm.dirty_ratio = 15 # percentage of total system memory that can hold dirty pages
vm.dirty_background_ratio = 5 # threshold to start flushing dirty pages to disk in the background
Concerning the network side, Percona (see links below) advises to add the following configuration:
[sysctl]
net.core.somaxconn = 4096
net.ipv4.tcp_fin_timeout = 30
net.ipv4.tcp_keepalive_intvl = 30
net.ipv4.tcp_keepalive_time = 120
net.ipv4.tcp_max_syn_backlog = 4096
IO Scheduler and Read-Ahead Configuration
The IO Scheduler defines how disk Input/Output are managed (see tutorial).
The deadline IO scheduler should be chosen in all the cases except when dealing with virtual machines where the noop scheduler is the best: it is useless to ask a virtual machine to order its IO when all the work is done at the hypervisor level.
The Read-Ahead setting defines how much data is stored in the cache for each read. This is a way to increase efficiency in case of sequential reads.
As MongoDB reads are not sequential, this ends up wasting cache space.
Percona (see links below) provides a nice way to specify both configurations at the same time.
In the case of a physical server, create the /etc/udev/rules.d/60-sda.rules file and paste the following lines:
# set deadline scheduler and 16kb read-ahead for /dev/sda
ACTION=="add|change", KERNEL=="sda", ATTR{queue/scheduler}="deadline", ATTR{bdi/read_ahead_kb}="16"
Note: You can test the IO scheduler in place with # cat /sys/block/sda/queue/scheduler and the Read-Ahead parameter with # blockdev –getra /dev/sda
Filesystem Configuration
XFS is the best available option when choosing a filesystem, EXT4 being a second-best option.
Also, to avoid access-time metadata updates by default, add the flag noatime to the filesystem mount options in the /etc/fstab file. After a reboot or umount/mount operations with MongoDB stopped, this change will reduce the amount of useless disk IO activity.
Additional Resources
Several websites provide tutorials about installing MongoDB 3.2 on CentOS 7:
- The Digital Ocean website,
- The HowToForge website,
- The Linode website.
The Percona blog provides several very interesting posts about MongoDB performances:
If you are interested in sharding, the HowToForge website explains you How to Deploy a MongoDB Sharded Cluster on CentOS 7.
The Percona blog also offers many interesting articles about MongoDB:
- Schema Design in MongoDB vs Schema Design in MySQL,
- Designing one to many relations – MongoDB vs MySQL,
- Choosing a good sharding key in MongoDB (and MySQL),
- Using Cgroups to Limit MySQL and MongoDB memory usage,
- MongoDB benchmark: sysbench-mongodb IO-bound workload comparison,
- Checkpoint strikes back,
- Find unused indexes on MongoDB and TokuMX,
- MongoDB and Percona TokuMX Security Guidelines,
- RocksDB 101,
- MongoDB 3.2 WiredTiger in iiBench,
- Percona Server for MongoDB storage engines in iiBench insert workload,
- MongoDB revs you up: What storage engine is right for you? (Part 1),
- MongoDB revs you up: What storage engine is right for you? (Part 2),
- MongoDB revs you up: What storage engine is right for you? (Part 3),
- MongoDB revs you up: What storage engine is right for you? (Part 4),
- Setup a MongoDB replica/sharding set in seconds,
- MongoDB, security, and how not to get stung,
- Myth Busting: MongoDB Scalability (it scales!),
- Percona Back to Basics: MongoDB updates,
- Percona How To: Field Names and Document Size in MongoDB,
- Dealing with Jumbo Chunks in MongoDB,
- Benchmark MongoDB with sysbench,
- MongoDB 3.2: elections just got better!
- MongoDB Administration Checklist for MySQL DBAs,
- MongoDB and non-existent collections,
- MongoDB Data Durability,
- MongoDB Consistent Backups,
- Monitoring MongoDB with Nagios,
- Percona Memory Engine for MongoDB,
- Tuning Linux for MongoDB,
- MongoDB point-in-time backups made easy,
- MongoDB Through a MySQL Lens,
- MongoDB Troubleshooting: My Top 5,
- First MongoDB replica-set Configuration for MySQL DBAs,
- MongoDB 3.4: Sharding Improvements,
- MongoDB PIT Backups In Depth,
- MongoDB PIT Backups: Part 2,
- Securing MongoDB Instances,
- MongoDB 3.4 Views,
- Percona MongoDB 3.4 Bundle Release: Percona Server for MongoDB 3.4 Features Explored,
- MongoDB Audit Log: Why and How,
- Migrating MongoDB Away from MMAPv1,
- Percona Server for MongoDB: Dashing New LDAP Authentication Plugin,
- How to Change MongoDB Storage Engines Without Downtime,
- Upgrading to Percona Server for MongoDB 3.4 from Previous Versions,
- Percona-Lab/mongodb_consistent_backup: 1.0 Release Explained,
- MongoDB Authentication and Roles: Creating Your First Personalized Role,
- Last Resort: How to Use a Backup to Start a Secondary Instance for MongoDB,
- When Should I Enable MongoDB Sharding?
- Common MongoDB Topologies,
- Exciting Pre-Cursors to MongoDB 3.6 Transactions,
- MongoDB 3.6 Security Improvements,
- MongoDB 3.6 Sorting Changes and Useful New Features,
- MongoDB 3.6 Aggregation and Array Improvements,
- Using MongoDB 3.6 Change Streams,
- Using the New MongoDB 3.6 Expression Query Operator $expr,
- Mass Upgrade MongoDB Versions: from 2.6 to 3.6,
- The Anatomy of a MongoDB Replica Set,
- Five Tips to Optimize MongoDB,
- MongoDB 3.6 Retryable Writes . . . Retryable Writes,
- Managing MongoDB Bulk Deletes and Inserts with Minimal Impact to Production Environments,
- MongoDB Sharding: Are Chunks Balanced (Part 1)?
The ScaleGrid blog provides many interesting articles about MongoDB:
- Importing data into a MongoDB instance,
- Atomicity, isolation & concurrency in MongoDB,
- Should you enable MongoDB journalling?
- Automatically age out documents from a collection,
- Geo distributed MongoDB replica sets for 100% uptime,
- 10 tips to improve your MongoDB security,
- The three A’s of MongoDB security – Authentication, Authorization & Auditing,
- Understanding durability & write safety in MongoDB,
- Getting started with user management in MongoDB,
- Fast Paging with MongoDB,
- High performance MongoDB clusters on Amazon EC2,
- Three simple steps to improve the security of your MongoDB installation,
- How to Benchmark MongoDB with YCSB?
- MongoDB SSL with self signed certificates in Node.js,
- Enabling Data Compression in MongoDB 3.0,
- Comparing MongoDB Performance on Public Clouds: AWS, Azure & DigitalOcean,
- Index prefix compression in MongoDB 3.0 WiredTiger,
- The case for MongoDB hashed indexes,
- MongoDB Regex, Index & Performance,
- Multikey indexes & index intersection bounds in MongoDB,
- How to recover from a MongoDB rollback?
- Configuring MongoDB-CR authentication as default on MongoDB 3.x,
- Managing long running operations in MongoDB,
- Understanding Mongodb backup options,
- Cassandra vs. MongoDB,
- MongoDB Schema Design: There Is Always A Schema,
- Which is the Best MongoDB GUI? — 2016 Update,
- Understanding MongoDB Client Timeout Options,
- MongoDB Performance: Running MongoDB Aggregations On Secondaries,
- Comparing Cassandra vs. MongoDB,
- MongoDB Performance: Running MongoDB Map-Reduce Operations On Secondaries,
- The Perils of Building Indexes on MongoDB,
- Understanding and Managing Disk Space on your MongoDB Server,
- XFS vs EXT4 – Comparing MongoDB Performance on AWS EC2,
- How to Stop a Runaway Index Build in MongoDB,
- Redis vs. MongoDB: Comparing In-Memory Databases with Percona Memory Engine,
- How to Create Case-Insensitive Indexes in MongoDB,
- MongoDB Basics: Configuring Role-Based Access Control (RBAC),
- How to Find Unused Indexes in MongoDB?
Leave a comment