天天看点

MongoDB集群部署安装部署测试日常维护命令

安装部署

部署3分片的Mongo集群,下面是架构图,实际环境每个分片最好不要放在同一台机器,不同分片的主从也尽量分散,否则数据库压力大的时候磁盘会成为瓶颈,下面的安装示例仅为学习交流。
MongoDB集群部署安装部署测试日常维护命令

每个节点创建用户

groupadd mongo
useradd -g mongo mongo
echo "mongo"|passwd --stdin mongo
           

每个节点解压安装并配置环境变量

# tar -zxvf mongodb-linux-x86_64-rhel62-4.2.1.tgz 
mongodb-linux-x86_64-rhel62-4.2.1/THIRD-PARTY-NOTICES.gotools
mongodb-linux-x86_64-rhel62-4.2.1/README
mongodb-linux-x86_64-rhel62-4.2.1/THIRD-PARTY-NOTICES
mongodb-linux-x86_64-rhel62-4.2.1/MPL-2
mongodb-linux-x86_64-rhel62-4.2.1/LICENSE-Community.txt
mongodb-linux-x86_64-rhel62-4.2.1/bin/mongodump
mongodb-linux-x86_64-rhel62-4.2.1/bin/mongorestore
mongodb-linux-x86_64-rhel62-4.2.1/bin/mongoexport
mongodb-linux-x86_64-rhel62-4.2.1/bin/mongoimport
mongodb-linux-x86_64-rhel62-4.2.1/bin/mongostat
mongodb-linux-x86_64-rhel62-4.2.1/bin/mongotop
mongodb-linux-x86_64-rhel62-4.2.1/bin/bsondump
mongodb-linux-x86_64-rhel62-4.2.1/bin/mongofiles
mongodb-linux-x86_64-rhel62-4.2.1/bin/mongoreplay
mongodb-linux-x86_64-rhel62-4.2.1/bin/mongod
mongodb-linux-x86_64-rhel62-4.2.1/bin/mongos
mongodb-linux-x86_64-rhel62-4.2.1/bin/mongo
mongodb-linux-x86_64-rhel62-4.2.1/bin/install_compass
# vi /etc/profile
PATH=/usr/local/mongodb/bin/
# mv mongodb-linux-x86_64-rhel62-4.2.1 /usr/local/mongodb
# source /etc/profile
# mongod -version
db version v4.2.1
git version: edf6d45851c0b9ee15548f0f847df141764a317e
OpenSSL version: OpenSSL 1.0.1e-fips 11 Feb 2013
allocator: tcmalloc
modules: none
build environment:
    distmod: rhel62
    distarch: x86_64
    target_arch: x86_64
           

每个节点创建目录

mkdir -p /mongodb/conf
mkdir -p /mongodb/mongos/log
mkdir -p /mongodb/config/data
mkdir -p /mongodb/config/log
mkdir -p /mongodb/shard1/data
mkdir -p /mongodb/shard1/log
mkdir -p /mongodb/shard2/data
mkdir -p /mongodb/shard2/log
mkdir -p /mongodb/shard3/data
mkdir -p /mongodb/shard3/log
chown -R mongo:mongo /mongodb
chmod -R 755 /mongodb
           

编辑配置文件

config配置文件

cat config.conf 
systemLog:
  destination: file
  path: /mongodb/config/log/configsrv.log
  logAppend: true
processManagement:
  fork: true
  pidFilePath: /mongodb/config/log/configsrv.pid
storage:
  dbPath: /mongodb/config/data
  journal:
    enabled: true
net:
  bindIp: localhost, 192.168.62.41
  port: 20011
security:
  keyFile: /mongodb/secret
  authorization: enabled
sharding:
  clusterRole: configsvr
replication:
  replSetName: configs
           

mongos配置文件

cat mongos.conf 
port=20012
configdb=configs/192.168.62.41:20011,192.168.64.42:20011,192.168.64.43:20011  ###不能出现空格,
logpath=/mongodb/mongos/log/mongos.log
fork=true
logappend=true
keyFile=/mongodb/secret
clusterAuthMode=keyFile
#authorization=enabled
           

