Linux/Python学习论坛-京峰教育

 找回密码
 立即注册

一键登录:

搜索
热搜: 活动 交友 discuz
查看: 1921|回复: 0

mongodb集群的搭建和扩展还有容灾

[复制链接]

238

主题

288

帖子

1925

积分

超级版主

Rank: 8Rank: 8

积分
1925
QQ
发表于 2015-3-18 13:03:51 | 显示全部楼层 |阅读模式
8台服务器(在这里是虚拟机)
系统都是CentOS6.4
都是64位系统
ip分别是192.168.206.101-108,其中192.168.206.101有两个IP,另一个IP是192.168.0.172用来与集群之外的服务器连接(导入数据的时候用到)
192.168.206.101-103作配置服务器
192.168.206.101-102作mongos
192.168.206.103-105作数据存储服务器(1个副本集,其中192.168.206.103也是配置服务器)
192.168.206.106-108作数据存储服务器(另1个副本集)
内核版本都是2.6.32-358.el6.x86_64


感谢一下老大给的支持, 每次搞这些需要很多虚拟机的实验我都在感叹, 有这么一台能开好多好多虚拟机又不卡的机器真好啊.


这文章说明了整个架构的搭建, 副本集的创建, 以及配置服务器挂掉, 副本集的数据服务器挂掉, 以及扩容, 还有用新机器在线恢复集群的过程


数据的查询是这样的, 客户端发送请求到mongos, mongos去查询配置服务器得到数据的概况生成执行计划, 然后再根据执行计划到具体的服务器去查数据, 然后把结果汇总返回给客户端, 而这个"具体的服务器"可以是一个集群, 也就是"副本集", 也可以是单台服务器. 我觉得mongodb的集群就像是阵列10, 它只对外一个接口(或者多个接口, 开启多个mongos即可), 然后下面做了阵列RAID0(数据存取单元), 然后这个RAID0里面的每一个单位又是一个RAID1的阵列(副本集), 而这个阵列的信息就保存在阵列卡(配置服务器)中.


下载到mongodb
[root@101 ~]# ls
mongodb-linux-x86_64-2.6.1.tgz
[root@101 ~]# ▊




解压
[root@101 ~]# tar xvf mongodb-linux-x86_64-2.6.1.tgz
[root@101 ~]# ▊




安装mongodb
[root@101 ~]# cd mongodb-linux-x86_64-2.6.1/bin/
[root@101 bin]# for i in `seq 1 8`;do scp * 192.168.206.10$i:/bin;done
[root@101 bin]# cd
[root@101 ~]# ▊




建立存放配置数据库文件的目录
[root@101 ~]# for i in `seq 1 2`;do ssh 192.168.206.10$i "mkdir /opt/mongodb/dbs -p";done
[root@101 ~]# ▊




创建存放数据数据库文件的目录
[root@101 ~]# for i in `seq 3 8`;do echo 192.168.206.10$i;ssh 192.168.206.10$i "mkdir -p /opt/mongodb/datadbs";done
[root@101 ~]# ▊




关闭selinux
[root@101 ~]# for i in `seq 1 8`;do ssh 192.168.206.10$i "setenforce 0";done
[root@101 ~]# ▊




关闭防火墙
[root@101 ~]# for i in `seq 1 8`;do ssh 192.168.206.10$i "service iptables stop";done
[root@101 ~]# ▊




同步时间
[root@101 ~]# for i in `seq 1 8`;do echo 192.168.206.10$i;ssh 192.168.206.10$i "ntpdate 0.rhel.pool.ntp.org";done
192.168.206.101
28 Jul 11:36:46 ntpdate[11605]: step time server 202.112.31.197 offset 221.640082 sec
192.168.206.102
28 Jul 11:36:52 ntpdate[8635]: step time server 202.112.29.82 offset 47022.628137 sec
192.168.206.103
28 Jul 11:36:58 ntpdate[7805]: step time server 202.112.31.197 offset 47021.573732 sec
192.168.206.104
28 Jul 11:37:04 ntpdate[7379]: step time server 202.112.29.82 offset 47021.571892 sec
192.168.206.105
28 Jul 11:37:10 ntpdate[3335]: step time server 202.112.29.82 offset 47021.770995 sec
192.168.206.106
28 Jul 11:36:46 ntpdate[11605]: step time server 202.112.31.197 offset 221.640082 sec
192.168.206.107
28 Jul 11:36:58 ntpdate[7805]: step time server 202.112.31.197 offset 47021.573732 sec
192.168.206.108
28 Jul 11:36:52 ntpdate[8635]: step time server 202.112.29.82 offset 47022.628137 sec
[root@101 ~]# ▊




启动配置服务器
[root@101 ~]# mongod --configsvr --dbpath /opt/mongodb/dbs/
2014-07-28T11:38:44.714+0800 [initandlisten] MongoDB starting : pid=11647 port=27019 dbpath=/opt/mongodb/dbs/ master=1 64-bit host=101
2014-07-28T11:38:44.714+0800 [initandlisten] db version v2.6.1
2014-07-28T11:38:44.714+0800 [initandlisten] git version: 4b95b086d2374bdcfcdf2249272fb552c9c726e8
2014-07-28T11:38:44.714+0800 [initandlisten] build info: Linux build14.nj1.10gen.cc 2.6.32-431.3.1.el6.x86_64 #1 SMP Fri Jan 3 21:39:27 UTC 2014 x86_64 BOOST_LIB_VERSION=1_49
2014-07-28T11:38:44.714+0800 [initandlisten] allocator: tcmalloc
2014-07-28T11:38:44.714+0800 [initandlisten] options: { sharding: { clusterRole: "configsvr" }, storage: { dbPath: "/opt/mongodb/dbs/" } }
2014-07-28T11:38:44.727+0800 [initandlisten] journal dir=/opt/mongodb/dbs/journal
2014-07-28T11:38:44.727+0800 [initandlisten] recover : no journal files present, no recovery needed
2014-07-28T11:38:44.776+0800 [FileAllocator] allocating new datafile /opt/mongodb/dbs/local.ns, filling with zeroes...
2014-07-28T11:38:44.776+0800 [FileAllocator] creating directory /opt/mongodb/dbs/_tmp
2014-07-28T11:38:44.777+0800 [FileAllocator] done allocating datafile /opt/mongodb/dbs/local.ns, size: 16MB,  took 0 secs
2014-07-28T11:38:44.782+0800 [FileAllocator] allocating new datafile /opt/mongodb/dbs/local.0, filling with zeroes...
2014-07-28T11:38:44.782+0800 [FileAllocator] done allocating datafile /opt/mongodb/dbs/local.0, size: 16MB,  took 0 secs
2014-07-28T11:38:44.784+0800 [initandlisten] build index on: local.startup_log properties: { v: 1, key: { _id: 1 }, name: "_id_", ns: "local.startup_log" }
2014-07-28T11:38:44.784+0800 [initandlisten]     added index to empty collection
2014-07-28T11:38:44.797+0800 [initandlisten] ******
2014-07-28T11:38:44.797+0800 [initandlisten] creating replication oplog of size: 5MB...
2014-07-28T11:38:44.797+0800 [initandlisten] ******
2014-07-28T11:38:44.797+0800 [initandlisten] waiting for connections on port 27019





[root@102 ~]# mongod --configsvr --dbpath /opt/mongodb/dbs/
2014-07-28T11:39:10.001+0800 [initandlisten] MongoDB starting : pid=8649 port=27019 dbpath=/opt/mongodb/dbs/ master=1 64-bit host=102
2014-07-28T11:39:10.001+0800 [initandlisten] db version v2.6.1
2014-07-28T11:39:10.002+0800 [initandlisten] git version: 4b95b086d2374bdcfcdf2249272fb552c9c726e8
2014-07-28T11:39:10.002+0800 [initandlisten] build info: Linux build14.nj1.10gen.cc 2.6.32-431.3.1.el6.x86_64 #1 SMP Fri Jan 3 21:39:27 UTC 2014 x86_64 BOOST_LIB_VERSION=1_49
2014-07-28T11:39:10.002+0800 [initandlisten] allocator: tcmalloc
2014-07-28T11:39:10.002+0800 [initandlisten] options: { sharding: { clusterRole: "configsvr" }, storage: { dbPath: "/opt/mongodb/dbs/" } }
2014-07-28T11:39:10.006+0800 [initandlisten] journal dir=/opt/mongodb/dbs/journal
2014-07-28T11:39:10.006+0800 [initandlisten] recover : no journal files present, no recovery needed
2014-07-28T11:39:10.040+0800 [FileAllocator] allocating new datafile /opt/mongodb/dbs/local.ns, filling with zeroes...
2014-07-28T11:39:10.040+0800 [FileAllocator] creating directory /opt/mongodb/dbs/_tmp
2014-07-28T11:39:10.041+0800 [FileAllocator] done allocating datafile /opt/mongodb/dbs/local.ns, size: 16MB,  took 0 secs
2014-07-28T11:39:10.045+0800 [FileAllocator] allocating new datafile /opt/mongodb/dbs/local.0, filling with zeroes...
2014-07-28T11:39:10.046+0800 [FileAllocator] done allocating datafile /opt/mongodb/dbs/local.0, size: 16MB,  took 0 secs
2014-07-28T11:39:10.047+0800 [initandlisten] build index on: local.startup_log properties: { v: 1, key: { _id: 1 }, name: "_id_", ns: "local.startup_log" }
2014-07-28T11:39:10.048+0800 [initandlisten]     added index to empty collection
2014-07-28T11:39:10.048+0800 [initandlisten] ******
2014-07-28T11:39:10.048+0800 [initandlisten] creating replication oplog of size: 5MB...
2014-07-28T11:39:10.049+0800 [initandlisten] ******
2014-07-28T11:39:10.049+0800 [initandlisten] waiting for connections on port 27019





