案例演示:
一、在linux环境下,在一台机器上创建3个节点,创建一个测试为目的的副本集
1.首先创建3个不同的数据存储文件夹
[root@wugenqiang db]# cd /usr/local/mongodb/data/db [root@wugenqiang db]# mkdir rs0-0 [root@wugenqiang db]# mkdir rs0-1 [root@wugenqiang db]# mkdir rs0-2 [root@wugenqiang db]# ls rs0-0 rs0-1 rs0-2
2.分别打开3个终端,启动3个mongod服务
[root@wugenqiang db]# mongod --port 27017 --dbpath=rs0-0 -replSet rs0
[root@wugenqiang db]# mongod --port 27018 --dbpath=rs0-1 -replSet rs0
[root@wugenqiang db]# mongod --port 27019 --dbpath=rs0-2 -replSet rs0
3.打开第4个终端,验证mongod服务是否启动成功
二、集群初始化
1.启动客户端,登入一个机器
[root@wugenqiang ~]# mongo --port 27017
2.初始化集群
> rs.initiate() { "info2" : "no configuration specified. Using a default configuration for the set", "me" : "wugenqiang.bigdata:27017", "ok" : 1 }
3.为集群增加节点
rs0:SECONDARY> rs.add("wugenqiang.bigdata:27018") { "ok" : 1 } rs0:PRIMARY> rs.add("wugenqiang.bigdata:27019") { "ok" : 1 }
操作时请将wugenqiang.bigdata换成您自己的真实主机名hostname
4.查看集群配置信息
rs0:PRIMARY> rs.conf() { "_id" : "rs0", "version" : 3, "protocolVersion" : NumberLong(1), "members" : [ { "_id" : 0, "host" : "wugenqiang.bigdata:27017", "arbiterOnly" : false, "buildIndexes" : true, "hidden" : false, "priority" : 1, "tags" : { }, "slaveDelay" : NumberLong(0), "votes" : 1 }, { "_id" : 1, "host" : "wugenqiang.bigdata:27018", "arbiterOnly" : false, "buildIndexes" : true, "hidden" : false, "priority" : 1, "tags" : { }, "slaveDelay" : NumberLong(0), "votes" : 1 }, { "_id" : 2, "host" : "wugenqiang.bigdata:27019", "arbiterOnly" : false, "buildIndexes" : true, "hidden" : false, "priority" : 1, "tags" : { }, "slaveDelay" : NumberLong(0), "votes" : 1 } ], "settings" : { "chainingAllowed" : true, "heartbeatIntervalMillis" : 2000, "heartbeatTimeoutSecs" : 10, "electionTimeoutMillis" : 10000, "catchUpTimeoutMillis" : 60000, "getLastErrorModes" : { }, "getLastErrorDefaults" : { "w" : 1, "wtimeout" : 0 }, "replicaSetId" : ObjectId("5b48831fbcf017394d5a56d4") } }
5.查看集群状态
rs0:PRIMARY> rs.status function () { return db._adminCommand("replSetGetStatus"); }
至此创建副本集群以及初始化完成