shard1配置文件

cat shard1.conf 
systemLog:
  destination: file
  path: /mongodb/shard1/log/shard1.log
  logAppend: true
processManagement:
  fork: true
  pidFilePath: /mongodb/shard1/log/shard1.pid
storage:
  dbPath: /mongodb/shard1/data
  journal:
    enabled: true
net:
  bindIp: localhost, 192.168.62.41
  port: 20013
security:
  keyFile: /mongodb/secret
  authorization: enabled
sharding:
  clusterRole: shardsvr
replication:
  replSetName: shard1
           

shard2配置文件

cat shard2.conf 
systemLog:
  destination: file
  path: /mongodb/shard2/log/shard2.log
  logAppend: true
processManagement:
  fork: true
  pidFilePath: /mongodb/shard2/log/shard2.pid
storage:
  dbPath: /mongodb/shard2/data
  journal:
    enabled: true
net:
  bindIp: localhost, 192.168.62.41
  port: 20014
security:
  keyFile: /mongodb/secret
  authorization: enabled
sharding:
  clusterRole: shardsvr
replication:
  replSetName: shard2
           

shard3配置文件

cat shard3.conf 
systemLog:
  destination: file
  path: /mongodb/shard3/log/shard3.log
  logAppend: true
processManagement:
  fork: true
  pidFilePath: /mongodb/shard3/log/shard3.pid
storage:
  dbPath: /mongodb/shard3/data
  journal:
    enabled: true
net:
  bindIp: localhost, 192.168.62.41
  port: 20015
security:
  keyFile: /mongodb/secret
  authorization: enabled
sharding:
  clusterRole: shardsvr
replication:
  replSetName: shard3
           

传输配置文件到其其他节点

节点2

scp *.conf 192.168.62.41:`pwd`

sed -i 's/192.168.62.41/192.168.62.42/g' config.conf
sed -i 's/192.168.62.41/192.168.62.42/g' shard1.conf 
sed -i 's/192.168.62.41/192.168.62.42/g' shard2.conf 
sed -i 's/192.168.62.41/192.168.62.42/g' shard3.conf 
           

节点3

scp *.conf 192.168.62.41:`pwd`

sed -i 's/192.168.62.41/192.168.62.43/g' config.conf
sed -i 's/192.168.62.41/192.168.62.43/g' shard1.conf 
sed -i 's/192.168.62.41/192.168.62.43/g' shard2.conf 
sed -i 's/192.168.62.41/192.168.62.43/g' shard3.conf 
           

创建密码文件

创建密码文件并传输到其他节点
openssl rand -base64 666 > /mongodb/secret
chmod 400 /mongodb/secret 
scp /mongodb/secret 192.168.62.42:`pwd` 
scp /mongodb/secret 192.168.62.43:`pwd`
           

启动服务

三个节点启动服务
mongod -f /mongodb/conf/shard1.conf
mongod -f /mongodb/conf/shard2.conf
mongod -f /mongodb/conf/shard3.conf
mongod -f /mongodb/conf/config.conf
           

初始化副本集

config

