MongoDB分片集群部署(四)

2022-02-24 14:54:15 浏览数 (1)

五、shard2安装配置
5.1、创建目录
代码语言:javascript复制
mkdir -p /work/mongo-cluster/shard2/{bin,data,conf,logs,keys}
5.2、编辑启动脚本
代码语言:javascript复制
vim /work/mongo-cluster/shard2/bin/start.sh
代码语言:javascript复制
docker run -d --name shard2 
        --network=host 
        --log-opt max-size=10m 
        -v /etc/timezone:/etc/timezone 
        -v /etc/localtime:/etc/localtime 
        -v /work/mongo-cluster/shard2/data:/data 
        -v /work/mongo-cluster/shard2/keys:/keys 
        -v /work/mongo-cluster/shard2/logs:/var/log/mongodb 
        -v /work/mongo-cluster/shard2/conf/mongod.conf:/etc/mongo/mongod.conf 
        mongo:4.2.5 --config /etc/mongo/mongod.conf 
代码语言:javascript复制
chmod 755  /work/mongo-cluster/shard2/bin/start.sh
5.3、编辑配置文件
代码语言:javascript复制
vim /work/mongo-cluster/shard2/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: 27020
  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: shard2
​
#sharding:
sharding:
    clusterRole: shardsvr
​
## Enterprise-Only Options:
​
#auditLog:
​
#snmp:
5.4、复制keyfile
代码语言:javascript复制
cp ~/mongo-cluster.key /work/mongo-cluster/shard2/keys/
sudo chown 999 /work/mongo-cluster/shard2/keys/mongo-cluster.key
sudo chmod 400 /work/mongo-cluster/shard2/keys/mongo-cluster.key
5.5、设置目录权限
代码语言:javascript复制
sudo chown 999 /work/mongo-cluster/shard2/data/ /work/mongo-cluster/shard2/logs/
5.6、启动应用
代码语言:javascript复制
/work/mongo-cluster/shard2/bin/start.sh
5.7、初始化副本集

连接172.30.3.234节点

代码语言:javascript复制
docker exec -it shard2 mongo --port 27020

初始化副本集

代码语言:javascript复制
rs.initiate(
  {
    _id : "shard2",
    members: [
      { _id : 0, host : "172.30.3.231:27020" , arbiterOnly: true },
      { _id : 1, host : "172.30.3.234:27020" },
      { _id : 2, host : "172.30.3.238:27020" }
    ]
  }
)

返回值

代码语言:javascript复制
{ "ok" : 1 }
5.8、查询状态
代码语言:javascript复制
rs.status()

返回值