[root@103 ~]# mongod --configsvr --dbpath /opt/mongodb/dbs/
2014-07-28T11:41:43.812+0800 [initandlisten] MongoDB starting : pid=7824 port=27019 dbpath=/opt/mongodb/dbs/ master=1 64-bit host=sl64
2014-07-28T11:41:43.813+0800 [initandlisten] db version v2.6.1
2014-07-28T11:41:43.813+0800 [initandlisten] git version: 4b95b086d2374bdcfcdf2249272fb552c9c726e8
2014-07-28T11:41:43.813+0800 [initandlisten] build info: Linux build14.nj1.10gen.cc 2.6.32-431.3.1.el6.x86_64 #1 SMP Fri Jan 3 21:39:27 UTC 2014 x86_64 BOOST_LIB_VERSION=1_49
2014-07-28T11:41:43.814+0800 [initandlisten] allocator: tcmalloc
2014-07-28T11:41:43.814+0800 [initandlisten] options: { sharding: { clusterRole: "configsvr" }, storage: { dbPath: "/opt/mongodb/dbs/" } }
2014-07-28T11:41:43.838+0800 [initandlisten] journal dir=/opt/mongodb/dbs/journal
2014-07-28T11:41:43.838+0800 [initandlisten] recover : no journal files present, no recovery needed
2014-07-28T11:41:43.881+0800 [FileAllocator] allocating new datafile /opt/mongodb/dbs/local.ns, filling with zeroes...
2014-07-28T11:41:43.881+0800 [FileAllocator] creating directory /opt/mongodb/dbs/_tmp
2014-07-28T11:41:43.883+0800 [FileAllocator] done allocating datafile /opt/mongodb/dbs/local.ns, size: 16MB,  took 0 secs
2014-07-28T11:41:43.893+0800 [FileAllocator] allocating new datafile /opt/mongodb/dbs/local.0, filling with zeroes...
2014-07-28T11:41:43.894+0800 [FileAllocator] done allocating datafile /opt/mongodb/dbs/local.0, size: 16MB,  took 0 secs
2014-07-28T11:41:43.899+0800 [initandlisten] build index on: local.startup_log properties: { v: 1, key: { _id: 1 }, name: "_id_", ns: "local.startup_log" }
2014-07-28T11:41:43.899+0800 [initandlisten]     added index to empty collection
2014-07-28T11:41:43.901+0800 [initandlisten] ******
2014-07-28T11:41:43.901+0800 [initandlisten] creating replication oplog of size: 5MB...
2014-07-28T11:41:43.902+0800 [initandlisten] ******
2014-07-28T11:41:43.902+0800 [initandlisten] waiting for connections on port 27019





启动mongos,可以只启动一个, 也可以随意启动多个, 随意
[root@101 ~]# mongos --configdb 192.168.206.101:27019,192.168.206.102:27019,192.168.206.103:27019
2014-07-28T11:49:56.432+0800 [mongosMain] MongoS version 2.6.1 starting: pid=11898 port=27017 64-bit host=101 (--help for usage)
2014-07-28T11:49:56.432+0800 [mongosMain] db version v2.6.1
2014-07-28T11:49:56.432+0800 [mongosMain] git version: 4b95b086d2374bdcfcdf2249272fb552c9c726e8
2014-07-28T11:49:56.432+0800 [mongosMain] build info: Linux build14.nj1.10gen.cc 2.6.32-431.3.1.el6.x86_64 #1 SMP Fri Jan 3 21:39:27 UTC 2014 x86_64 BOOST_LIB_VERSION=1_49
2014-07-28T11:49:56.432+0800 [mongosMain] allocator: tcmalloc
2014-07-28T11:49:56.432+0800 [mongosMain] options: { sharding: { configDB: "192.168.206.101:27019,192.168.206.102:27019,192.168.206.103:27019" } }
2014-07-28T11:49:56.458+0800 [mongosMain] SyncClusterConnection connecting to [192.168.206.101:27019]
2014-07-28T11:49:56.459+0800 [mongosMain] SyncClusterConnection connecting to [192.168.206.102:27019]
2014-07-28T11:49:56.461+0800 [mongosMain] SyncClusterConnection connecting to [192.168.206.103:27019]
2014-07-28T11:49:56.530+0800 [mongosMain] scoped connection to 192.168.206.101:27019,192.168.206.102:27019,192.168.206.103:27019 not being returned to the pool
2014-07-28T11:49:56.547+0800 [mongosMain] SyncClusterConnection connecting to [192.168.206.101:27019]
2014-07-28T11:49:56.547+0800 [mongosMain] SyncClusterConnection connecting to [192.168.206.102:27019]
2014-07-28T11:49:56.548+0800 [LockPinger] creating distributed lock ping thread for 192.168.206.101:27019,192.168.206.102:27019,192.168.206.103:27019 and process 101:27017:1406519396:1804289383 (sleeping for 30000ms)
2014-07-28T11:49:56.548+0800 [LockPinger] SyncClusterConnection connecting to [192.168.206.101:27019]
2014-07-28T11:49:56.550+0800 [mongosMain] SyncClusterConnection connecting to [192.168.206.103:27019]
2014-07-28T11:49:56.551+0800 [LockPinger] SyncClusterConnection connecting to [192.168.206.102:27019]
2014-07-28T11:49:56.553+0800 [LockPinger] SyncClusterConnection connecting to [192.168.206.103:27019]
2014-07-28T11:49:56.808+0800 [LockPinger] cluster 192.168.206.101:27019,192.168.206.102:27019,192.168.206.103:27019 pinged successfully at Mon Jul 28 11:49:56 2014 by distributed lock pinger '192.168.206.101:27019,192.168.206.102:27019,192.168.206.103:27019/101:27017:1406519396:1804289383', sleeping for 30000ms
2014-07-28T11:49:56.844+0800 [mongosMain] distributed lock 'configUpgrade/101:27017:1406519396:1804289383' acquired, ts : 53d5c864d5c6355ca4b322bf
2014-07-28T11:49:56.857+0800 [mongosMain] starting upgrade of config server from v0 to v5
2014-07-28T11:49:56.857+0800 [mongosMain] starting next upgrade step from v0 to v5
2014-07-28T11:49:56.857+0800 [mongosMain] about to log new metadata event: { _id: "101-2014-07-28T03:49:56-53d5c864d5c6355ca4b322c0", server: "101", clientAddr: "N/A", time: new Date(1406519396857), what: "starting upgrade of config database", ns: "config.version", details: { from: 0, to: 5 } }
2014-07-28T11:49:56.983+0800 [mongosMain] creating WriteBackListener for: 192.168.206.101:27019 serverID: 000000000000000000000000
2014-07-28T11:49:56.986+0800 [mongosMain] creating WriteBackListener for: 192.168.206.102:27019 serverID: 000000000000000000000000
2014-07-28T11:49:56.997+0800 [mongosMain] creating WriteBackListener for: 192.168.206.103:27019 serverID: 000000000000000000000000
2014-07-28T11:49:57.068+0800 [mongosMain] writing initial config version at v5
2014-07-28T11:49:57.078+0800 [mongosMain] about to log new metadata event: { _id: "101-2014-07-28T03:49:57-53d5c865d5c6355ca4b322c2", server: "101", clientAddr: "N/A", time: new Date(1406519397078), what: "finished upgrade of config database", ns: "config.version", details: { from: 0, to: 5 } }
2014-07-28T11:49:57.093+0800 [mongosMain] upgrade of config server to v5 successful
2014-07-28T11:49:57.190+0800 [mongosMain] distributed lock 'configUpgrade/101:27017:1406519396:1804289383' unlocked.
2014-07-28T11:49:57.272+0800 [mongosMain] scoped connection to 192.168.206.101:27019,192.168.206.102:27019,192.168.206.103:27019 not being returned to the pool
2014-07-28T11:49:57.275+0800 [mongosMain] waiting for connections on port 27017
2014-07-28T11:49:57.275+0800 [Balancer] about to contact config servers and shards
2014-07-28T11:49:57.275+0800 [Balancer] SyncClusterConnection connecting to [192.168.206.101:27019]
2014-07-28T11:49:57.275+0800 [Balancer] SyncClusterConnection connecting to [192.168.206.102:27019]
2014-07-28T11:49:57.276+0800 [Balancer] SyncClusterConnection connecting to [192.168.206.103:27019]
2014-07-28T11:49:57.277+0800 [Balancer] config servers and shards contacted successfully
2014-07-28T11:49:57.277+0800 [Balancer] balancer id: 101:27017 started at Jul 28 11:49:57
2014-07-28T11:49:57.280+0800 [Balancer] SyncClusterConnection connecting to [192.168.206.101:27019]
2014-07-28T11:49:57.281+0800 [Balancer] SyncClusterConnection connecting to [192.168.206.102:27019]
2014-07-28T11:49:57.282+0800 [Balancer] SyncClusterConnection connecting to [192.168.206.103:27019]
2014-07-28T11:49:57.503+0800 [Balancer] distributed lock 'balancer/101:27017:1406519396:1804289383' acquired, ts : 53d5c865d5c6355ca4b322c4
2014-07-28T11:49:57.573+0800 [Balancer] distributed lock 'balancer/101:27017:1406519396:1804289383' unlocked.
2014-07-28T11:50:03.848+0800 [Balancer] distributed lock 'balancer/101:27017:1406519396:1804289383' acquired, ts : 53d5c86bd5c6355ca4b322c5
2014-07-28T11:50:03.986+0800 [Balancer] distributed lock 'balancer/101:27017:1406519396:1804289383' unlocked.
2014-07-28T11:50:10.254+0800 [Balancer] distributed lock 'balancer/101:27017:1406519396:1804289383' acquired, ts : 53d5c871d5c6355ca4b322c6
2014-07-28T11:50:10.392+0800 [Balancer] distributed lock 'balancer/101:27017:1406519396:1804289383' unlocked.
2014-07-28T11:50:16.683+0800 [Balancer] distributed lock 'balancer/101:27017:1406519396:1804289383' acquired, ts : 53d5c878d5c6355ca4b322c7
2014-07-28T11:50:16.821+0800 [Balancer] distributed lock 'balancer/101:27017:1406519396:1804289383' unlocked.