节点1操作,初始化config
mongo --port 20011 //config server
use admin
rs.initiate({_id:"configs",members:[{ _id:0, host:"192.168.62.41:20011",priority:3},{ _id:1, host:" 192.168.62.42:20011",priority:2},{ _id:2, host:" 192.168.62.43:20011",priority:1}]})
rs.status()
{
        "set" : "configs",
        "date" : ISODate("2020-09-20T12:26:13.392Z"),
        "myState" : 2,
        "term" : NumberLong(0),
        "syncingTo" : "",
        "syncSourceHost" : "",
        "syncSourceId" : -1,
        "configsvr" : true,
        "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(1600604767, 1),
                        "t" : NumberLong(-1)
                },
                "durableOpTime" : {
                        "ts" : Timestamp(1600604767, 1),
                        "t" : NumberLong(-1)
                },
                "lastAppliedWallTime" : ISODate("2020-09-20T12:26:07.395Z"),
                "lastDurableWallTime" : ISODate("2020-09-20T12:26:07.395Z")
        },
        "lastStableRecoveryTimestamp" : Timestamp(0, 0),
        "lastStableCheckpointTimestamp" : Timestamp(0, 0),
        "members" : [
                {
                        "_id" : 0,
                        "name" : "192.168.62.41:20011",
                        "ip" : "192.168.62.41",
                        "health" : 1,
                        "state" : 2,
                        "stateStr" : "SECONDARY",
                        "uptime" : 182294,
                        "optime" : {
                                "ts" : Timestamp(1600604767, 1),
                                "t" : NumberLong(-1)
                        },
                        "optimeDate" : ISODate("2020-09-20T12:26:07Z"),
                        "syncingTo" : "",
                        "syncSourceHost" : "",
                        "syncSourceId" : -1,
                        "infoMessage" : "could not find member to sync from",
                        "configVersion" : 1,
                        "self" : true,
                        "lastHeartbeatMessage" : ""
                },
                {
                        "_id" : 1,
                        "name" : "192.168.62.42:20011",
                        "ip" : "192.168.62.42",
                        "health" : 1,
                        "state" : 2,
                        "stateStr" : "SECONDARY",
                        "uptime" : 5,
                        "optime" : {
                                "ts" : Timestamp(1600604767, 1),
                                "t" : NumberLong(-1)
                        },
                        "optimeDurable" : {
                                "ts" : Timestamp(1600604767, 1),
                                "t" : NumberLong(-1)
                        },
                        "optimeDate" : ISODate("2020-09-20T12:26:07Z"),
                        "optimeDurableDate" : ISODate("2020-09-20T12:26:07Z"),
                        "lastHeartbeat" : ISODate("2020-09-20T12:26:13.044Z"),
                        "lastHeartbeatRecv" : ISODate("2020-09-20T12:26:13.108Z"),
                        "pingMs" : NumberLong(0),
                        "lastHeartbeatMessage" : "",
                        "syncingTo" : "",
                        "syncSourceHost" : "",
                        "syncSourceId" : -1,
                        "infoMessage" : "",
                        "configVersion" : 1
                },
                {
                        "_id" : 2,
                        "name" : "192.168.62.43:20011",
                        "ip" : "192.168.62.43",
                        "health" : 1,
                        "state" : 2,
                        "stateStr" : "SECONDARY",
                        "uptime" : 5,
                        "optime" : {
                                "ts" : Timestamp(1600604767, 1),
                                "t" : NumberLong(-1)
                        },
                        "optimeDurable" : {
                                "ts" : Timestamp(1600604767, 1),
                                "t" : NumberLong(-1)
                        },
                        "optimeDate" : ISODate("2020-09-20T12:26:07Z"),
                        "optimeDurableDate" : ISODate("2020-09-20T12:26:07Z"),
                        "lastHeartbeat" : ISODate("2020-09-20T12:26:13.044Z"),
                        "lastHeartbeatRecv" : ISODate("2020-09-20T12:26:13.055Z"),
                        "pingMs" : NumberLong(2),
                        "lastHeartbeatMessage" : "",
                        "syncingTo" : "",
                        "syncSourceHost" : "",
                        "syncSourceId" : -1,
                        "infoMessage" : "",
                        "configVersion" : 1
                }
        ],
        "ok" : 1,
        "$gleStats" : {
                "lastOpTime" : Timestamp(1600604767, 1),
                "electionId" : ObjectId("000000000000000000000000")
        },
        "lastCommittedOpTime" : Timestamp(0, 0)
}
           

shard1

节点1操作,初始化shard1
mongo --port 20013

