mongodb 索引建立问题
1.主从库索引建立不是同步建立:
MONGODB 2.6.6 版本,索引建立并不是同步的,而是主库建立完成后,从库接着建立索引。
不知是3.0版本是否也一样,以前使用3.0版本也没去监控主从日志情况。这坑太大了。
5:00开始建立,以为主库完成即OK.
官方说明:
Background index build (page 491) allowed on secondaries. If you initiate a background index build on a
primary, the secondaries will replicate the index build in the background.
Automatic rebuild of interrupted index builds after a restart.
– If a standalone or a primary instance terminates during an index build without a clean shutdown, mongod
now restarts the index build when the instance restarts. If the instance shuts down cleanly or if a user kills
the index build, the interrupted index builds do not automatically restart upon the restart of the server.
– If a secondary instance terminates during an index build, the mongod instance will now restart the interrupted index build when the instance restarts.
To disable this behavior, use the --noIndexBuildRetry command-line option.
从库建立索引太长时间,如果中断从库建立索引,可以使用 --noIndexBuildRetry 参数重启。
开始不知还有这个参数,直接在主库中删除前面建立的索引了。
网上看到别人是这样处理的,感觉也不错:
1.首先把 secondary server 停止,在取消 --replSet 参数,并且更改 MongoDB port 之后重新启动 MongoDB,这时候 MongoDB 将进入 standalone 模式;
2.在 standalone 模式下运行命令 ensureIndex 建立索引,建议使用 foreground 方式运行;
3.建立索引完毕之后关闭 secondary server 按正常方式启动;
下面是日志
primary:
2015-12-17T07:46:53.074+0800 [conn5530345] Index Build(background): 27309700/27314911 99%
。。。
slave:
2015-12-17T07:46:54.985+0800 [repl index builder 14] building index in background
2015-12-17T07:46:55.529+0800 [conn2154798] killcursors keyUpdates:0 numYields:0 locks(micros) r:73 167ms
2015-12-17T07:46:58.226+0800 [repl index builder 14] Index Build(background): 4700/27314911 0%
2.TTL 索引删除数据,不是一次性删除超出日期的数据,而是一点点来的,
也是一样,以前一直以为是索引建立后,TTL 删除数据是一次性操作,那里知道几个小时也没删除完成,导致数据库
压力一直很高。
后来只好先改名,再建立好新表后把数据插入进去。
db.WebLog.renameCollection("WebLog_bak");
db.WebLog.ensureIndex({MallID:1,Mac:1});
db.WebLog.ensureIndex({CreateTime:-1},{expireAfterSeconds:3600*24*3*30});
var cursor=db.WebLog_bak.find({_id:{$gte:24600000}});
while (cursor.hasNext()){
nc=cursor.next();
db.WebLog.insert(nc);
};