当前位置:Gxlcms > 数据库问题 > MongoDB数据量较大时如何构建索引--减少业务最少影响

MongoDB数据量较大时如何构建索引--减少业务最少影响

时间:2021-07-01 10:21:17 帮助过:20人阅读

为了尽量降低建立索引对 MongoDB Server 的影响,有一种方法是把 MongoDB Server 转换成standalone模式后建立。具体做法如下:

(1)首先把 secondary server 停止,在取消 --replSet 参数,并且更改 MongoDB port 之后重新启动 MongoDB,这时候 MongoDB 将进入 standalone 模式;

(2).在 standalone 模式下运行命令 ensureIndex 建立索引,使用 foreground 方式运行也可以,建议使用background方式运行;

(3)建立索引完毕之后关闭 secondary server 按正常方式启动;

4.根据上述 1~3 的步骤轮流为 secondary 建立索引,最后把 primary server 临时转换为 secondary server,同样按 1~3 的方法建立索引,再把其转换为 primary server。

这种方式还是比较麻烦的,但可以把建立索引操作对 MongoDB 的影响降到最低,在有些情况下还是值得做的。


6. 具体做法

(1)停其中一台Secondary节点

   上述副本集是三节点:节点1是primary节点,节点2和节点3是secondary节点。

以节点3第二个secondary为例操作:

$ pwd

/data/users/mgousr01/mongodb/etc

$ mongod -f shard3.conf --shutdown   关闭mongod进程

(2)将shard3.conf配置文件的replSet=shard1注释掉

$ vim shard3.conf 

……

#replSet=shard1

……

(3)然后启动

mongod -f shard3.conf

(4)登陆进去创建索引

mongo x.x.x.x:37017/admin

> use chicago

Build the Index

> db.users.ensureIndex({username:1,created:1},{unique:true},{name:"username_created_unique"},{background:true}) 

【建议以后创建索引 要有命名规范】

(5)查看索引信息

> db.users.getIndexes()  

[

{

"v" : 1,

"key" : {

"_id" : 1

},

"name" : "_id_",

"ns" : "chicago.users"

},

{

"v" : 1,

"unique" : true,

"key" : {

"username" : 1,

"created" : 1

},

"name" : "username_1_created_1",

"ns" : "chicago.users"

}

]


(6)再次停掉副本集的mongod进程

$ pwd

/data/users/mgousr01/mongodb/etc

$ mongod -f shard3.conf --shutdown

(7)启动mongod进程

将shard3.conf配置文件的replSet=shard1注释去掉;然后启动

mongod -f shard3.conf

mongo ip:37017/admin


启动后,会将节点加入到副本集中,然后同步primary数据,secondary上的索引不会对主造成影响导致主从不一致状况发生。



7.对第二个secondary副本操作--节点2

重复第6步即可。


8.构建所有secondarys索引大致步骤

For each secondary in the set, build an index according to the following steps:

(1)Stop One Secondary

(2)Build the Index

(3)Restart the Program mongod


9.构建primary节点索引

(1)登陆到主节点

mongo ip:37017/admin

(2) 将主节点降级

shard1:PRIMARY> rs.stepDown(30)

2016-04-19T12:49:44.423+0800 I NETWORK  DBClientCursor::init call() failed

2016-04-19T12:49:44.426+0800 E QUERY    Error: error doing query: failed

    at DBQuery._exec (src/mongo/shell/query.js:83:36)

    at DBQuery.hasNext (src/mongo/shell/query.js:240:10)

    at DBCollection.findOne (src/mongo/shell/collection.js:187:19)

    at DB.runCommand (src/mongo/shell/db.js:58:41)

    at DB.adminCommand (src/mongo/shell/db.js:66:41)

    at Function.rs.stepDown (src/mongo/shell/utils.js:1006:15)

    at (shell):1:4 at src/mongo/shell/query.js:83

2016-04-19T12:49:44.427+0800 I NETWORK  trying reconnect to xxxx failed

2016-04-19T12:49:44.428+0800 I NETWORK  reconnect xxxx ok

shard1:SECONDARY> 

降级命令执行后,会主动变成secondary节点。上述两个secondary节点会有一个节点选举成为primary节点。

(3)后续构建索引的方法和第6步一样。


说明:

让primary降级:rs.stepDown(downseconds=60),primary降级期间,它不会参与选举,如果降级时限过后,副本集还是没有primary,它会参与选举。

Preventing Electoins

让secondaries保持状态:rs.freeze(seconds),这样就可以在这个时间内对primary做处理,而不用担心除非选举。

解除状态锁定:rs.freeze(0)。





      


本文出自 “九指神丐” 博客,请务必保留此出处http://beigai.blog.51cto.com/8308160/1765457

MongoDB数据量较大时如何构建索引--减少业务最少影响

标签:大数据量 索引

人气教程排行