use admin 
rs.initiate({_id:"shard1",members:[{ _id:0, host:"192.168.62.41:20013",priority:3},{ _id:1, host:" 192.168.62.42:20013",priority:2},{ _id:2, host:" 192.168.62.43:20013",priority:1}]})
rs.status()
{
        "set" : "shard1",
        "date" : ISODate("2020-09-20T13:20:55.829Z"),
        "myState" : 2,
        "term" : NumberLong(0),
        "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(1600608048, 1),
                        "t" : NumberLong(-1)
                },
                "durableOpTime" : {
                        "ts" : Timestamp(1600608048, 1),
                        "t" : NumberLong(-1)
                },
                "lastAppliedWallTime" : ISODate("2020-09-20T13:20:48.757Z"),
                "lastDurableWallTime" : ISODate("2020-09-20T13:20:48.757Z")
        },
        "lastStableRecoveryTimestamp" : Timestamp(0, 0),
        "lastStableCheckpointTimestamp" : Timestamp(0, 0),
        "members" : [
                {
                        "_id" : 0,
                        "name" : "192.168.62.41:20013",
                        "ip" : "192.168.62.41",
                        "health" : 1,
                        "state" : 2,
                        "stateStr" : "SECONDARY",
                        "uptime" : 185608,
                        "optime" : {
                                "ts" : Timestamp(1600608048, 1),
                                "t" : NumberLong(-1)
                        },
                        "optimeDate" : ISODate("2020-09-20T13:20:48Z"),
                        "syncingTo" : "",
                        "syncSourceHost" : "",
                        "syncSourceId" : -1,
                        "infoMessage" : "could not find member to sync from",
                        "configVersion" : 1,
                        "self" : true,
                        "lastHeartbeatMessage" : ""
                },
                {
                        "_id" : 1,
                        "name" : "192.168.62.42:20013",
                        "ip" : "192.168.62.42",
                        "health" : 1,
                        "state" : 2,
                        "stateStr" : "SECONDARY",
                        "uptime" : 7,
                        "optime" : {
                                "ts" : Timestamp(1600608048, 1),
                                "t" : NumberLong(-1)
                        },
                        "optimeDurable" : {
                                "ts" : Timestamp(1600608048, 1),
                                "t" : NumberLong(-1)
                        },
                        "optimeDate" : ISODate("2020-09-20T13:20:48Z"),
                        "optimeDurableDate" : ISODate("2020-09-20T13:20:48Z"),
                        "lastHeartbeat" : ISODate("2020-09-20T13:20:55.808Z"),
                        "lastHeartbeatRecv" : ISODate("2020-09-20T13:20:55.442Z"),
                        "pingMs" : NumberLong(0),
                        "lastHeartbeatMessage" : "",
                        "syncingTo" : "",
                        "syncSourceHost" : "",
                        "syncSourceId" : -1,
                        "infoMessage" : "",
                        "configVersion" : 1
                },
                {
                        "_id" : 2,
                        "name" : "192.168.62.43:20013",
                        "ip" : "192.168.62.43",
                        "health" : 1,
                        "state" : 2,
                        "stateStr" : "SECONDARY",
                        "uptime" : 7,
                        "optime" : {
                                "ts" : Timestamp(1600608048, 1),
                                "t" : NumberLong(-1)
                        },
                        "optimeDurable" : {
                                "ts" : Timestamp(1600608048, 1),
                                "t" : NumberLong(-1)
                        },
                        "optimeDate" : ISODate("2020-09-20T13:20:48Z"),
                        "optimeDurableDate" : ISODate("2020-09-20T13:20:48Z"),
                        "lastHeartbeat" : ISODate("2020-09-20T13:20:55.827Z"),
                        "lastHeartbeatRecv" : ISODate("2020-09-20T13:20:55.427Z"),
                        "pingMs" : NumberLong(0),
                        "lastHeartbeatMessage" : "",
                        "syncingTo" : "",
                        "syncSourceHost" : "",
                        "syncSourceId" : -1,
                        "infoMessage" : "",
                        "configVersion" : 1
                }
        ],
        "ok" : 1
}
           

shard2