代码语言:javascript复制
{
        "set" : "shard2",
        "date" : ISODate("2020-04-11T07:18:38.972Z"),
        "myState" : 1,
        "term" : NumberLong(1),
        "syncingTo" : "",
        "syncSourceHost" : "",
        "syncSourceId" : -1,
        "heartbeatIntervalMillis" : NumberLong(2000),
        "majorityVoteCount" : 2,
        "writeMajorityCount" : 2,
        "optimes" : {
                "lastCommittedOpTime" : {
                        "ts" : Timestamp(0, 0),
                        "t" : NumberLong(-1)
                },
                "lastCommittedWallTime" : ISODate("1970-01-01T00:00:00Z"),
                "appliedOpTime" : {
                        "ts" : Timestamp(1586589518, 3),
                        "t" : NumberLong(1)
                },
                "durableOpTime" : {
                        "ts" : Timestamp(1586589518, 3),
                        "t" : NumberLong(1)
                },
                "lastAppliedWallTime" : ISODate("2020-04-11T07:18:38.648Z"),
                "lastDurableWallTime" : ISODate("2020-04-11T07:18:38.648Z")
        },
        "lastStableRecoveryTimestamp" : Timestamp(0, 0),
        "lastStableCheckpointTimestamp" : Timestamp(0, 0),
        "electionCandidateMetrics" : {
                "lastElectionReason" : "electionTimeout",
                "lastElectionDate" : ISODate("2020-04-11T07:18:38.530Z"),
                "electionTerm" : NumberLong(1),
                "lastCommittedOpTimeAtElection" : {
                        "ts" : Timestamp(0, 0),
                        "t" : NumberLong(-1)
                },
                "lastSeenOpTimeAtElection" : {
                        "ts" : Timestamp(1586589508, 1),
                        "t" : NumberLong(-1)
                },
                "numVotesNeeded" : 2,
                "priorityAtElection" : 1,
                "electionTimeoutMillis" : NumberLong(10000),
                "numCatchUpOps" : NumberLong(0),
                "newTermStartDate" : ISODate("2020-04-11T07:18:38.648Z")
        },
        "members" : [
                {
                        "_id" : 0,
                        "name" : "172.30.3.231:27020",
                        "health" : 1,
                        "state" : 7,
                        "stateStr" : "ARBITER",
                        "uptime" : 10,
                        "lastHeartbeat" : ISODate("2020-04-11T07:18:38.537Z"),
                        "lastHeartbeatRecv" : ISODate("2020-04-11T07:18:38.872Z"),
                        "pingMs" : NumberLong(0),
                        "lastHeartbeatMessage" : "",
                        "syncingTo" : "",
                        "syncSourceHost" : "",
                        "syncSourceId" : -1,
                        "infoMessage" : "",
                        "configVersion" : 1
                },
                {
                        "_id" : 1,
                        "name" : "172.30.3.234:27020",
                        "health" : 1,
                        "state" : 1,
                        "stateStr" : "PRIMARY",
                        "uptime" : 62,
                        "optime" : {
                                "ts" : Timestamp(1586589518, 3),
                                "t" : NumberLong(1)
                        },
                        "optimeDate" : ISODate("2020-04-11T07:18:38Z"),
                        "syncingTo" : "",
                        "syncSourceHost" : "",
                        "syncSourceId" : -1,
                        "infoMessage" : "could not find member to sync from",
                        "electionTime" : Timestamp(1586589518, 1),
                        "electionDate" : ISODate("2020-04-11T07:18:38Z"),
                        "configVersion" : 1,
                        "self" : true,
                        "lastHeartbeatMessage" : ""
                },
                {
                        "_id" : 2,
                        "name" : "172.30.3.238:27020",
                        "health" : 1,
                        "state" : 2,
                        "stateStr" : "SECONDARY",
                        "uptime" : 10,
                        "optime" : {
                                "ts" : Timestamp(1586589508, 1),
                                "t" : NumberLong(-1)
                        },
                        "optimeDurable" : {
                                "ts" : Timestamp(1586589508, 1),
                                "t" : NumberLong(-1)
                        },
                        "optimeDate" : ISODate("2020-04-11T07:18:28Z"),
                        "optimeDurableDate" : ISODate("2020-04-11T07:18:28Z"),
                        "lastHeartbeat" : ISODate("2020-04-11T07:18:38.618Z"),
                        "lastHeartbeatRecv" : ISODate("2020-04-11T07:18:38.802Z"),
                        "pingMs" : NumberLong(0),
                        "lastHeartbeatMessage" : "",
                        "syncingTo" : "",
                        "syncSourceHost" : "",
                        "syncSourceId" : -1,
                        "infoMessage" : "",
                        "configVersion" : 1
                }
        ],
        "ok" : 1
}
5.9、创建shard本地管理用户

该用户在集群中不可见,用于管理shard节点

代码语言:javascript复制
admin = db.getSiblingDB("admin")
admin.createUser(
  {
    user: "shard2admin",
    pwd: "q1w2e3r4", 
    roles: [ { role: "root", db: "admin" } ]
  }
)

返回值类似

代码语言:javascript复制
Successfully added user: {
        "user" : "shard2admin",
        "roles" : [
                {
                        "role" : "root",
                        "db" : "admin"
                }
        ]
}

0 人点赞