[root@102 ~]# mongos --configdb 192.168.206.101:27019,192.168.206.102:27019,192.168.206.103:27019
2014-07-28T11:50:57.916+0800 [mongosMain] MongoS version 2.6.1 starting: pid=8832 port=27017 64-bit host=102 (--help for usage)
2014-07-28T11:50:57.916+0800 [mongosMain] db version v2.6.1
2014-07-28T11:50:57.916+0800 [mongosMain] git version: 4b95b086d2374bdcfcdf2249272fb552c9c726e8
2014-07-28T11:50:57.917+0800 [mongosMain] build info: Linux build14.nj1.10gen.cc 2.6.32-431.3.1.el6.x86_64 #1 SMP Fri Jan 3 21:39:27 UTC 2014 x86_64 BOOST_LIB_VERSION=1_49
2014-07-28T11:50:57.917+0800 [mongosMain] allocator: tcmalloc
2014-07-28T11:50:57.917+0800 [mongosMain] options: { sharding: { configDB: "192.168.206.101:27019,192.168.206.102:27019,192.168.206.103:27019" } }
2014-07-28T11:50:57.936+0800 [mongosMain] SyncClusterConnection connecting to [192.168.206.101:27019]
2014-07-28T11:50:57.943+0800 [mongosMain] SyncClusterConnection connecting to [192.168.206.102:27019]
2014-07-28T11:50:57.944+0800 [mongosMain] SyncClusterConnection connecting to [192.168.206.103:27019]
2014-07-28T11:50:57.949+0800 [mongosMain] creating WriteBackListener for: 192.168.206.101:27019 serverID: 000000000000000000000000
2014-07-28T11:50:57.950+0800 [mongosMain] creating WriteBackListener for: 192.168.206.102:27019 serverID: 000000000000000000000000
2014-07-28T11:50:57.952+0800 [mongosMain] creating WriteBackListener for: 192.168.206.103:27019 serverID: 000000000000000000000000
2014-07-28T11:50:57.963+0800 [mongosMain] scoped connection to 192.168.206.101:27019,192.168.206.102:27019,192.168.206.103:27019 not being returned to the pool
2014-07-28T11:50:57.973+0800 [Balancer] about to contact config servers and shards
2014-07-28T11:50:57.973+0800 [Balancer] SyncClusterConnection connecting to [192.168.206.101:27019]
2014-07-28T11:50:57.975+0800 [Balancer] SyncClusterConnection connecting to [192.168.206.102:27019]
2014-07-28T11:50:57.975+0800 [mongosMain] waiting for connections on port 27017
2014-07-28T11:50:57.976+0800 [Balancer] SyncClusterConnection connecting to [192.168.206.103:27019]
2014-07-28T11:50:57.977+0800 [Balancer] config servers and shards contacted successfully
2014-07-28T11:50:57.977+0800 [Balancer] balancer id: 102:27017 started at Jul 28 11:50:57
2014-07-28T11:50:57.978+0800 [Balancer] SyncClusterConnection connecting to [192.168.206.101:27019]
2014-07-28T11:50:57.978+0800 [Balancer] SyncClusterConnection connecting to [192.168.206.102:27019]
2014-07-28T11:50:57.979+0800 [Balancer] SyncClusterConnection connecting to [192.168.206.103:27019]
2014-07-28T11:50:57.992+0800 [Balancer] SyncClusterConnection connecting to [192.168.206.101:27019]
2014-07-28T11:50:57.993+0800 [LockPinger] creating distributed lock ping thread for 192.168.206.101:27019,192.168.206.102:27019,192.168.206.103:27019 and process 102:27017:1406519457:1804289383 (sleeping for 30000ms)
2014-07-28T11:50:57.993+0800 [Balancer] SyncClusterConnection connecting to [192.168.206.102:27019]
2014-07-28T11:50:57.995+0800 [Balancer] SyncClusterConnection connecting to [192.168.206.103:27019]
2014-07-28T11:50:58.258+0800 [LockPinger] cluster 192.168.206.101:27019,192.168.206.102:27019,192.168.206.103:27019 pinged successfully at Mon Jul 28 11:50:57 2014 by distributed lock pinger '192.168.206.101:27019,192.168.206.102:27019,192.168.206.103:27019/102:27017:1406519457:1804289383', sleeping for 30000ms
2014-07-28T11:50:58.259+0800 [Balancer] distributed lock 'balancer/102:27017:1406519457:1804289383' acquired, ts : 53d5c8a12dc340b986a38970
2014-07-28T11:50:58.396+0800 [Balancer] distributed lock 'balancer/102:27017:1406519457:1804289383' unlocked.





如果只有两个配置服务器会起不来mongos
[root@101 ~]# mongos --configdb 192.168.206.101:27019,192.168.206.102:27019
BadValue need either 1 or 3 configdbs
try 'mongos --help' for more information
[root@101 ~]# ▊






启动副本集, 副本集的名字是cc
[root@103 ~]# mongod --replSet cc --dbpath /opt/mongodb/datadbs/
2014-07-28T14:17:23.021+0800 [initandlisten] MongoDB starting : pid=8554 port=27017 dbpath=/opt/mongodb/datadbs/ 64-bit host=sl64
2014-07-28T14:17:23.022+0800 [initandlisten] db version v2.6.1
2014-07-28T14:17:23.022+0800 [initandlisten] git version: 4b95b086d2374bdcfcdf2249272fb552c9c726e8
2014-07-28T14:17:23.022+0800 [initandlisten] build info: Linux build14.nj1.10gen.cc 2.6.32-431.3.1.el6.x86_64 #1 SMP Fri Jan 3 21:39:27 UTC 2014 x86_64 BOOST_LIB_VERSION=1_49
2014-07-28T14:17:23.022+0800 [initandlisten] allocator: tcmalloc
2014-07-28T14:17:23.022+0800 [initandlisten] options: { replication: { replSet: "cc" }, storage: { dbPath: "/opt/mongodb/datadbs/" } }
2014-07-28T14:17:23.035+0800 [initandlisten] journal dir=/opt/mongodb/datadbs/journal
2014-07-28T14:17:23.035+0800 [initandlisten] recover : no journal files present, no recovery needed
2014-07-28T14:17:23.078+0800 [FileAllocator] allocating new datafile /opt/mongodb/datadbs/local.ns, filling with zeroes...
2014-07-28T14:17:23.078+0800 [FileAllocator] creating directory /opt/mongodb/datadbs/_tmp
2014-07-28T14:17:23.079+0800 [FileAllocator] done allocating datafile /opt/mongodb/datadbs/local.ns, size: 16MB,  took 0 secs
2014-07-28T14:17:23.083+0800 [FileAllocator] allocating new datafile /opt/mongodb/datadbs/local.0, filling with zeroes...
2014-07-28T14:17:23.083+0800 [FileAllocator] done allocating datafile /opt/mongodb/datadbs/local.0, size: 64MB,  took 0 secs
2014-07-28T14:17:23.087+0800 [initandlisten] build index on: local.startup_log properties: { v: 1, key: { _id: 1 }, name: "_id_", ns: "local.startup_log" }
2014-07-28T14:17:23.087+0800 [initandlisten]     added index to empty collection
2014-07-28T14:17:23.107+0800 [initandlisten] waiting for connections on port 27017
2014-07-28T14:17:23.140+0800 [rsStart] replSet can't get local.system.replset config from self or any seed (EMPTYCONFIG)
2014-07-28T14:17:23.140+0800 [rsStart] replSet info you may need to run replSetInitiate -- rs.initiate() in the shell -- if that is not already done
2014-07-28T14:17:24.141+0800 [rsStart] replSet can't get local.system.replset config from self or any seed (EMPTYCONFIG)