节点1操作,初始化shard2
mongo --port 20014 
rs.initiate({_id:"shard2",members:[{ _id:0, host:"192.168.62.41:20014",priority:3},{ _id:1, host:" 192.168.62.42:20014",priority:2},{ _id:2, host:" 192.168.62.43:20014",priority:1}]})
rs.status()
{
        "set" : "shard2",
        "date" : ISODate("2020-09-20T13:27:27.597Z"),
        "myState" : 2,
        "term" : NumberLong(0),
        "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(1600608443, 1),
                        "t" : NumberLong(-1)
                },
                "durableOpTime" : {
                        "ts" : Timestamp(1600608443, 1),
                        "t" : NumberLong(-1)
                },
                "lastAppliedWallTime" : ISODate("2020-09-20T13:27:23.260Z"),
                "lastDurableWallTime" : ISODate("2020-09-20T13:27:23.260Z")
        },
        "lastStableRecoveryTimestamp" : Timestamp(0, 0),
        "lastStableCheckpointTimestamp" : Timestamp(0, 0),
        "members" : [
                {
                        "_id" : 0,
                        "name" : "192.168.62.41:20014",
                        "ip" : "192.168.62.41",
                        "health" : 1,
                        "state" : 2,
                        "stateStr" : "SECONDARY",
                        "uptime" : 185970,
                        "optime" : {
                                "ts" : Timestamp(1600608443, 1),
                                "t" : NumberLong(-1)
                        },
                        "optimeDate" : ISODate("2020-09-20T13:27:23Z"),
                        "syncingTo" : "",
                        "syncSourceHost" : "",
                        "syncSourceId" : -1,
                        "infoMessage" : "could not find member to sync from",
                        "configVersion" : 1,
                        "self" : true,
                        "lastHeartbeatMessage" : ""
                },
                {
                        "_id" : 1,
                        "name" : "192.168.62.42:20014",
                        "ip" : "192.168.62.42",
                        "health" : 1,
                        "state" : 2,
                        "stateStr" : "SECONDARY",
                        "uptime" : 4,
                        "optime" : {
                                "ts" : Timestamp(1600608443, 1),
                                "t" : NumberLong(-1)
                        },
                        "optimeDurable" : {
                                "ts" : Timestamp(1600608443, 1),
                                "t" : NumberLong(-1)
                        },
                        "optimeDate" : ISODate("2020-09-20T13:27:23Z"),
                        "optimeDurableDate" : ISODate("2020-09-20T13:27:23Z"),
                        "lastHeartbeat" : ISODate("2020-09-20T13:27:27.353Z"),
                        "lastHeartbeatRecv" : ISODate("2020-09-20T13:27:27.583Z"),
                        "pingMs" : NumberLong(1),
                        "lastHeartbeatMessage" : "",
                        "syncingTo" : "",
                        "syncSourceHost" : "",
                        "syncSourceId" : -1,
                        "infoMessage" : "",
                        "configVersion" : 1
                },
                {
                        "_id" : 2,
                        "name" : "192.168.62.43:20014",
                        "ip" : "192.168.62.43",
                        "health" : 1,
                        "state" : 2,
                        "stateStr" : "SECONDARY",
                        "uptime" : 4,
                        "optime" : {
                                "ts" : Timestamp(1600608443, 1),
                                "t" : NumberLong(-1)
                        },
                        "optimeDurable" : {
                                "ts" : Timestamp(1600608443, 1),
                                "t" : NumberLong(-1)
                        },
                        "optimeDate" : ISODate("2020-09-20T13:27:23Z"),
                        "optimeDurableDate" : ISODate("2020-09-20T13:27:23Z"),
                        "lastHeartbeat" : ISODate("2020-09-20T13:27:27.344Z"),
                        "lastHeartbeatRecv" : ISODate("2020-09-20T13:27:27.579Z"),
                        "pingMs" : NumberLong(0),
                        "lastHeartbeatMessage" : "",
                        "syncingTo" : "",
                        "syncSourceHost" : "",
                        "syncSourceId" : -1,
                        "infoMessage" : "",
                        "configVersion" : 1
                }
        ],
        "ok" : 1
}
           

shard3

