Last week in Ruby-china posted "MongoDB those pits", reflected quite enthusiastic, many reply very insightful, one of the children's shoes in-depth mention of the MongoDB indexing method, which led me to a more in-depth understanding of the MongoDB index method and some considerations.
In the MongoDB, it is mentioned that running the index command directly at the front desk will cause the entire database to block, so the index is recommended to be built using the background method. However, this also poses a problem, before version 2.6, in the secondary server even if the background way of indexing, secondary will be indexed in a foreground way, which causes the secondary also raises the database blocking Plug problem. The 2.6 version fixes the bug,2.6 version and then uses the background method to build the index, and actually turns to the background to run.
In order to minimize the impact of indexing on MongoDB server, one approach is to create MongoDB server after converting it to standalone mode. The following are specific practices:
1. Stop the secondary server first, restart MongoDB after canceling the--replset parameter and changing the MongoDB port, at which point MongoDB will enter the standalone mode;
2. In standalone mode to run the command Ensureindex index, it is recommended to use the foreground mode of operation;
3. After indexing, shut down secondary server and start in normal way;
4. According to the above 1~3 steps to secondary indexing, and finally the primary server temporarily converted to secondary server, the same as the 1~3 index, and then convert it to primary server.
This approach is still troublesome, but it is possible to minimize the impact of indexing operations on MongoDB, and in some cases it is worth doing.