[root@104 ~]# mongod --replSet cc --dbpath /opt/mongodb/datadbs/
2014-07-28T14:18:56.005+0800 [initandlisten] MongoDB starting : pid=8229 port=27017 dbpath=/opt/mongodb/datadbs/ 64-bit host=104
2014-07-28T14:18:56.006+0800 [initandlisten] db version v2.6.1
2014-07-28T14:18:56.006+0800 [initandlisten] git version: 4b95b086d2374bdcfcdf2249272fb552c9c726e8
2014-07-28T14:18:56.006+0800 [initandlisten] build info: Linux build14.nj1.10gen.cc 2.6.32-431.3.1.el6.x86_64 #1 SMP Fri Jan 3 21:39:27 UTC 2014 x86_64 BOOST_LIB_VERSION=1_49
2014-07-28T14:18:56.006+0800 [initandlisten] allocator: tcmalloc
2014-07-28T14:18:56.006+0800 [initandlisten] options: { replication: { replSet: "cc" }, storage: { dbPath: "/opt/mongodb/datadbs/" } }
2014-07-28T14:18:56.012+0800 [initandlisten] journal dir=/opt/mongodb/datadbs/journal
2014-07-28T14:18:56.013+0800 [initandlisten] recover : no journal files present, no recovery needed
2014-07-28T14:18:56.078+0800 [FileAllocator] allocating new datafile /opt/mongodb/datadbs/local.ns, filling with zeroes...
2014-07-28T14:18:56.078+0800 [FileAllocator] creating directory /opt/mongodb/datadbs/_tmp
2014-07-28T14:18:56.079+0800 [FileAllocator] done allocating datafile /opt/mongodb/datadbs/local.ns, size: 16MB,  took 0 secs
2014-07-28T14:18:56.083+0800 [FileAllocator] allocating new datafile /opt/mongodb/datadbs/local.0, filling with zeroes...
2014-07-28T14:18:56.084+0800 [FileAllocator] done allocating datafile /opt/mongodb/datadbs/local.0, size: 64MB,  took 0 secs
2014-07-28T14:18:56.089+0800 [initandlisten] build index on: local.startup_log properties: { v: 1, key: { _id: 1 }, name: "_id_", ns: "local.startup_log" }
2014-07-28T14:18:56.089+0800 [initandlisten]     added index to empty collection
2014-07-28T14:18:56.089+0800 [initandlisten] waiting for connections on port 27017
2014-07-28T14:18:56.094+0800 [rsStart] replSet can't get local.system.replset config from self or any seed (EMPTYCONFIG)
2014-07-28T14:18:56.094+0800 [rsStart] replSet info you may need to run replSetInitiate -- rs.initiate() in the shell -- if that is not already done
2014-07-28T14:18:57.094+0800 [rsStart] replSet can't get local.system.replset config from self or any seed (EMPTYCONFIG)





[root@105 ~]# mongod --replSet cc --dbpath /opt/mongodb/datadbs
2014-07-28T14:19:27.993+0800 [initandlisten] MongoDB starting : pid=6303 port=27017 dbpath=/opt/mongodb/datadbs 64-bit host=105
2014-07-28T14:19:27.994+0800 [initandlisten] db version v2.6.1
2014-07-28T14:19:27.994+0800 [initandlisten] git version: 4b95b086d2374bdcfcdf2249272fb552c9c726e8
2014-07-28T14:19:27.994+0800 [initandlisten] build info: Linux build14.nj1.10gen.cc 2.6.32-431.3.1.el6.x86_64 #1 SMP Fri Jan 3 21:39:27 UTC 2014 x86_64 BOOST_LIB_VERSION=1_49
2014-07-28T14:19:27.994+0800 [initandlisten] allocator: tcmalloc
2014-07-28T14:19:27.995+0800 [initandlisten] options: { replication: { replSet: "cc" }, storage: { dbPath: "/opt/mongodb/datadbs" } }
2014-07-28T14:19:28.000+0800 [initandlisten] journal dir=/opt/mongodb/datadbs/journal
2014-07-28T14:19:28.000+0800 [initandlisten] recover : no journal files present, no recovery needed
2014-07-28T14:19:28.092+0800 [FileAllocator] allocating new datafile /opt/mongodb/datadbs/local.ns, filling with zeroes...
2014-07-28T14:19:28.092+0800 [FileAllocator] creating directory /opt/mongodb/datadbs/_tmp
2014-07-28T14:19:28.094+0800 [FileAllocator] done allocating datafile /opt/mongodb/datadbs/local.ns, size: 16MB,  took 0 secs
2014-07-28T14:19:28.099+0800 [FileAllocator] allocating new datafile /opt/mongodb/datadbs/local.0, filling with zeroes...
2014-07-28T14:19:28.100+0800 [FileAllocator] done allocating datafile /opt/mongodb/datadbs/local.0, size: 64MB,  took 0.001 secs
2014-07-28T14:19:28.104+0800 [initandlisten] build index on: local.startup_log properties: { v: 1, key: { _id: 1 }, name: "_id_", ns: "local.startup_log" }
2014-07-28T14:19:28.104+0800 [initandlisten]     added index to empty collection
2014-07-28T14:19:28.104+0800 [initandlisten] waiting for connections on port 27017
2014-07-28T14:19:28.108+0800 [rsStart] replSet can't get local.system.replset config from self or any seed (EMPTYCONFIG)
2014-07-28T14:19:28.108+0800 [rsStart] replSet info you may need to run replSetInitiate -- rs.initiate() in the shell -- if that is not already done
2014-07-28T14:19:29.109+0800 [rsStart] replSet can't get local.system.replset config from self or any seed (EMPTYCONFIG)





初始化副本集
[root@103 ~]# mongo # 任意机器启动mongo shell
MongoDB shell version: 2.6.1
connecting to: test
Welcome to the MongoDB shell.
For interactive help, type "help".
For more comprehensive documentation, see
        http://docs.mongodb.org/
Questions? Try the support group
        http://groups.google.com/group/mongodb-user
> config = {   # 定义一个配置文件变量
...     "_id": "cc",
...     "members": [
...         {"_id": 0, "host": "192.168.206.103:27017"},
...         {"_id": 1, "host": "192.168.206.104:27017"},
...         {"_id": 2, "host": "192.168.206.105:27017"},
...     ]
... }
{
        "_id" : "cc",
        "members" : [
                {
                        "_id" : 0,
                        "host" : "192.168.206.103:27017"
                },
                {
                        "_id" : 1,
                        "host" : "192.168.206.104:27017"
                },
                {
                        "_id" : 2,
                        "host" : "192.168.206.105:27017"
                }
        ]
}
> db = (new Mongo("192.168.206.103:27017")).getDB("test") # 连接到副本集的其中一台机器
test
> rs.initiate(config) # 初始化副本集
{
        "info" : "Config now saved locally.  Should come online in about a minute.",
        "ok" : 1
}
> ▊




