操作系统:CentOS 6 x86_64
MongoDB版本:3.4.3
集群主机拓扑:
主机 mongo shardsvr & ReplSetName mongo configsvr & ReplSetName mongostest1.lan shard-a shard-b
test2.lan shard-a shard-b
test3.lan shard-a shard-b
test4.lan cfgshard
test5.lan cfgshard
test6.lan cfgshard
test7.lan yes
test1-3 分别在一台主机上启动两个不同副本集名称的mongod实例。
test4-6 三台主机作为 config server 单独运行。
test7 主机作为 mongos 路由主机。
安装 MongoDB
配置 repo 源
1234567 [mongodb-org-3.4]
name=MongoDB Repository
#baseurl=https://repo.mongodb.org/yum/RedHat//mongodb-org/3.4/x86_64/
baseurl=https://mirrors.aliyun.com/mongodb/yum/redhat/$releasever/mongodb-org/3.4/x86_64/
gpgcheck=0
enabled=1
gpgkey=https://www.mongodb.org/static/pgp/server-3.4.asc
选择国内 阿里云 镜像资源。
# yum install mongodb-org -y
配置 /etc/mongod.conf
# mongod.conf
# for documentation of all options, see:
#
# where to write logging data.
systemLog:
destination: file
logAppend: true
path: /var/log/mongodb/mongod.log
# Where and how to store data.
storage:
dbPath: /var/lib/mongo
journal:
enabled: true
# engine:
# mmapv1:
# wiredTiger:
# how the process runs
processManagement:
fork: true # fork and run in background
pidFilePath: /var/run/mongodb/mongod.pid # location of pidfile
# network interfaces
net:
port: 27017
bindIp: 0.0.0.0 # Listen to local interface only, comment to listen on all interfaces.
#security:
#operationProfiling:
replication:
replSetName: shard-a
sharding:
clusterRole: shardsvr
## Enterprise-Only Options
#auditLog:
#snmp:
replication 处配置 副本集 名,sharding 开启 shardsvr 模式。
启动 mongod 服务
[root@test1 ~]# service mongod start
Starting mongod: [ OK ]
[root@test2 ~]# service mongod start
Starting mongod: [ OK ]
[root@test3 ~]# service mongod start
Starting mongod: [ OK ]
配置 shard-a 副本集
[root@test1 ~]# mongo test1.lan:27017
MongoDB shell version v3.4.3
connecting to: test1.lan:27017
MongoDB server version: 3.4.3
Server has startup warnings:
2017-04-24T22:46:19.703+0800 I STORAGE [initandlisten]
2017-04-24T22:46:19.703+0800 I STORAGE [initandlisten] ** WARNING: Using the XFS filesystem is strongly recommended with the WiredTiger storage engine
2017-04-24T22:46:19.703+0800 I STORAGE [initandlisten] ** See
2017-04-24T22:46:20.321+0800 I CONTROL [initandlisten]
> rs.initiate()
{
"info2" : "no configuration specified. Using a default configuration for the set",
"me" : "test1.lan:27017",
"ok" : 1
}
shard-a:SECONDARY>
shard-a:PRIMARY> config = rs.config() # 保存配置对象
{
"_id" : "shard-a",
"version" : 1,
"protocolVersion" : NumberLong(1),
"members" : [
{
"_id" : 0,
"host" : "test1.lan:27017",
"arbiterOnly" : false,
"buildIndexes" : true,
"hidden" : false,
"priority" : 1,
"tags" : {
},
"slaveDelay" : NumberLong(0),
"votes" : 1
}
],
"settings" : {
"chainingAllowed" : true,
"heartbeatIntervalMillis" : 2000,
"heartbeatTimeoutSecs" : 10,
"electionTimeoutMillis" : 10000,
"catchUpTimeoutMillis" : 2000,
"getLastErrorModes" : {
},
"getLastErrorDefaults" : {
"w" : 1,
"wtimeout" : 0
},
"replicaSetId" : ObjectId("58fe111823612a418eb7f3fc")
}
}
shard-a:PRIMARY> config.members[0].priority = 2 # 这里增加自身主机的优先级为 2,防止后面 PRIMARY 重新选举到其余主机
2
shard-a:PRIMARY> rs.reconfig(config) # 重新应用该配置
{ "ok" : 1 }
shard-a:PRIMARY> rs.add("test2.lan:27017") # 添加副本集主机
{ "ok" : 1 }
shard-a:PRIMARY> rs.add("test3.lan") # 添加副本集主机(默认端口为 27017)
{ "ok" : 1 }
shard-a:PRIMARY> rs.config()
{
"_id" : "shard-a",
"version" : 4,
"protocolVersion" : NumberLong(1),
"members" : [
{
"_id" : 0,
"host" : "test1.lan:27017",
"arbiterOnly" : false,
"buildIndexes" : true,
"hidden" : false,
"priority" : 2,
"tags" : {
},
"slaveDelay" : NumberLong(0),
"votes" : 1
},
{
"_id" : 1,
"host" : "test2.lan:27017",
"arbiterOnly" : false,
"buildIndexes" : true,
"hidden" : false,
"priority" : 1,
"tags" : {
},
"slaveDelay" : NumberLong(0),
"votes" : 1
},
{
"_id" : 2,
"host" : "test3.lan:27017",
"arbiterOnly" : false,
"buildIndexes" : true,
"hidden" : false,
"priority" : 1,
"tags" : {
},
"slaveDelay" : NumberLong(0),
"votes" : 1
}
],
"settings" : {
"chainingAllowed" : true,
"heartbeatIntervalMillis" : 2000,
"heartbeatTimeoutSecs" : 10,
"electionTimeoutMillis" : 10000,
"catchUpTimeoutMillis" : 2000,
"getLastErrorModes" : {
},
"getLastErrorDefaults" : {
"w" : 1,
"wtimeout" : 0
},
"replicaSetId" : ObjectId("58fe111823612a418eb7f3fc")
}
}
这样,副本集 shard-a 就配置完成
接下来我们启动并配置副本集 shard-b