四、shard1安装配置
4.1、创建目录
代码语言:javascript复制mkdir -p /work/mongo-cluster/shard1/{bin,data,conf,logs,keys}
4.2、编辑启动脚本
代码语言:javascript复制vim /work/mongo-cluster/shard1/bin/start.sh
内容
代码语言:javascript复制docker run -d --name shard1
--network=host
--log-opt max-size=10m
-v /etc/timezone:/etc/timezone
-v /etc/localtime:/etc/localtime
-v /work/mongo-cluster/shard1/data:/data
-v /work/mongo-cluster/shard1/keys:/keys
-v /work/mongo-cluster/shard1/logs:/var/log/mongodb
-v /work/mongo-cluster/shard1/conf/mongod.conf:/etc/mongo/mongod.conf
mongo:4.2.5 --config /etc/mongo/mongod.conf
代码语言:javascript复制chmod 755 /work/mongo-cluster/shard1/bin/start.sh
4.3、编辑配置文件
代码语言:javascript复制vim /work/mongo-cluster/shard1/conf/mongod.conf
内容
代码语言:javascript复制# mongod.conf
# for documentation of all options, see:
# http://docs.mongodb.org/manual/reference/configuration-options/
# Where and how to store data.
storage:
dbPath: /data
journal:
enabled: true
# engine:
# mmapv1:
# wiredTiger:
# where to write logging data.
systemLog:
destination: file
logAppend: true
path: /var/log/mongodb/mongo.log
# network interfaces
net:
port: 27019
bindIp: 0.0.0.0
# how the process runs
processManagement:
timeZoneInfo: /usr/share/zoneinfo
#security:
security:
keyFile: /keys/mongo-cluster.key
#operationProfiling:
#replication:
replication:
replSetName: shard1
#sharding:
sharding:
clusterRole: shardsvr
## Enterprise-Only Options:
#auditLog:
#snmp:
4.4、复制keyfile
代码语言:javascript复制cp ~/mongo-cluster.key /work/mongo-cluster/shard1/keys/
sudo chown 999 /work/mongo-cluster/shard1/keys/mongo-cluster.key
sudo chmod 400 /work/mongo-cluster/shard1/keys/mongo-cluster.key
4.5、设置目录权限
代码语言:javascript复制sudo chown 999 /work/mongo-cluster/shard1/data/ /work/mongo-cluster/shard1/logs/
4.6、启动应用
代码语言:javascript复制/work/mongo-cluster/shard1/bin/start.sh
4.7、初始化副本集
连接172.30.3.231节点
代码语言:javascript复制docker exec -it shard1 mongo --port 27019
初始化副本集
代码语言:javascript复制rs.initiate(
{
_id : "shard1",
members: [
{ _id : 0, host : "172.30.3.231:27019" },
{ _id : 1, host : "172.30.3.234:27019" },
{ _id : 2, host : "172.30.3.238:27019" , arbiterOnly: true }
]
}
)
返回值
代码语言:javascript复制{ "ok" : 1 }
4.8、查询状态
代码语言:javascript复制rs.status()
返回值
代码语言:javascript复制{
"set" : "shard1",
"date" : ISODate("2020-04-11T07:11:39.182Z"),
"myState" : 1,
"term" : NumberLong(1),
"syncingTo" : "",
"syncSourceHost" : "",
"syncSourceId" : -1,
"heartbeatIntervalMillis" : NumberLong(2000),
"majorityVoteCount" : 2,
"writeMajorityCount" : 2,
"optimes" : {
"lastCommittedOpTime" : {
"ts" : Timestamp(1586589098, 1),
"t" : NumberLong(1)
},
"lastCommittedWallTime" : ISODate("2020-04-11T07:11:38.642Z"),
"readConcernMajorityOpTime" : {
"ts" : Timestamp(1586589098, 1),
"t" : NumberLong(1)
},
"readConcernMajorityWallTime" : ISODate("2020-04-11T07:11:38.642Z"),
"appliedOpTime" : {
"ts" : Timestamp(1586589098, 1),
"t" : NumberLong(1)
},
"durableOpTime" : {
"ts" : Timestamp(1586589098, 1),
"t" : NumberLong(1)
},
"lastAppliedWallTime" : ISODate("2020-04-11T07:11:38.642Z"),
"lastDurableWallTime" : ISODate("2020-04-11T07:11:38.642Z")
},
"lastStableRecoveryTimestamp" : Timestamp(1586589088, 1),
"lastStableCheckpointTimestamp" : Timestamp(1586589088, 1),
"electionCandidateMetrics" : {
"lastElectionReason" : "electionTimeout",
"lastElectionDate" : ISODate("2020-04-11T07:10:28.612Z"),
"electionTerm" : NumberLong(1),
"lastCommittedOpTimeAtElection" : {
"ts" : Timestamp(0, 0),
"t" : NumberLong(-1)
},
"lastSeenOpTimeAtElection" : {
"ts" : Timestamp(1586589018, 1),
"t" : NumberLong(-1)
},
"numVotesNeeded" : 2,
"priorityAtElection" : 1,
"electionTimeoutMillis" : NumberLong(10000),
"numCatchUpOps" : NumberLong(0),
"newTermStartDate" : ISODate("2020-04-11T07:10:28.640Z"),
"wMajorityWriteAvailabilityDate" : ISODate("2020-04-11T07:10:29.302Z")
},
"members" : [
{
"_id" : 0,
"name" : "172.30.3.231:27019",
"health" : 1,
"state" : 1,
"stateStr" : "PRIMARY",
"uptime" : 555,
"optime" : {
"ts" : Timestamp(1586589098, 1),
"t" : NumberLong(1)
},
"optimeDate" : ISODate("2020-04-11T07:11:38Z"),
"syncingTo" : "",
"syncSourceHost" : "",
"syncSourceId" : -1,
"infoMessage" : "could not find member to sync from",
"electionTime" : Timestamp(1586589028, 1),
"electionDate" : ISODate("2020-04-11T07:10:28Z"),
"configVersion" : 1,
"self" : true,
"lastHeartbeatMessage" : ""
},
{
"_id" : 1,
"name" : "172.30.3.234:27019",
"health" : 1,
"state" : 2,
"stateStr" : "SECONDARY",
"uptime" : 80,
"optime" : {
"ts" : Timestamp(1586589088, 1),
"t" : NumberLong(1)
},
"optimeDurable" : {
"ts" : Timestamp(1586589088, 1),
"t" : NumberLong(1)
},
"optimeDate" : ISODate("2020-04-11T07:11:28Z"),
"optimeDurableDate" : ISODate("2020-04-11T07:11:28Z"),
"lastHeartbeat" : ISODate("2020-04-11T07:11:38.619Z"),
"lastHeartbeatRecv" : ISODate("2020-04-11T07:11:37.274Z"),
"pingMs" : NumberLong(0),
"lastHeartbeatMessage" : "",
"syncingTo" : "172.30.3.231:27019",
"syncSourceHost" : "172.30.3.231:27019",
"syncSourceId" : 0,
"infoMessage" : "",
"configVersion" : 1
},
{
"_id" : 2,
"name" : "172.30.3.238:27019",
"health" : 1,
"state" : 7,
"stateStr" : "ARBITER",
"uptime" : 80,
"lastHeartbeat" : ISODate("2020-04-11T07:11:38.618Z"),
"lastHeartbeatRecv" : ISODate("2020-04-11T07:11:38.886Z"),
"pingMs" : NumberLong(0),
"lastHeartbeatMessage" : "",
"syncingTo" : "",
"syncSourceHost" : "",
"syncSourceId" : -1,
"infoMessage" : "",
"configVersion" : 1
}
],
"ok" : 1
}
4.9、创建shard本地管理用户
该用户在集群中不可见,用于管理shard节点
代码语言:javascript复制admin = db.getSiblingDB("admin")
admin.createUser(
{
user: "shard1admin",
pwd: "q1w2e3r4",
roles: [ { role: "root", db: "admin" } ]
}
)
返回值类似
代码语言:javascript复制Successfully added user: {
"user" : "shard1admin",
"roles" : [
{
"role" : "root",
"db" : "admin"
}
]
}