查看副本集状态, 当创建了副本集之后, 提示符就变了
[root@103 ~]# mongo
MongoDB shell version: 2.6.1
connecting to: test
cc:PRIMARY> rs.status()
{
        "set" : "cc",
        "date" : ISODate("2014-07-28T06:29:38Z"),
        "myState" : 1,
        "members" : [
                {
                        "_id" : 0,
                        "name" : "192.168.206.103:27017",
                        "health" : 1,
                        "state" : 1,
                        "stateStr" : "PRIMARY",
                        "uptime" : 652,
                        "optime" : Timestamp(1406528714, 1),
                        "optimeDate" : ISODate("2014-07-28T06:25:14Z"),
                        "electionTime" : Timestamp(1406528725, 1),
                        "electionDate" : ISODate("2014-07-28T06:25:25Z"),
                        "self" : true
                },
                {
                        "_id" : 1,
                        "name" : "192.168.206.104:27017",
                        "health" : 1,
                        "state" : 2,
                        "stateStr" : "SECONDARY",
                        "uptime" : 261,
                        "optime" : Timestamp(1406528714, 1),
                        "optimeDate" : ISODate("2014-07-28T06:25:14Z"),
                        "lastHeartbeat" : ISODate("2014-07-28T06:29:37Z"),
                        "lastHeartbeatRecv" : ISODate("2014-07-28T06:29:36Z"),
                        "pingMs" : 0,
                        "syncingTo" : "192.168.206.103:27017"
                },
                {
                        "_id" : 2,
                        "name" : "192.168.206.105:27017",
                        "health" : 1,
                        "state" : 2,
                        "stateStr" : "SECONDARY",
                        "uptime" : 261,
                        "optime" : Timestamp(1406528714, 1),
                        "optimeDate" : ISODate("2014-07-28T06:25:14Z"),
                        "lastHeartbeat" : ISODate("2014-07-28T06:29:37Z"),
                        "lastHeartbeatRecv" : ISODate("2014-07-28T06:29:38Z"),
                        "pingMs" : 0,
                        "syncingTo" : "192.168.206.103:27017"
                }
        ],
        "ok" : 1
}
cc:PRIMARY> ▊




把副本集添加到集群, 只需要指定副本集里面的一台机器就可以了
[root@101 ~]# mongo
MongoDB shell version: 2.6.1
connecting to: test
Welcome to the MongoDB shell.
For interactive help, type "help".
For more comprehensive documentation, see
        http://docs.mongodb.org/
Questions? Try the support group
        http://groups.google.com/group/mongodb-user
mongos> sh.addShard("cc/192.168.206.103:27017")
{ "shardAdded" : "cc", "ok" : 1 }
mongos> sh.status()
--- Sharding Status ---
  sharding version: {
        "_id" : 1,
        "version" : 4,
        "minCompatibleVersion" : 4,
        "currentVersion" : 5,
        "clusterId" : ObjectId("53d5c865d5c6355ca4b322c1")
}
  shards:
        {  "_id" : "cc",  "host" : "cc/192.168.206.103:27017,192.168.206.104:27017,192.168.206.105:27017" }
  databases:
        {  "_id" : "admin",  "partitioned" : false,  "primary" : "config" }


mongos> ▊




倒入数据到数据库cc,collection名字是scan(过程略)


把数据库cc启用分片(在一个配置服务器当机的情况下)
mongos> sh.enableSharding("cc")
{
        "code" : 13396,
        "ok" : 0,
        "errmsg" : "exception: DBConfig save failed: { ok: 0, code: 25, errmsg: \"could not verify config servers were active and reachable before write\" }"
}
mongos> sh.enableSharding("cc")
{ "ok" : 0, "errmsg" : "already enabled" }
mongos> ▊




对数据库的scan这个collection启用分片(所有配置服务器都正常的情况下),根据_id做的分片
mongos> sh.shardCollection("cc.scan", {"_id":1})
{ "collectionsharded" : "cc.scan", "ok" : 1 }
mongos> ▊




当一个配置服务器当机, 是不能对collection启用分片的
mongos> sh.shardCollection("cc.scan", {"_id":1})
{
        "code" : 15903,
        "ok" : 0,
        "errmsg" : "exception: creating first chunks failed. result: could not verify config servers were active and reachable before write"
}
mongos> ▊




查看状态
mongos> sh.status()
--- Sharding Status ---
  sharding version: {
        "_id" : 1,
        "version" : 4,
        "minCompatibleVersion" : 4,
        "currentVersion" : 5,
        "clusterId" : ObjectId("53d5c865d5c6355ca4b322c1")
}
  shards:
        {  "_id" : "cc",  "host" : "cc/192.168.206.103:27017,192.168.206.104:27017,192.168.206.105:27017" }
  databases:
        {  "_id" : "admin",  "partitioned" : false,  "primary" : "config" }
        {  "_id" : "cc",  "partitioned" : true,  "primary" : "cc" }
                cc.scan
                        shard key: { "_id" : 1 }
                        chunks:
                                cc      13
                        { "_id" : { "$minKey" : 1 } } -->> { "_id" : ObjectId("53ba8013d6ca3f1f110037cc") } on : cc Timestamp(1, 0)
                        { "_id" : ObjectId("53ba8013d6ca3f1f110037cc") } -->> { "_id" : ObjectId("53ba801cd6ca3f1f1100613f") } on : cc Timestamp(1, 1)
                        { "_id" : ObjectId("53ba801cd6ca3f1f1100613f") } -->> { "_id" : ObjectId("53ba8022d6ca3f1f11007d41") } on : cc Timestamp(1, 2)
                        { "_id" : ObjectId("53ba8022d6ca3f1f11007d41") } -->> { "_id" : ObjectId("53ba8028d6ca3f1f11009943") } on : cc Timestamp(1, 3)
                        { "_id" : ObjectId("53ba8028d6ca3f1f11009943") } -->> { "_id" : ObjectId("53ba802dd6ca3f1f1100b545") } on : cc Timestamp(1, 4)
                        { "_id" : ObjectId("53ba802dd6ca3f1f1100b545") } -->> { "_id" : ObjectId("53ba8034d6ca3f1f1100d14b") } on : cc Timestamp(1, 5)
                        { "_id" : ObjectId("53ba8034d6ca3f1f1100d14b") } -->> { "_id" : ObjectId("53ba803ad6ca3f1f1100ed56") } on : cc Timestamp(1, 6)
                        { "_id" : ObjectId("53ba803ad6ca3f1f1100ed56") } -->> { "_id" : ObjectId("53ba8040d6ca3f1f11010958") } on : cc Timestamp(1, 7)
                        { "_id" : ObjectId("53ba8040d6ca3f1f11010958") } -->> { "_id" : ObjectId("53ba8046d6ca3f1f1101255a") } on : cc Timestamp(1, 8)
                        { "_id" : ObjectId("53ba8046d6ca3f1f1101255a") } -->> { "_id" : ObjectId("53ba804cd6ca3f1f1101415c") } on : cc Timestamp(1, 9)
                        { "_id" : ObjectId("53ba804cd6ca3f1f1101415c") } -->> { "_id" : ObjectId("53ba8053d6ca3f1f11015d5e") } on : cc Timestamp(1, 10)
                        { "_id" : ObjectId("53ba8053d6ca3f1f11015d5e") } -->> { "_id" : ObjectId("53ba8059d6ca3f1f11017960") } on : cc Timestamp(1, 11)
                        { "_id" : ObjectId("53ba8059d6ca3f1f11017960") } -->> { "_id" : { "$maxKey" : 1 } } on : cc Timestamp(1, 12) # 这里的cc是副本集名字


mongos> ▊




配置另一个副本集
[root@106 ~]# mongod --replSet dd --dbpath /opt/mongodb/datadbs/
2014-07-28T16:11:58.132+0800 [initandlisten] MongoDB starting : pid=3350 port=27017 dbpath=/opt/mongodb/datadbs/ 64-bit host=106
2014-07-28T16:11:58.132+0800 [initandlisten] db version v2.6.1
2014-07-28T16:11:58.132+0800 [initandlisten] git version: 4b95b086d2374bdcfcdf2249272fb552c9c726e8
2014-07-28T16:11:58.132+0800 [initandlisten] build info: Linux build14.nj1.10gen.cc 2.6.32-431.3.1.el6.x86_64 #1 SMP Fri Jan 3 21:39:27 UTC 2014 x86_64 BOOST_LIB_VERSION=1_49
2014-07-28T16:11:58.132+0800 [initandlisten] allocator: tcmalloc
2014-07-28T16:11:58.132+0800 [initandlisten] options: { replication: { replSet: "dd" }, storage: { dbPath: "/opt/mongodb/datadbs/" } }
2014-07-28T16:11:58.149+0800 [initandlisten] journal dir=/opt/mongodb/datadbs/journal
2014-07-28T16:11:58.149+0800 [initandlisten] recover : no journal files present, no recovery needed
2014-07-28T16:11:58.203+0800 [FileAllocator] allocating new datafile /opt/mongodb/datadbs/local.ns, filling with zeroes...
2014-07-28T16:11:58.203+0800 [FileAllocator] creating directory /opt/mongodb/datadbs/_tmp
2014-07-28T16:11:58.205+0800 [FileAllocator] done allocating datafile /opt/mongodb/datadbs/local.ns, size: 16MB,  took 0.001 secs
2014-07-28T16:11:58.210+0800 [FileAllocator] allocating new datafile /opt/mongodb/datadbs/local.0, filling with zeroes...
2014-07-28T16:11:58.210+0800 [FileAllocator] done allocating datafile /opt/mongodb/datadbs/local.0, size: 64MB,  took 0 secs
2014-07-28T16:11:58.212+0800 [initandlisten] build index on: local.startup_log properties: { v: 1, key: { _id: 1 }, name: "_id_", ns: "local.startup_log" }
2014-07-28T16:11:58.212+0800 [initandlisten]     added index to empty collection
2014-07-28T16:11:58.212+0800 [initandlisten] waiting for connections on port 27017
2014-07-28T16:11:58.216+0800 [rsStart] replSet can't get local.system.replset config from self or any seed (EMPTYCONFIG)
2014-07-28T16:11:58.216+0800 [rsStart] replSet info you may need to run replSetInitiate -- rs.initiate() in the shell -- if that is not already done
2014-07-28T16:11:59.216+0800 [rsStart] replSet can't get local.system.replset config from self or any seed (EMPTYCONFIG)
2014-07-28T16:12:00.217+0800 [rsStart] replSet can't get local.system.replset config from self or any seed (EMPTYCONFIG)