节点1操作,初始化shard3
mongo --port 20015
rs.initiate({_id:"shard3",members:[{ _id:0, host:"192.168.62.41:20015",priority:3},{ _id:1, host:" 192.168.62.42:20015",priority:2},{ _id:2, host:" 192.168.62.43:20015",priority:1}]})
rs.status()
{
        "set" : "shard3",
        "date" : ISODate("2020-09-20T13:32:02.252Z"),
        "myState" : 1,
        "term" : NumberLong(1),
        "syncingTo" : "",
        "syncSourceHost" : "",
        "syncSourceId" : -1,
        "heartbeatIntervalMillis" : NumberLong(2000),
        "majorityVoteCount" : 2,
        "writeMajorityCount" : 2,
        "optimes" : {
                "lastCommittedOpTime" : {
                        "ts" : Timestamp(1600608720, 1),
                        "t" : NumberLong(1)
                },
                "lastCommittedWallTime" : ISODate("2020-09-20T13:32:00.422Z"),
                "readConcernMajorityOpTime" : {
                        "ts" : Timestamp(1600608720, 1),
                        "t" : NumberLong(1)
                },
                "readConcernMajorityWallTime" : ISODate("2020-09-20T13:32:00.422Z"),
                "appliedOpTime" : {
                        "ts" : Timestamp(1600608720, 1),
                        "t" : NumberLong(1)
                },
                "durableOpTime" : {
                        "ts" : Timestamp(1600608720, 1),
                        "t" : NumberLong(1)
                },
                "lastAppliedWallTime" : ISODate("2020-09-20T13:32:00.422Z"),
                "lastDurableWallTime" : ISODate("2020-09-20T13:32:00.422Z")
        },
        "lastStableRecoveryTimestamp" : Timestamp(1600608700, 2),
        "lastStableCheckpointTimestamp" : Timestamp(1600608700, 2),
        "electionCandidateMetrics" : {
                "lastElectionReason" : "electionTimeout",
                "lastElectionDate" : ISODate("2020-09-20T13:31:39.945Z"),
                "termAtElection" : NumberLong(1),
                "lastCommittedOpTimeAtElection" : {
                        "ts" : Timestamp(0, 0),
                        "t" : NumberLong(-1)
                },
                "lastSeenOpTimeAtElection" : {
                        "ts" : Timestamp(1600608689, 1),
                        "t" : NumberLong(-1)
                },
                "numVotesNeeded" : 2,
                "priorityAtElection" : 3,
                "electionTimeoutMillis" : NumberLong(10000),
                "numCatchUpOps" : NumberLong(20015),
                "newTermStartDate" : ISODate("2020-09-20T13:31:40.420Z"),
                "wMajorityWriteAvailabilityDate" : ISODate("2020-09-20T13:31:41.340Z")
        },
        "members" : [
                {
                        "_id" : 0,
                        "name" : "192.168.62.41:20015",
                        "ip" : "192.168.62.41",
                        "health" : 1,
                        "state" : 1,
                        "stateStr" : "PRIMARY",
                        "uptime" : 186244,
                        "optime" : {
                                "ts" : Timestamp(1600608720, 1),
                                "t" : NumberLong(1)
                        },
                        "optimeDate" : ISODate("2020-09-20T13:32:00Z"),
                        "syncingTo" : "",
                        "syncSourceHost" : "",
                        "syncSourceId" : -1,
                        "infoMessage" : "could not find member to sync from",
                        "electionTime" : Timestamp(1600608699, 1),
                        "electionDate" : ISODate("2020-09-20T13:31:39Z"),
                        "configVersion" : 1,
                        "self" : true,
                        "lastHeartbeatMessage" : ""
                },
                {
                        "_id" : 1,
                        "name" : "192.168.62.42:20015",
                        "ip" : "192.168.62.42",
                        "health" : 1,
                        "state" : 2,
                        "stateStr" : "SECONDARY",
                        "uptime" : 32,
                        "optime" : {
                                "ts" : Timestamp(1600608720, 1),
                                "t" : NumberLong(1)
                        },
                        "optimeDurable" : {
                                "ts" : Timestamp(1600608720, 1),
                                "t" : NumberLong(1)
                        },
                        "optimeDate" : ISODate("2020-09-20T13:32:00Z"),
                        "optimeDurableDate" : ISODate("2020-09-20T13:32:00Z"),
                        "lastHeartbeat" : ISODate("2020-09-20T13:32:01.967Z"),
                        "lastHeartbeatRecv" : ISODate("2020-09-20T13:32:01.379Z"),
                        "pingMs" : NumberLong(0),
                        "lastHeartbeatMessage" : "",
                        "syncingTo" : "192.168.62.41:20015",
                        "syncSourceHost" : "192.168.62.41:20015",
                        "syncSourceId" : 0,
                        "infoMessage" : "",
                        "configVersion" : 1
                },
                {
                        "_id" : 2,
                        "name" : "192.168.62.43:20015",
                        "ip" : "192.168.62.43",
                        "health" : 1,
                        "state" : 2,
                        "stateStr" : "SECONDARY",
                        "uptime" : 32,
                        "optime" : {
                                "ts" : Timestamp(1600608720, 1),
                                "t" : NumberLong(1)
                        },
                        "optimeDurable" : {
                                "ts" : Timestamp(1600608720, 1),
                                "t" : NumberLong(1)
                        },
                        "optimeDate" : ISODate("2020-09-20T13:32:00Z"),
                        "optimeDurableDate" : ISODate("2020-09-20T13:32:00Z"),
                        "lastHeartbeat" : ISODate("2020-09-20T13:32:02.093Z"),
                        "lastHeartbeatRecv" : ISODate("2020-09-20T13:32:01.365Z"),
                        "pingMs" : NumberLong(0),
                        "lastHeartbeatMessage" : "",
                        "syncingTo" : "192.168.62.41:20015",
                        "syncSourceHost" : "192.168.62.41:20015",
                        "syncSourceId" : 0,
                        "infoMessage" : "",
                        "configVersion" : 1
                }
        ],
        "ok" : 1
}
           

