在任意一个节点执行初始化服务。
[root@hdp06 ~]# mongod --port 27019 > rs.initiate({_id:"myShard_1",members:[{_id:1,host:"hdp06.thinkjoy.tt:27019",priority:2},{_id:2,host:"hdp07.thinkjoy.tt:27019"},{_id:3,host:"hdp08.thinkjoy.tt:27019"}]}) { "ok" : 1, "operationTime" : Timestamp(1534818896, 1), "$clusterTime" : { "clusterTime" : Timestamp(1534818896, 1), "signature" : { "hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="), "keyId" : NumberLong(0) } } } [root@hdp06 ~]# mongod --port 27020 > rs.initiate({_id:"myShard_2",members:[{_id:1,host:"hdp06.thinkjoy.tt:27020",priority:2},{_id:2,host:"hdp07.thinkjoy.tt:27020"},{_id:3,host:"hdp08.thinkjoy.tt:27020"}]}) { "operationTime" : Timestamp(1534818908, 2), "ok" : 0, "errmsg" : "already initialized", "code" : 23, "codeName" : "AlreadyInitialized", "$clusterTime" : { "clusterTime" : Timestamp(1534818908, 2), "signature" : { "hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="), "keyId" : NumberLong(0) } } } [root@hdp06 ~]# mongod --port 27021 > rs.initiate({_id:"myShard_3",members:[{_id:1,host:"hdp06.thinkjoy.tt:27021",priority:2},{_id:2,host:"hdp07.thinkjoy.tt:27021"},{_id:3,host:"hdp08.thinkjoy.tt:27021"}]}) { "operationTime" : Timestamp(1534818928, 1), "ok" : 0, "errmsg" : "already initialized", "code" : 23, "codeName" : "AlreadyInitialized", "$clusterTime" : { "clusterTime" : Timestamp(1534818928, 1), "signature" : { "hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="), "keyId" : NumberLong(0) } } } 4、配置Mongos服务 [root@hdp06 ~]# vi /data/mongo/router/mongod.router net: bindIp: 0.0.0.0 port: 27018 processManagement: fork: "true" sharding: configDB: configRS/hdp06.thinkjoy.tt:27017,hdp07.thinkjoy.tt:27017,hdp08.thinkjoy.tt:27017 systemLog: destination: file path: /data/mongo/router/logs/mongo_router.log [root@hdp06 ~]# scp /data/mongo/router/mongod.router hdp07:/data/mongo/router [root@hdp06 ~]# scp /data/mongo/router/mongod.router hdp08:/data/mongo/router [root@hdp06 ~]# mongos -f /data/mongo/router/mongod.router [root@hdp07 ~]# mongos -f /data/mongo/router/mongod.router [root@hdp08 ~]# mongos -f /data/mongo/router/mongod.router配置分片,将主片添加至集群,如下:
[root@hdp06 ~]# mongo --port 27018 mongos> show dbs admin 0.000GB config 0.000GB mongos> use admin switched to db admin mongos> db.runCommand({"addShard":"myShard_1/hdp06.thinkjoy.tt:27019" ,"maxsize":1024}) { "shardAdded" : "myShard_1", "ok" : 1, "operationTime" : Timestamp(1534819816, 6), "$clusterTime" : { "clusterTime" : Timestamp(1534819816, 6), "signature" : { "hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="), "keyId" : NumberLong(0) } } } mongos> db.runCommand({"addShard":"myShard_2/hdp06.thinkjoy.tt:27020" ,"maxsize":1024}) { "shardAdded" : "myShard_2", "ok" : 1, "operationTime" : Timestamp(1534819823, 5), "$clusterTime" : { "clusterTime" : Timestamp(1534819823, 5), "signature" : { "hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="), "keyId" : NumberLong(0) } } } mongos> db.runCommand({"addShard":"myShard_3/hdp06.thinkjoy.tt:27021" ,"maxsize":1024}) { "shardAdded" : "myShard_3", "ok" : 1, "operationTime" : Timestamp(1534819830, 5), "$clusterTime" : { "clusterTime" : Timestamp(1534819830, 5), "signature" : { "hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="), "keyId" : NumberLong(0) } }