[root@107 ~]# mongod --replSet dd --dbpath /opt/mongodb/datadbs/
2014-07-28T16:13:04.883+0800 [initandlisten] MongoDB starting : pid=3302 port=27017 dbpath=/opt/mongodb/datadbs/ 64-bit host=107
2014-07-28T16:13:04.884+0800 [initandlisten] db version v2.6.1
2014-07-28T16:13:04.884+0800 [initandlisten] git version: 4b95b086d2374bdcfcdf2249272fb552c9c726e8
2014-07-28T16:13:04.884+0800 [initandlisten] build info: Linux build14.nj1.10gen.cc 2.6.32-431.3.1.el6.x86_64 #1 SMP Fri Jan 3 21:39:27 UTC 2014 x86_64 BOOST_LIB_VERSION=1_49
2014-07-28T16:13:04.884+0800 [initandlisten] allocator: tcmalloc
2014-07-28T16:13:04.884+0800 [initandlisten] options: { replication: { replSet: "dd" }, storage: { dbPath: "/opt/mongodb/datadbs/" } }
2014-07-28T16:13:04.888+0800 [initandlisten] journal dir=/opt/mongodb/datadbs/journal
2014-07-28T16:13:04.889+0800 [initandlisten] recover : no journal files present, no recovery needed
2014-07-28T16:13:04.959+0800 [FileAllocator] allocating new datafile /opt/mongodb/datadbs/local.ns, filling with zeroes...
2014-07-28T16:13:04.959+0800 [FileAllocator] creating directory /opt/mongodb/datadbs/_tmp
2014-07-28T16:13:04.961+0800 [FileAllocator] done allocating datafile /opt/mongodb/datadbs/local.ns, size: 16MB,  took 0 secs
2014-07-28T16:13:04.971+0800 [FileAllocator] allocating new datafile /opt/mongodb/datadbs/local.0, filling with zeroes...
2014-07-28T16:13:04.972+0800 [FileAllocator] done allocating datafile /opt/mongodb/datadbs/local.0, size: 64MB,  took 0 secs
2014-07-28T16:13:04.981+0800 [initandlisten] build index on: local.startup_log properties: { v: 1, key: { _id: 1 }, name: "_id_", ns: "local.startup_log" }
2014-07-28T16:13:04.981+0800 [initandlisten]     added index to empty collection
2014-07-28T16:13:04.982+0800 [initandlisten] waiting for connections on port 27017
2014-07-28T16:13:05.004+0800 [rsStart] replSet can't get local.system.replset config from self or any seed (EMPTYCONFIG)
2014-07-28T16:13:05.004+0800 [rsStart] replSet info you may need to run replSetInitiate -- rs.initiate() in the shell -- if that is not already done
2014-07-28T16:13:06.004+0800 [rsStart] replSet can't get local.system.replset config from self or any seed (EMPTYCONFIG)





[root@108 ~]# mongod --replSet dd --dbpath /opt/mongodb/datadbs/
2014-07-28T16:13:29.068+0800 [initandlisten] MongoDB starting : pid=5125 port=27017 dbpath=/opt/mongodb/datadbs/ 64-bit host=108
2014-07-28T16:13:29.069+0800 [initandlisten] db version v2.6.1
2014-07-28T16:13:29.069+0800 [initandlisten] git version: 4b95b086d2374bdcfcdf2249272fb552c9c726e8
2014-07-28T16:13:29.069+0800 [initandlisten] build info: Linux build14.nj1.10gen.cc 2.6.32-431.3.1.el6.x86_64 #1 SMP Fri Jan 3 21:39:27 UTC 2014 x86_64 BOOST_LIB_VERSION=1_49
2014-07-28T16:13:29.069+0800 [initandlisten] allocator: tcmalloc
2014-07-28T16:13:29.069+0800 [initandlisten] options: { replication: { replSet: "dd" }, storage: { dbPath: "/opt/mongodb/datadbs/" } }
2014-07-28T16:13:29.073+0800 [initandlisten] journal dir=/opt/mongodb/datadbs/journal
2014-07-28T16:13:29.073+0800 [initandlisten] recover : no journal files present, no recovery needed
2014-07-28T16:13:29.126+0800 [FileAllocator] allocating new datafile /opt/mongodb/datadbs/local.ns, filling with zeroes...
2014-07-28T16:13:29.126+0800 [FileAllocator] creating directory /opt/mongodb/datadbs/_tmp
2014-07-28T16:13:29.127+0800 [FileAllocator] done allocating datafile /opt/mongodb/datadbs/local.ns, size: 16MB,  took 0 secs
2014-07-28T16:13:29.131+0800 [FileAllocator] allocating new datafile /opt/mongodb/datadbs/local.0, filling with zeroes...
2014-07-28T16:13:29.132+0800 [FileAllocator] done allocating datafile /opt/mongodb/datadbs/local.0, size: 64MB,  took 0 secs
2014-07-28T16:13:29.135+0800 [initandlisten] build index on: local.startup_log properties: { v: 1, key: { _id: 1 }, name: "_id_", ns: "local.startup_log" }
2014-07-28T16:13:29.135+0800 [initandlisten]     added index to empty collection
2014-07-28T16:13:29.135+0800 [initandlisten] waiting for connections on port 27017
2014-07-28T16:13:29.138+0800 [rsStart] replSet can't get local.system.replset config from self or any seed (EMPTYCONFIG)
2014-07-28T16:13:29.138+0800 [rsStart] replSet info you may need to run replSetInitiate -- rs.initiate() in the shell -- if that is not already done
2014-07-28T16:13:30.139+0800 [rsStart] replSet can't get local.system.replset config from self or any seed (EMPTYCONFIG)
2014-07-28T16:13:31.139+0800 [rsStart] replSet can't get local.system.replset config from self or any seed (EMPTYCONFIG)





[root@108 ~]# mongo
MongoDB shell version: 2.6.1
connecting to: test
Welcome to the MongoDB shell.
For interactive help, type "help".
For more comprehensive documentation, see
        http://docs.mongodb.org/
Questions? Try the support group
        http://groups.google.com/group/mongodb-user
> config = {
...      "_id": "dd",
...      "members": [
...          {"_id": 0, "host": "192.168.206.106:27017"},
...          {"_id": 1, "host": "192.168.206.107:27017"},
...          {"_id": 2, "host": "192.168.206.108:27017"},
...      ]
... }
{
        "_id" : "dd",
        "members" : [
                {
                        "_id" : 0,
                        "host" : "192.168.206.106:27017"
                },
                {
                        "_id" : 1,
                        "host" : "192.168.206.107:27017"
                },
                {
                        "_id" : 2,
                        "host" : "192.168.206.108:27017"
                }
        ]
}
> db = (new Mongo("192.168.206.106:27017")).getDB("test")
test
> rs.initiate(config)
{
        "info" : "Config now saved locally.  Should come online in about a minute.",
        "ok" : 1
}
> ▊




[root@106 ~]# mongo
MongoDB shell version: 2.6.1
connecting to: test
Welcome to the MongoDB shell.
For interactive help, type "help".
For more comprehensive documentation, see
        http://docs.mongodb.org/
Questions? Try the support group
        http://groups.google.com/group/mongodb-user