启动mongos

所有节点启动mongos
mongos -f /mongodb/conf/mongos.conf
           

主节点创建管理员用户

config

mongo --port 20011
use admin
db.createUser({user:"admin",pwd:"admin",roles:[{role:"root",db:"admin"}]})
           

shard1

mongo --port 20013
use admin
db.createUser({user:"admin",pwd:"admin",roles:[{role:"root",db:"admin"}]})
           

shard2

mongo --port 20014
use admin
db.createUser({user:"admin",pwd:"admin",roles:[{role:"root",db:"admin"}]})
           

shard3

mongo --port 20015
use admin
db.createUser({user:"admin",pwd:"admin",roles:[{role:"root",db:"admin"}]})
           

建立数据库及操作用户

为某个数据库创建用户必须先use这个数据库,否则用户无法直接登录这个库
mongo --port 20012
use admin
mongos> use admin
switched to db admin
mongos> db.auth("admin","admin")
1
mongos> use test
switched to db test
mongos> db.createUser({user:"test",pwd:"test",roles:[{role:"readWrite",db:"test"}]})
Successfully added user: {
        "user" : "test",
        "roles" : [
                {
                        "role" : "readWrite",
                        "db" : "test"
                }
        ]
}
           

添加分片

shard1

db.runCommand({addshard:"shard1/192.168.62.41:20013,192.168.62.42:20013,192.168.62.43:20013"})
{
        "shardAdded" : "shard1",
        "ok" : 1,
        "operationTime" : Timestamp(1600612178, 6),
        "$clusterTime" : {
                "clusterTime" : Timestamp(1600612178, 6),
                "signature" : {
                        "hash" : BinData(0,"21NENQGf/UNwgBiHRuLO/AuBcMU="),
                        "keyId" : NumberLong("6874545179626307614")
                }
        }
}
           

shard2

