发布于 2015-09-14 14:57:50 | 145 次阅读 | 评论: 0 | 来源: 网络整理
This page details system configurations that affect MongoDB, especially in production.
Always run MongoDB in a trusted environment, with network rules that prevent access from all unknown machines, systems, or networks. As with any sensitive system dependent on network access, your MongoDB deployment should only be accessible to specific systems that require access: application servers, monitoring services, and other MongoDB components.
See documents in the 安全 section for additional information, specifically:
If you use the Linux kernel, the MongoDB user community has recommended Linux kernel 2.6.36 or later for running MongoDB in production.
Because MongoDB preallocates its database files before using them and because MongoDB uses very large files on average, you should use the Ext4 and XFS file systems if using the Linux kernel:
For MongoDB on Linux use the following recommended configurations:
For random access use patterns set readahead values low, for example setting readahead to a small value such as 32 (16KB) often works well.
The section describes considerations when running MongoDB in some of the more common virtual environments.
MongoDB is compatible with EC2 and requires no configuration changes specific to the environment.
MongoDB is compatible with VMWare. Some in the MongoDB community have run into issues with VMWare’s memory overcommit feature and suggest disabling the feature.
You can clone a virtual machine running MongoDB. You might use this to spin up a new virtual host that will be added as a member of a replica set. If journaling is enabled, the clone snapshot will be consistent. If not using journaling, stop mongod, clone, and then restart.
The MongoDB community has encountered issues running MongoDB on OpenVZ.
Configure swap space for your systems. Having swap can prevent issues with memory contention and can prevent the OOM Killer on Linux systems from killing mongod. Because of the way mongod maps memory files to memory, the operating system will never store MongoDB data in swap.
Most MongoDB deployments should use disks backed by RAID-10.
RAID-5 and RAID-6 do not typically provide sufficient performance to support a MongoDB deployment.
RAID-0 provides good write performance but provides limited availability, and reduced performance on read operations, particularly using Amazon’s EBS volumes: as a result, avoid RAID-0 with MongoDB deployments.
Some versions of NFS perform very poorly with MongoDB and NFS is not recommended for use with MongoDB. Performance problems arise when both the data files and the journal files are both hosted on NFS: you may experience better performance if you place the journal on local or iscsi volumes. If you must use NFS, add the following NFS options to your /etc/fstab file: bg, nolock, and noatime.
Many MongoDB deployments work successfully with Amazon’s Elastic Block Store (EBS) volumes. There are certain intrinsic performance characteristics, with EBS volumes that users should consider.
MongoDB is designed specifically with commodity hardware in mind and has few hardware requirements or limitations. MongoDB core components runs on little-endian hardware primarily x86/x86_64 processors. Client libraries (i.e. drivers) can run on big or little endian systems.
When installing hardware for MongoDB, consider the following:
MongoDB and NUMA, Non-Uniform Access Memory, do not work well together. When running MongoDB on NUMA hardware, disable NUMA for MongoDB and run with an interleave memory policy. NUMA can cause a number of operational problems with MongoDB, including slow performance for periods of time or high system processor usage.
注解
On Linux, MongoDB version 2.0 and greater checks these settings on start up and prints a warning if the system is NUMA-based.
To disable NUMA for MongoDB, use the numactl command and start mongod in the following manner:
numactl --interleave=all /usr/bin/local/mongod
Adjust the proc settings using the following command:
echo 0 > /proc/sys/vm/zone_reclaim_mode
To fully disable NUMA you must perform both operations. However, you can change zone_reclaim_mode without restarting mongod. For more information, see documentation on Proc/sys/vm.
See the The MySQL “swap insanity” problem and the effects of NUMA post, which describes the effects of NUMA on databases. This blog post addresses the impact of NUMA for MySQL; however, the issues for MongoDB are similar. The post introduces NUMA its goals, and illustrates how these goals are not compatible with production databases.
On Linux, use the iostat command to check if disk I/O is a bottleneck for your database. Specify a number of seconds when running iostat to avoid displaying stats covering the time since server boot.
For example:
iostat -xm 2
Use the mount command to see what device your data directory resides on.
Key fields from iostat:
Always use 64-bit Builds for Production. MongoDB uses memory mapped files. See the 32-bit limitations for more information.
32-bit builds exist to support use on development machines and also for other miscellaneous things such as replica set arbiters.
There is a BSON Document Size – at the time of this writing 16MB per document. If you have large objects, use GridFS instead.
See write concern for more information.
Data in MongoDB has a dynamic schema. Collections do not enforce document structure. This facilitates iterative development and polymorphism. However, collections often hold documents with highly homogeneous structures. See 数据建模 for more information.
Some operational considerations include:
One very simple rule-of-thumb is not to import data from a relational database unmodified: you will generally want to “roll up” certain data into richer documents that use some embedding of nested documents and arrays (and/or arrays of subdocuments).
Set the multi parameter to true to update multiple documents that meet the query criteria. The mongo shell syntax is:
db.my_collection_name.update(my_query, my_update_expression, bool_upsert, bool_multi)
Set bool_multi to true when updating many documents. Otherwise only the first matched will update.
MongoDB strings are case sensitive. So a search for "joe" will not find "Joe".
Consider:
MongoDB data – which is JSON-style, specifically, BSON format – have several data types.
Consider the following document which has a field x with the string value "123":
{ x : "123" }
Then the following query which looks for a number value 123 will not return that document:
db.mycollection.find( { x : 123 } )
Older versions of MongoDB used a “global lock”; use MongoDB v2.2+ for better results. See the Concurrency page for more information.
Be sure you have the latest stable release if you are using a package manager. You can see what is current on the Downloads page, even if you then choose to install via a package manager.
Replica sets perform consensus elections. Use either an odd number of members (e.g., three) or else use an arbiter to get up to an odd number of votes.
See Journaling for more information.
This is important as MongoDB replica sets support automatic failover. Thus you want your secondaries to be up-to-date. You have a few options here:
Additionally, see information on replica set rollbacks.