dd:PRIMARY> rs.status()
{
        "set" : "dd",
        "date" : ISODate("2014-07-28T08:16:08Z"),
        "myState" : 1,
        "members" : [
                {
                        "_id" : 0,
                        "name" : "192.168.206.106:27017",
                        "health" : 1,
                        "state" : 1,
                        "stateStr" : "PRIMARY",
                        "uptime" : 192,
                        "optime" : Timestamp(1406535343, 1),
                        "optimeDate" : ISODate("2014-07-28T08:15:43Z"),
                        "electionTime" : Timestamp(1406535352, 1),
                        "electionDate" : ISODate("2014-07-28T08:15:52Z"),
                        "self" : true
                },
                {
                        "_id" : 1,
                        "name" : "192.168.206.107:27017",
                        "health" : 1,
                        "state" : 2,
                        "stateStr" : "SECONDARY",
                        "uptime" : 24,
                        "optime" : Timestamp(1406535343, 1),
                        "optimeDate" : ISODate("2014-07-28T08:15:43Z"),
                        "lastHeartbeat" : ISODate("2014-07-28T08:16:08Z"),
                        "lastHeartbeatRecv" : ISODate("2014-07-28T08:16:08Z"),
                        "pingMs" : 0,
                        "syncingTo" : "192.168.206.106:27017"
                },
                {
                        "_id" : 2,
                        "name" : "192.168.206.108:27017",
                        "health" : 1,
                        "state" : 2,
                        "stateStr" : "SECONDARY",
                        "uptime" : 24,
                        "optime" : Timestamp(1406535343, 1),
                        "optimeDate" : ISODate("2014-07-28T08:15:43Z"),
                        "lastHeartbeat" : ISODate("2014-07-28T08:16:08Z"),
                        "lastHeartbeatRecv" : ISODate("2014-07-28T08:16:08Z"),
                        "pingMs" : 0,
                        "syncingTo" : "192.168.206.106:27017"
                }
        ],
        "ok" : 1
}
dd:PRIMARY> ▊




添加副本集到集群
mongos> sh.addShard("dd/192.168.206.106:27017")
{ "shardAdded" : "dd", "ok" : 1 }
mongos> ▊




查看集群状态
mongos> sh.status()
--- Sharding Status ---
  sharding version: {
        "_id" : 1,
        "version" : 4,
        "minCompatibleVersion" : 4,
        "currentVersion" : 5,
        "clusterId" : ObjectId("53d5c865d5c6355ca4b322c1")
}
  shards:
        {  "_id" : "cc",  "host" : "cc/192.168.206.103:27017,192.168.206.104:27017,192.168.206.105:27017" }
        {  "_id" : "dd",  "host" : "dd/192.168.206.106:27017,192.168.206.107:27017,192.168.206.108:27017" }
  databases:
        {  "_id" : "admin",  "partitioned" : false,  "primary" : "config" }
        {  "_id" : "cc",  "partitioned" : true,  "primary" : "cc" }
                cc.scan
                        shard key: { "_id" : 1 }
                        chunks:
                                dd      2
                                cc      11
                        { "_id" : { "$minKey" : 1 } } -->> { "_id" : ObjectId("53ba8013d6ca3f1f110037cc") } on : dd Timestamp(2, 0)
                        { "_id" : ObjectId("53ba8013d6ca3f1f110037cc") } -->> { "_id" : ObjectId("53ba801cd6ca3f1f1100613f") } on : dd Timestamp(3, 0)
                        { "_id" : ObjectId("53ba801cd6ca3f1f1100613f") } -->> { "_id" : ObjectId("53ba8022d6ca3f1f11007d41") } on : cc Timestamp(3, 1)
                        { "_id" : ObjectId("53ba8022d6ca3f1f11007d41") } -->> { "_id" : ObjectId("53ba8028d6ca3f1f11009943") } on : cc Timestamp(1, 3)
                        { "_id" : ObjectId("53ba8028d6ca3f1f11009943") } -->> { "_id" : ObjectId("53ba802dd6ca3f1f1100b545") } on : cc Timestamp(1, 4)
                        { "_id" : ObjectId("53ba802dd6ca3f1f1100b545") } -->> { "_id" : ObjectId("53ba8034d6ca3f1f1100d14b") } on : cc Timestamp(1, 5)
                        { "_id" : ObjectId("53ba8034d6ca3f1f1100d14b") } -->> { "_id" : ObjectId("53ba803ad6ca3f1f1100ed56") } on : cc Timestamp(1, 6)
                        { "_id" : ObjectId("53ba803ad6ca3f1f1100ed56") } -->> { "_id" : ObjectId("53ba8040d6ca3f1f11010958") } on : cc Timestamp(1, 7)
                        { "_id" : ObjectId("53ba8040d6ca3f1f11010958") } -->> { "_id" : ObjectId("53ba8046d6ca3f1f1101255a") } on : cc Timestamp(1, 8)
                        { "_id" : ObjectId("53ba8046d6ca3f1f1101255a") } -->> { "_id" : ObjectId("53ba804cd6ca3f1f1101415c") } on : cc Timestamp(1, 9)
                        { "_id" : ObjectId("53ba804cd6ca3f1f1101415c") } -->> { "_id" : ObjectId("53ba8053d6ca3f1f11015d5e") } on : cc Timestamp(1, 10)
                        { "_id" : ObjectId("53ba8053d6ca3f1f11015d5e") } -->> { "_id" : ObjectId("53ba8059d6ca3f1f11017960") } on : cc Timestamp(1, 11)
                        { "_id" : ObjectId("53ba8059d6ca3f1f11017960") } -->> { "_id" : { "$maxKey" : 1 } } on : cc Timestamp(1, 12)


mongos> ▊




过一段时间, 数据就均匀分布到所有的分片上了
mongos> sh.status()
--- Sharding Status ---
  sharding version: {
        "_id" : 1,
        "version" : 4,
        "minCompatibleVersion" : 4,
        "currentVersion" : 5,
        "clusterId" : ObjectId("53d5c865d5c6355ca4b322c1")
}
  shards:
        {  "_id" : "cc",  "host" : "cc/192.168.206.103:27017,192.168.206.104:27017,192.168.206.105:27017" }
        {  "_id" : "dd",  "host" : "dd/192.168.206.106:27017,192.168.206.107:27017,192.168.206.108:27017" }
  databases:
        {  "_id" : "admin",  "partitioned" : false,  "primary" : "config" }
        {  "_id" : "cc",  "partitioned" : true,  "primary" : "cc" }
                cc.scan
                        shard key: { "_id" : 1 }
                        chunks:
                                dd      6
                                cc      7
                        { "_id" : { "$minKey" : 1 } } -->> { "_id" : ObjectId("53ba8013d6ca3f1f110037cc") } on : dd Timestamp(2, 0)
                        { "_id" : ObjectId("53ba8013d6ca3f1f110037cc") } -->> { "_id" : ObjectId("53ba801cd6ca3f1f1100613f") } on : dd Timestamp(3, 0)
                        { "_id" : ObjectId("53ba801cd6ca3f1f1100613f") } -->> { "_id" : ObjectId("53ba8022d6ca3f1f11007d41") } on : dd Timestamp(4, 0)
                        { "_id" : ObjectId("53ba8022d6ca3f1f11007d41") } -->> { "_id" : ObjectId("53ba8028d6ca3f1f11009943") } on : dd Timestamp(5, 0)
                        { "_id" : ObjectId("53ba8028d6ca3f1f11009943") } -->> { "_id" : ObjectId("53ba802dd6ca3f1f1100b545") } on : dd Timestamp(6, 0)
                        { "_id" : ObjectId("53ba802dd6ca3f1f1100b545") } -->> { "_id" : ObjectId("53ba8034d6ca3f1f1100d14b") } on : dd Timestamp(7, 0)
                        { "_id" : ObjectId("53ba8034d6ca3f1f1100d14b") } -->> { "_id" : ObjectId("53ba803ad6ca3f1f1100ed56") } on : cc Timestamp(7, 1)
                        { "_id" : ObjectId("53ba803ad6ca3f1f1100ed56") } -->> { "_id" : ObjectId("53ba8040d6ca3f1f11010958") } on : cc Timestamp(1, 7)
                        { "_id" : ObjectId("53ba8040d6ca3f1f11010958") } -->> { "_id" : ObjectId("53ba8046d6ca3f1f1101255a") } on : cc Timestamp(1, 8)
                        { "_id" : ObjectId("53ba8046d6ca3f1f1101255a") } -->> { "_id" : ObjectId("53ba804cd6ca3f1f1101415c") } on : cc Timestamp(1, 9)
                        { "_id" : ObjectId("53ba804cd6ca3f1f1101415c") } -->> { "_id" : ObjectId("53ba8053d6ca3f1f11015d5e") } on : cc Timestamp(1, 10)
                        { "_id" : ObjectId("53ba8053d6ca3f1f11015d5e") } -->> { "_id" : ObjectId("53ba8059d6ca3f1f11017960") } on : cc Timestamp(1, 11)
                        { "_id" : ObjectId("53ba8059d6ca3f1f11017960") } -->> { "_id" : { "$maxKey" : 1 } } on : cc Timestamp(1, 12)


mongos> ▊




把192.168.206.103关掉(配置服务器和副本集1的一个服务器)
[root@103 ~]# init 0
[root@103 ~]# Connection to 192.168.206.103 closed by remote host.
Connection to 192.168.206.103 closed.
[root@101 ~]# ▊