db.runCommand({addshard:"shard2/192.168.62.41:20014,192.168.62.42:20014,192.168.62.43:20014"})
{
        "shardAdded" : "shard2",
        "ok" : 1,
        "operationTime" : Timestamp(1600612221, 4),
        "$clusterTime" : {
                "clusterTime" : Timestamp(1600612221, 4),
                "signature" : {
                        "hash" : BinData(0,"8jsEF1RSqvSlZQrN/WD9ajjgGlM="),
                        "keyId" : NumberLong("6874545179626307614")
                }
        }
}
           

shard3

db.runCommand({addshard:"shard3/192.168.62.41:20015,192.168.62.42:20015,192.168.62.43:20015"})
{
        "shardAdded" : "shard3",
        "ok" : 1,
        "operationTime" : Timestamp(1600612242, 4),
        "$clusterTime" : {
                "clusterTime" : Timestamp(1600612242, 4),
                "signature" : {
                        "hash" : BinData(0,"16e+ePSYJ9Zpe+thLDU59ZZfGuI="),
                        "keyId" : NumberLong("6874545179626307614")
                }
        }
}
           

检查分片

use admin
switched to db admin
mongos> db.auth("admin","admin")
1
db.runCommand({listshards:1})
{
        "shards" : [
                {
                        "_id" : "shard1",
                        "host" : "shard1/192.168.62.41:20013,192.168.62.42:20013,192.168.62.43:20013",
                        "state" : 1
                },
                {
                        "_id" : "shard2",
                        "host" : "shard2/192.168.62.41:20014,192.168.62.42:20014,192.168.62.43:20014",
                        "state" : 1
                },
                {
                        "_id" : "shard3",
                        "host" : "shard3/192.168.62.41:20015,192.168.62.42:20015,192.168.62.43:20015",
                        "state" : 1
                }
        ],
        "ok" : 1,
        "operationTime" : Timestamp(1600612312, 1),
        "$clusterTime" : {
                "clusterTime" : Timestamp(1600612312, 1),
                "signature" : {
                        "hash" : BinData(0,"1m76Zzb45daZFKO3tnzjMsODK5s="),
                        "keyId" : NumberLong("6874545179626307614")
                }
        }
}
           

测试

指定test分片生效

db.runCommand({enablesharding:"test"});
{
        "ok" : 1,
        "operationTime" : Timestamp(1600612406, 5),
        "$clusterTime" : {
                "clusterTime" : Timestamp(1600612406, 5),
                "signature" : {
                        "hash" : BinData(0,"9WN8so7e29EZou9bCM35IKz3HLE="),
                        "keyId" : NumberLong("6874545179626307614")
                }
        }
}
           

指定分片的集合和片建

集合testdb.table1以片建id按哈希分片
db.runCommand({shardcollection:"testdb.table1",key:{id:"hashed"}})
{
        "ok" : 0,
        "errmsg" : "database testdb not found",
        "code" : 26,
        "codeName" : "NamespaceNotFound",
        "operationTime" : Timestamp(1600612527, 4),
        "$clusterTime" : {
                "clusterTime" : Timestamp(1600612527, 4),
                "signature" : {
                        "hash" : BinData(0,"bS6KhipblswYeHefmwRWuYRyEh8="),
                        "keyId" : NumberLong("6874545179626307614")
                }
        }
}
           

插入数据

use admin
db.auth("admin","admin")
use test
for(var i=1;i<600000;i++) db.table1.insert({name:"mongo",x:i})
db.getCollection('table1').createIndex({x:"hashed"},{background:true});

use admin
db.auth("admin","admin")
db.runCommand({enablesharding:"test"})
{
        "ok" : 1,
        "operationTime" : Timestamp(1600650070, 3),
        "$clusterTime" : {
                "clusterTime" : Timestamp(1600650070, 3),
                "signature" : {
                        "hash" : BinData(0,"sxGAGmKnKxSjuaMynIUajXBexo0="),
                        "keyId" : NumberLong("6874545179626307614")
                }
        }
}


           

日常维护命令

db.runCommand({shardcollection:"test.table1",key:{x:"hashed"}})
db.printShardingStatus()
sh.status()
use test
show tables
db.table1.stats()
sh.startBalancer()