3. 配置分片副本集
3.1 设置第一个分片副本集
(三台机器)设置第一个分片副本集
配置文件
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
| vi /usr/local/mongodb/conf/shard1.conf
pidfilepath = /usr/local/mongodb/shard1/log/shard1.pid dbpath = /usr/local/mongodb/shard1/data logpath = /usr/local/mongodb/shard1/log/shard1.log logappend = true
bind_ip = 0.0.0.0 port = 27001 fork = true
replSet = shard1
shardsvr = true
maxConns = 20000
|
启动三台服务器的shard1 server
1
| mongod -f /usr/local/mongodb/conf/shard1.conf
|
登陆任意一台服务器,初始化副本集(除了192.168.252.123)
连接 MongoDB
使用admin数据库
定义副本集配置
1 2 3 4 5 6 7 8
| config = { _id : "shard1", members : [ {_id : 0, host : "192.168.252.121:27001" }, {_id : 1, host : "192.168.252.122:27001" }, {_id : 2, host : "192.168.252.123:27001" , arbiterOnly: true } ] }
|
初始化副本集配置
响应内容如下
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
| > use admin switched to db admin > config = { ... _id : "shard1", ... members : [ ... {_id : 0, host : "192.168.252.121:27001" }, ... {_id : 1, host : "192.168.252.122:27001" }, ... {_id : 2, host : "192.168.252.123:27001" , arbiterOnly: true } ... ] ... } { "_id" : "shard1", "members" : [ { "_id" : 0, "host" : "192.168.252.121:27001" }, { "_id" : 1, "host" : "192.168.252.122:27001" }, { "_id" : 2, "host" : "192.168.252.123:27001", "arbiterOnly" : true } ] } > rs.initiate(config) { "ok" : 1 }
|
此时会发现终端上的输出已经有了变化。
1 2 3 4
| //从单个一个 > //变成了 shard1:SECONDARY>
|
查询状态
1
| shard1:SECONDARY> rs.status()
|
当前共有 0 条评论