但是当前倒入数据是没问题的(在办公电脑开8个虚拟机,速度比较慢)
[root@master1 ~]# mongoimport --host 192.168.0.172 --db cc --collection scan < domain.dat
connected to: 192.168.0.172
2014-07-28T16:35:38.546+0800                    100     33/second
2014-07-28T16:35:41.037+0800                    4300    716/second
2014-07-28T16:35:44.029+0800                    9200    1022/second
2014-07-28T16:35:49.969+0800                    14100   1007/second
2014-07-28T16:35:52.037+0800                    17500   1029/second
2014-07-28T16:35:55.007+0800                    22600   1130/second
2014-07-28T16:35:58.009+0800                    27300   1186/second
2014-07-28T16:36:01.631+0800                    28200   1084/second
2014-07-28T16:36:04.012+0800                    32200   1110/second
2014-07-28T16:36:07.072+0800                    37300   1165/second
2014-07-28T16:36:10.008+0800                    40500   1157/second
2014-07-28T16:36:13.903+0800                    41600   1094/second
2014-07-28T16:36:16.106+0800                    45300   1104/second





查询也是没问题的
mongos> db.scan.findOne({"_id" : "01.com"})
{
        "_id" : "01.com",
        "ip" : 3657779848,
}
mongos> ▊




到副本集1看一下副本集的状态, 可以看到192.168.206.103的health是0, 即当机了, 而第二台主机192.168.206.104当前为主服务器
[root@104 ~]# mongo
cc:PRIMARY> rs.status()
{
        "set" : "cc",
        "date" : ISODate("2014-07-28T08:44:59Z"),
        "myState" : 1,
        "members" : [
                {
                        "_id" : 0,
                        "name" : "192.168.206.103:27017",
                        "health" : 0,
                        "state" : 8,
                        "stateStr" : "(not reachable/healthy)",
                        "uptime" : 0,
                        "optime" : Timestamp(1406535676, 70),
                        "optimeDate" : ISODate("2014-07-28T08:21:16Z"),
                        "lastHeartbeat" : ISODate("2014-07-28T08:44:49Z"),
                        "lastHeartbeatRecv" : ISODate("2014-07-28T08:32:35Z"),
                        "pingMs" : 0,
                        "syncingTo" : "192.168.206.104:27017"
                },
                {
                        "_id" : 1,
                        "name" : "192.168.206.104:27017",
                        "health" : 1,
                        "state" : 1,
                        "stateStr" : "PRIMARY",
                        "uptime" : 8758,
                        "optime" : Timestamp(1406535676, 70),
                        "optimeDate" : ISODate("2014-07-28T08:21:16Z"),
                        "electionTime" : Timestamp(1406530391, 1),
                        "electionDate" : ISODate("2014-07-28T06:53:11Z"),
                        "self" : true
                },
                {
                        "_id" : 2,
                        "name" : "192.168.206.105:27017",
                        "health" : 1,
                        "state" : 2,
                        "stateStr" : "SECONDARY",
                        "uptime" : 8381,
                        "optime" : Timestamp(1406535676, 70),
                        "optimeDate" : ISODate("2014-07-28T08:21:16Z"),
                        "lastHeartbeat" : ISODate("2014-07-28T08:44:59Z"),
                        "lastHeartbeatRecv" : ISODate("2014-07-28T08:44:59Z"),
                        "pingMs" : 0,
                        "syncingTo" : "192.168.206.104:27017"
                }
        ],
        "ok" : 1
}
cc:PRIMARY> ▊




启动192.168.206.103, 在起服务之前把数据库文件都删掉
[root@103 ~]# cd /opt/mongodb/dbs/
[root@103 dbs]# rm * -rfv
removed `config.0'
removed `config.ns'
removed directory: `journal'
removed `local.0'
removed `local.ns'
removed `mongod.lock'
[root@103 dbs]# cd ..
[root@103 mongodb]# cd datadbs/
[root@103 datadbs]# rm * -rfv
removed `cc.0'
removed `cc.1'
removed `cc.2'
removed `cc.ns'
removed directory: `journal'
removed `local.0'
removed `local.1'
removed `local.ns'
removed `mongod.lock'
[root@103 datadbs]#
[root@103 datadbs]# ls
[root@103 datadbs]# cd
[root@103 ~]# ▊




启动mongodb, 用以前的配置
[root@103 ~]# cat start.sh
service iptables stop
setenforce 0
mongod --configsvr --dbpath /opt/mongodb/dbs/ &> /dev/null &
mongod --replSet cc --dbpath /opt/mongodb/datadbs/ &> /dev/null &
[root@103 ~]# ./start.sh
[root@103 ~]# ▊




可以看到它的状态是STARTUP2, 就是正在恢复啦
[root@103 ~]# mongo
MongoDB shell version: 2.6.1
connecting to: test
cc:STARTUP2> rs.status()
{
        "set" : "cc",
        "date" : ISODate("2014-07-28T09:21:09Z"),
        "myState" : 5,
        "members" : [
                {
                        "_id" : 0,
                        "name" : "192.168.206.103:27017",
                        "health" : 1,
                        "state" : 5,
                        "stateStr" : "STARTUP2",
                        "uptime" : 7,
                        "optime" : Timestamp(0, 0),
                        "optimeDate" : ISODate("1970-01-01T00:00:00Z"),
                        "infoMessage" : "initial sync need a member to be primary or secondary to do our initial sync",
                        "self" : true
                },
                {
                        "_id" : 1,
                        "name" : "192.168.206.104:27017",
                        "health" : 1,
                        "state" : 1,
                        "stateStr" : "PRIMARY",
                        "uptime" : 6,
                        "optime" : Timestamp(1406535676, 70),
                        "optimeDate" : ISODate("2014-07-28T08:21:16Z"),
                        "lastHeartbeat" : ISODate("2014-07-28T09:21:07Z"),
                        "lastHeartbeatRecv" : ISODate("2014-07-28T09:21:08Z"),
                        "pingMs" : 0,
                        "electionTime" : Timestamp(1406530391, 1),
                        "electionDate" : ISODate("2014-07-28T06:53:11Z")
                },
                {
                        "_id" : 2,
                        "name" : "192.168.206.105:27017",
                        "health" : 1,
                        "state" : 2,
                        "stateStr" : "SECONDARY",
                        "uptime" : 6,
                        "optime" : Timestamp(1406535676, 70),
                        "optimeDate" : ISODate("2014-07-28T08:21:16Z"),
                        "lastHeartbeat" : ISODate("2014-07-28T09:21:07Z"),
                        "lastHeartbeatRecv" : ISODate("2014-07-28T09:21:09Z"),
                        "pingMs" : 0,
                        "syncingTo" : "192.168.206.104:27017"
                }
        ],
        "ok" : 1
}
cc:STARTUP2> ▊




过一阵子再登陆看, 就是从服务器了, SECONDARY
[root@103 ~]# mongo
MongoDB shell version: 2.6.1
connecting to: test
cc:SECONDARY> rs.status()
{
        "set" : "cc",
        "date" : ISODate("2014-07-28T09:28:38Z"),
        "myState" : 2,
        "syncingTo" : "192.168.206.104:27017",
        "members" : [
                {
                        "_id" : 0,
                        "name" : "192.168.206.103:27017",
                        "health" : 1,
                        "state" : 2,
                        "stateStr" : "SECONDARY",
                        "uptime" : 456,
                        "optime" : Timestamp(1406535676, 70),
                        "optimeDate" : ISODate("2014-07-28T08:21:16Z"),
                        "self" : true
                },
                {
                        "_id" : 1,
                        "name" : "192.168.206.104:27017",
                        "health" : 1,
                        "state" : 1,
                        "stateStr" : "PRIMARY",
                        "uptime" : 455,
                        "optime" : Timestamp(1406535676, 70),
                        "optimeDate" : ISODate("2014-07-28T08:21:16Z"),
                        "lastHeartbeat" : ISODate("2014-07-28T09:28:37Z"),
                        "lastHeartbeatRecv" : ISODate("2014-07-28T09:28:36Z"),
                        "pingMs" : 0,
                        "electionTime" : Timestamp(1406530391, 1),
                        "electionDate" : ISODate("2014-07-28T06:53:11Z")
                },
                {
                        "_id" : 2,
                        "name" : "192.168.206.105:27017",
                        "health" : 1,
                        "state" : 2,
                        "stateStr" : "SECONDARY",
                        "uptime" : 455,
                        "optime" : Timestamp(1406535676, 70),
                        "optimeDate" : ISODate("2014-07-28T08:21:16Z"),
                        "lastHeartbeat" : ISODate("2014-07-28T09:28:37Z"),
                        "lastHeartbeatRecv" : ISODate("2014-07-28T09:28:37Z"),
                        "pingMs" : 0,
                        "syncingTo" : "192.168.206.104:27017"
                }
        ],
        "ok" : 1
}
cc:SECONDARY> ▊


回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|小黑屋|京峰教育,只为有梦想的人 ( 京ICP备15013173号 )

GMT+8, 2020-8-6 14:34 , Processed in 0.046818 second(s), 12 queries , Apc On.

快速回复 返回顶部 返回列表