mongodb3.6副本集升級為分片副本集

服務器信息

ip 系統 配置 服務 目錄
172.24.32.201 centos7.7 2c4g mongo-primary /var/lib/mongo (the data directory) /var/log/mongodb (the log directory)
172.24.32.201 centos7.7 2c4g mongo-secondary /var/lib/mongo27018 (the data directory) /var/log/mongodb27018 (the log directory)
172.24.32.201 centos7.7 2c4g mongo-secondary /var/lib/mongo27019 (the data directory) /var/log/mongodb27019 (the log directory)

由于config、mongos、mongod全部在一臺服務器上。所以這里用目錄區分

整理下服務目錄和文件名

這里先清空之前的架構為一主兩從的三副本集,整理下目錄名和文件名

systemctl stop mongod
systemctl stop mongod27018
systemctl stop mongod27019
rm -rf /var/lib/mongo/*
rm -rf /var/lib/mongo2701*/*

mongo27017的信息

  • 配置文件
    /etc/mongod27017.conf
  • 啟動文件
    /usr/lib/systemd/system/mongod27017.service

mongo27018的信息

  • 配置文件
    /etc/mongod27018.conf
  • 啟動文件
    /usr/lib/systemd/system/mongod27018.service

mongo27019的信息

  • 配置文件
    /etc/mongod27019.conf
  • 啟動文件
    /usr/lib/systemd/system/mongod27019.service

啟動

systemctl daemon-reload
systemctl start mongod27017
systemctl start mongod27018
systemctl start mongod27019

進入并創建副本集

mongo 127.0.0.1:27017/admin
config = {
       _id : "lugotestrepl",
       members : [
            {_id : 0, host : "127.0.0.1:27017" , priority:33},
            {_id : 1, host : "127.0.0.1:27018" , priority:2},
            {_id : 2, host : "127.0.0.1:27019" , priority:2}
        ]
    }
rs.initiate(config);

副本集狀態

lugotestrepl:PRIMARY> rs.status();
{
    "set" : "lugotestrepl",
    "date" : ISODate("2020-05-15T06:50:06.639Z"),
    "myState" : 1,
    "term" : NumberLong(2),
    "syncingTo" : "",
    "syncSourceHost" : "",
    "syncSourceId" : -1,
    "heartbeatIntervalMillis" : NumberLong(2000),
    "optimes" : {
        "lastCommittedOpTime" : {
            "ts" : Timestamp(1589525404, 1),
            "t" : NumberLong(2)
        },
        "readConcernMajorityOpTime" : {
            "ts" : Timestamp(1589525404, 1),
            "t" : NumberLong(2)
        },
        "appliedOpTime" : {
            "ts" : Timestamp(1589525404, 1),
            "t" : NumberLong(2)
        },
        "durableOpTime" : {
            "ts" : Timestamp(1589525404, 1),
            "t" : NumberLong(2)
        }
    },
    "members" : [
        {
            "_id" : 0,
            "name" : "127.0.0.1:27017",
            "health" : 1,
            "state" : 1,
            "stateStr" : "PRIMARY",
            "uptime" : 1569,
            "optime" : {
                "ts" : Timestamp(1589525404, 1),
                "t" : NumberLong(2)
            },
            "optimeDate" : ISODate("2020-05-15T06:50:04Z"),
            "syncingTo" : "",
            "syncSourceHost" : "",
            "syncSourceId" : -1,
            "infoMessage" : "",
            "electionTime" : Timestamp(1589524572, 1),
            "electionDate" : ISODate("2020-05-15T06:36:12Z"),
            "configVersion" : 1,
            "self" : true,
            "lastHeartbeatMessage" : ""
        },
        {
            "_id" : 1,
            "name" : "127.0.0.1:27018",
            "health" : 1,
            "state" : 2,
            "stateStr" : "SECONDARY",
            "uptime" : 855,
            "optime" : {
                "ts" : Timestamp(1589525404, 1),
                "t" : NumberLong(2)
            },
            "optimeDurable" : {
                "ts" : Timestamp(1589525404, 1),
                "t" : NumberLong(2)
            },
            "optimeDate" : ISODate("2020-05-15T06:50:04Z"),
            "optimeDurableDate" : ISODate("2020-05-15T06:50:04Z"),
            "lastHeartbeat" : ISODate("2020-05-15T06:50:05.477Z"),
            "lastHeartbeatRecv" : ISODate("2020-05-15T06:50:04.876Z"),
            "pingMs" : NumberLong(0),
            "lastHeartbeatMessage" : "",
            "syncingTo" : "127.0.0.1:27017",
            "syncSourceHost" : "127.0.0.1:27017",
            "syncSourceId" : 0,
            "infoMessage" : "",
            "configVersion" : 1
        },
        {
            "_id" : 2,
            "name" : "127.0.0.1:27019",
            "health" : 1,
            "state" : 2,
            "stateStr" : "SECONDARY",
            "uptime" : 855,
            "optime" : {
                "ts" : Timestamp(1589525404, 1),
                "t" : NumberLong(2)
            },
            "optimeDurable" : {
                "ts" : Timestamp(1589525404, 1),
                "t" : NumberLong(2)
            },
            "optimeDate" : ISODate("2020-05-15T06:50:04Z"),
            "optimeDurableDate" : ISODate("2020-05-15T06:50:04Z"),
            "lastHeartbeat" : ISODate("2020-05-15T06:50:05.477Z"),
            "lastHeartbeatRecv" : ISODate("2020-05-15T06:50:04.755Z"),
            "pingMs" : NumberLong(0),
            "lastHeartbeatMessage" : "",
            "syncingTo" : "127.0.0.1:27017",
            "syncSourceHost" : "127.0.0.1:27017",
            "syncSourceId" : 0,
            "infoMessage" : "",
            "configVersion" : 1
        }
    ],
    "ok" : 1
}

插入數據
創建用戶

db.createUser({
    user: "admin",
    pwd: "test123",
    roles: ["root"]
});


插入數據

lugotestrepl:PRIMARY> db.movie.insert({"moviename":"大偵探福爾摩斯","points":"9.5"})
WriteResult({ "nInserted" : 1 })

查看數據

lugotestrepl:PRIMARY> db.movie.find().pretty();
{
    "_id" : ObjectId("5ebe402573a048240cf0e087"),
    "moviename" : "大偵探福爾摩斯",
    "points" : "9.5"
}
{
    "_id" : ObjectId("5ebe404173a048240cf0e088"),
    "moviename" : "掠奪",
    "points" : "9.2"
}
{
    "_id" : ObjectId("5ebe405073a048240cf0e089"),
    "moviename" : "搖滾黑幫",
    "points" : "9.9"
}
{
    "_id" : ObjectId("5ebe405b73a048240cf0e08a"),
    "moviename" : "兩桿大煙槍",
    "points" : "9.1"
}
{
    "_id" : ObjectId("5ebe406873a048240cf0e08b"),
    "moviename" : "殺死比爾",
    "points" : "9.0"
}

至此,mongo27017~27018副本集重構完畢
架構為一主兩從。

搭建config副本集

mongodb3.4以后要求配置服務器也創建副本集,不然集群搭建不成功。

-------------------config21000目錄--------------------------
mkdir -p /var/log/mongodbconfig/
mkdir -p /var/lib/mongoconfig/config21000/data
mkdir -p /var/run/mongodbconfig21000/
chown -R mongod:mongod /var/log/mongodbconfig/ &&chmod 0755 /var/log/mongodbconfig/
chown -R mongod:mongod /var/lib/mongoconfig/config21000/data &&chmod 0755 /var/lib/mongoconfig/config21000/data
chown -R mongod:mongod /var/run/mongodbconfig21000/ &&chmod 0755 /var/run/mongodbconfig21000/
-------------------config21001目錄--------------------------
mkdir -p /var/lib/mongoconfig/config21001/data
mkdir -p /var/run/mongodbconfig21001/
chown -R mongod:mongod /var/lib/mongoconfig/config21001/data &&chmod 0755 /var/lib/mongoconfig/config21001/data
chown -R mongod:mongod /var/run/mongodbconfig21001/ &&chmod 0755 /var/run/mongodbconfig21001/
-------------------config21002目錄--------------------------
mkdir -p /var/lib/mongoconfig/config21002/data
mkdir -p /var/run/mongodbconfig21002/
chown -R mongod:mongod /var/lib/mongoconfig/config21002/data &&chmod 0755 /var/lib/mongoconfig/config21002/data
chown -R mongod:mongod /var/run/mongodbconfig21002/ &&chmod 0755 /var/run/mongodbconfig21002/

config的副本集文件結構

  • 21000實例的配置文件
cat >>/etc/mongod/conf.d/config_21000.conf<<EOF
systemLog:
  destination: file
  logAppend: true
  path: /var/log/mongodbconfig/configsvr21000.log
 
storage:
  dbPath: /var/lib/mongoconfig/config21000/data
  journal:
    enabled: true

processManagement:
  fork: true
  pidFilePath: /var/run/mongodbconfig21000/configsvr.pid

net:
  port: 21000
  bindIp: 0.0.0.0
  maxIncomingConnections: 20000

replication:
  replSetName: lugotestreplconfig

sharding:
  clusterRole: configsvr
security:
    keyFile: /etc/mongokey/mongodb-keyfile
    clusterAuthMode: keyFile
setParameter:
    enableLocalhostAuthBypass: true

EOF
----------------------------------------------------------------------------
cat >>/usr/lib/systemd/system/mongodconfig21000.service<<EOF
[Unit]
Description=MongoDB Database Server
Documentation=https://docs.mongodb.org/manual
After=network.target

[Service]
User=mongod
Group=mongod
Environment="OPTIONS=-f /etc/mongod/conf.d/config_21000.conf"
ExecStart=/usr/bin/mongod $OPTIONS
#ExecStartPre=/usr/bin/mkdir -p /var/run/mongodb
#ExecStartPre=/usr/bin/chown mongod:mongod /var/run/mongodb
#ExecStartPre=/usr/bin/chmod 0755 /var/run/mongodb
PermissionsStartOnly=true
PIDFile=/var/run/mongodbconfig21000/mongod.pid
Type=forking
# file size
LimitFSIZE=infinity
# cpu time
LimitCPU=infinity
# virtual memory size
LimitAS=infinity
# open files
LimitNOFILE=64000
# processes/threads
LimitNPROC=64000
# locked memory
LimitMEMLOCK=infinity
# total threads (user+kernel)
TasksMax=infinity
TasksAccounting=false
# Recommended limits for for mongod as specified in
# http://docs.mongodb.org/manual/reference/ulimit/#recommended-settings

[Install]
WantedBy=multi-user.target
EOF
  • 21001實例的配置文件
/etc/mongod/conf.d/config_21001.conf
/usr/lib/systemd/system/mongodconfig21001.service
  • 21002實例的配置文件
/etc/mongod/conf.d/config_21002.conf
/usr/lib/systemd/system/mongodconfig21002.service

啟動config的三個實例

systemctl restart mongodconfig21001
systemctl restart mongodconfig21002
systemctl restart mongodconfig21000

進入實例,開始創建config副本集

mongo 127.0.0.1:21000
> config = {
...          _id : "lugotestreplconfig", 
...          members : [
...            {_id : 0, host : "127.0.0.1:21000" },
...            {_id : 1, host : "127.0.0.1:21001" },
...            {_id : 2, host : "127.0.0.1:21002" }
...          ]
...  }
{
    "_id" : "lugotestreplconfig",
    "members" : [
        {
            "_id" : 0,
            "host" : "127.0.0.1:21000"
        },
        {
            "_id" : 1,
            "host" : "127.0.0.1:21001"
        },
        {
            "_id" : 2,
            "host" : "127.0.0.1:21002"
        }
    ]
}
> rs.initiate(config)
{
    "ok" : 1,
    "$gleStats" : {
        "lastOpTime" : Timestamp(1589527585, 1),
        "electionId" : ObjectId("000000000000000000000000")
    }
}

至此,config節點副本集搭建完成,這個節點主要存放集群信息

mongos搭建

搭建路由節點,這里搭建個單機的測試使用

  • 配置文件
cat >>/etc/mongod/conf.d/mongos_22222.conf<<EOF
systemLog:
  destination: file
  logAppend: true
  path: /var/log/mongodb/mongos22222.log

processManagement:
  fork: true
  pidFilePath: /var/run/mongodb/mongos22222.pid

net:
  port: 22222
  bindIp: 0.0.0.0
  maxIncomingConnections: 20000

sharding:
  configDB: lugotestreplconfig/172.24.32.201:21000,172.24.32.201:21001,172.24.32.201:21002


security:
    keyFile: /etc/mongokey/mongodb-keyfile
    clusterAuthMode: keyFile
setParameter:
    enableLocalhostAuthBypass: true

EOF
  • 啟動文件
cat >>/usr/lib/systemd/system/mongos22222.service<<EOF
[Unit]
Description=MongoDB Database Server
Documentation=https://docs.mongodb.org/manual
After=network.target

[Service]
User=mongod
Group=mongod
Environment="OPTIONS=-f /etc/mongod/conf.d/mongos_22222.conf"
ExecStart=/usr/bin/mongos $OPTIONS
#ExecStartPre=/usr/bin/mkdir -p /var/run/mongodb
#ExecStartPre=/usr/bin/chown mongod:mongod /var/run/mongodb
#ExecStartPre=/usr/bin/chmod 0755 /var/run/mongodb
PermissionsStartOnly=true
PIDFile=/var/run/mongodb/mongos22222.pid
Type=forking
# file size
LimitFSIZE=infinity
# cpu time
LimitCPU=infinity
# virtual memory size
LimitAS=infinity
# open files
LimitNOFILE=64000
# processes/threads
LimitNPROC=64000
# locked memory
LimitMEMLOCK=infinity
# total threads (user+kernel)
TasksMax=infinity
TasksAccounting=false
# Recommended limits for for mongod as specified in
# http://docs.mongodb.org/manual/reference/ulimit/#recommended-settings

[Install]
WantedBy=multi-user.target
EOF

啟動和開機自啟動mongos

systemctl start mongos22222
systemctl enable mongos22222

將先前部署的副本集升級為一個分片副本集群中的一個分片

進入mongos

mongo 127.0.0.1:22222/admin

創建賬號

mongos> db.createUser({
...     user: "admin",
...     pwd: "test123",
...     roles: ["root"]
... });
Successfully added user: { "user" : "admin", "roles" : [ "root" ] }
mongos> db.auth('admin','test123')
1

分片

sh.addShard("lugotestrepl/127.0.0.1:27017,127.0.0.1:27018,127.0.0.1:27019")

查看信息

mongos> db.runCommand({ listshards : 1 });
{
    "shards" : [
        {
            "_id" : "lugotestrepl",
            "host" : "lugotestrepl/127.0.0.1:27017,127.0.0.1:27018,127.0.0.1:27019",
            "state" : 1
        }
    ],
    "ok" : 1,
    "operationTime" : Timestamp(1589528828, 1),
    "$clusterTime" : {
        "clusterTime" : Timestamp(1589528828, 1),
        "signature" : {
            "hash" : BinData(0,"9kplNbREI1p373yIwx9vXICQy1w="),
            "keyId" : NumberLong("6826969049499435034")
        }
    }
}

在分片副本集中,加入第二個副本集分片,并驗證分片集群狀態

創建第二個副本集,并加入分片

  • mongod37017配置文件
    /etc/mongod37017.conf
  • 啟動文件
    /usr/lib/systemd/system/mongod37017.service

mongo37018的信息

  • 配置文件
    /etc/mongod37018.conf
  • 啟動文件
    /usr/lib/systemd/system/mongod37019.service

mongo37019的信息

  • 配置文件
    /etc/mongod37019.conf
  • 啟動文件
    usr/lib/systemd/system/mongod37019.service

啟動mongod37017、mongod37018、mongod37019節點

systemctl start mongod37017
systemctl start mongod37018
systemctl start mongod37019
systemctl enable mongod37017
systemctl enable mongod37018
systemctl enable mongod37019

進入37017節點,創建副本集

mongo 127.0.0.1:37017/admin
config = {
       _id : "shard2",
       members : [
            {_id : 0, host : "127.0.0.1:37017" , priority:33},
            {_id : 1, host : "127.0.0.1:37018" , priority:2},
            {_id : 2, host : "127.0.0.1:37019" , priority:2}
        ]
    }

初始化,并查看狀態

> rs.initiate(config);
{ "ok" : 1 }

狀態

shard2:PRIMARY> rs.status();
{
    "set" : "shard2",
    "date" : ISODate("2020-05-15T08:25:25.375Z"),
    "myState" : 1,
    "term" : NumberLong(1),
    "syncingTo" : "",
    "syncSourceHost" : "",
    "syncSourceId" : -1,
    "heartbeatIntervalMillis" : NumberLong(2000),
    "optimes" : {
        "lastCommittedOpTime" : {
            "ts" : Timestamp(1589531119, 1),
            "t" : NumberLong(1)
        },
        "readConcernMajorityOpTime" : {
            "ts" : Timestamp(1589531119, 1),
            "t" : NumberLong(1)
        },
        "appliedOpTime" : {
            "ts" : Timestamp(1589531119, 1),
            "t" : NumberLong(1)
        },
        "durableOpTime" : {
            "ts" : Timestamp(1589531119, 1),
            "t" : NumberLong(1)
        }
    },
    "members" : [
        {
            "_id" : 0,
            "name" : "127.0.0.1:37017",
            "health" : 1,
            "state" : 1,
            "stateStr" : "PRIMARY",
            "uptime" : 555,
            "optime" : {
                "ts" : Timestamp(1589531119, 1),
                "t" : NumberLong(1)
            },
            "optimeDate" : ISODate("2020-05-15T08:25:19Z"),
            "syncingTo" : "",
            "syncSourceHost" : "",
            "syncSourceId" : -1,
            "infoMessage" : "could not find member to sync from",
            "electionTime" : Timestamp(1589531097, 1),
            "electionDate" : ISODate("2020-05-15T08:24:57Z"),
            "configVersion" : 1,
            "self" : true,
            "lastHeartbeatMessage" : ""
        },
        {
            "_id" : 1,
            "name" : "127.0.0.1:37018",
            "health" : 1,
            "state" : 2,
            "stateStr" : "SECONDARY",
            "uptime" : 37,
            "optime" : {
                "ts" : Timestamp(1589531119, 1),
                "t" : NumberLong(1)
            },
            "optimeDurable" : {
                "ts" : Timestamp(1589531119, 1),
                "t" : NumberLong(1)
            },
            "optimeDate" : ISODate("2020-05-15T08:25:19Z"),
            "optimeDurableDate" : ISODate("2020-05-15T08:25:19Z"),
            "lastHeartbeat" : ISODate("2020-05-15T08:25:23.540Z"),
            "lastHeartbeatRecv" : ISODate("2020-05-15T08:25:24.791Z"),
            "pingMs" : NumberLong(0),
            "lastHeartbeatMessage" : "",
            "syncingTo" : "127.0.0.1:37017",
            "syncSourceHost" : "127.0.0.1:37017",
            "syncSourceId" : 0,
            "infoMessage" : "",
            "configVersion" : 1
        },
        {
            "_id" : 2,
            "name" : "127.0.0.1:37019",
            "health" : 1,
            "state" : 2,
            "stateStr" : "SECONDARY",
            "uptime" : 37,
            "optime" : {
                "ts" : Timestamp(1589531119, 1),
                "t" : NumberLong(1)
            },
            "optimeDurable" : {
                "ts" : Timestamp(1589531119, 1),
                "t" : NumberLong(1)
            },
            "optimeDate" : ISODate("2020-05-15T08:25:19Z"),
            "optimeDurableDate" : ISODate("2020-05-15T08:25:19Z"),
            "lastHeartbeat" : ISODate("2020-05-15T08:25:23.540Z"),
            "lastHeartbeatRecv" : ISODate("2020-05-15T08:25:24.754Z"),
            "pingMs" : NumberLong(0),
            "lastHeartbeatMessage" : "",
            "syncingTo" : "127.0.0.1:37017",
            "syncSourceHost" : "127.0.0.1:37017",
            "syncSourceId" : 0,
            "infoMessage" : "",
            "configVersion" : 1
        }
    ],
    "ok" : 1
}

分片
進入mongos

[root@test02 run]# mongo 127.0.0.1:22222/admin
MongoDB shell version v3.6.18
connecting to: mongodb://127.0.0.1:22222/admin?gssapiServiceName=mongodb
Implicit session: session { "id" : UUID("098a5b12-bd06-4812-aa68-e2a0edb19136") }
MongoDB server version: 3.6.18
mongos> db.auth('admin','test123')
1

分片指令

sh.addShard("shard2/127.0.0.1:37017,127.0.0.1:37018,127.0.0.1:37019")

分片成功

1
mongos> sh.addShard("shard2/127.0.0.1:37017,127.0.0.1:37018,127.0.0.1:37019")
{
    "shardAdded" : "shard2",
    "ok" : 1,
    "operationTime" : Timestamp(1589531264, 7),
    "$clusterTime" : {
        "clusterTime" : Timestamp(1589531264, 7),
        "signature" : {
            "hash" : BinData(0,"Ks/rUayKm+xbS1OLr04B2DAt2g4="),
            "keyId" : NumberLong("6826969049499435034")
        }
    }
}

查看分片狀態

mongos> db.runCommand({ listshards : 1 });
{
    "shards" : [
        {
            "_id" : "lugotestrepl",
            "host" : "lugotestrepl/127.0.0.1:27017,127.0.0.1:27018,127.0.0.1:27019",
            "state" : 1
        },
        {
            "_id" : "shard2",
            "host" : "shard2/127.0.0.1:37017,127.0.0.1:37018,127.0.0.1:37019",
            "state" : 1
        }
    ],
    "ok" : 1,
    "operationTime" : Timestamp(1589531316, 1),
    "$clusterTime" : {
        "clusterTime" : Timestamp(1589531316, 1),
        "signature" : {
            "hash" : BinData(0,"EoOJLcpnWACjeMa+VNRBmcg2UOE="),
            "keyId" : NumberLong("6826969049499435034")
        }
    }
}

對先前創建的數據表進行分片操作,并驗證數據的分布狀態

指定lugotest這個庫分片生效

db.runCommand( { enablesharding :"lugotest"});

結果

mongos> db.runCommand( { enablesharding :"lugotest"});
{
    "ok" : 1,
    "operationTime" : Timestamp(1589532403, 5),
    "$clusterTime" : {
        "clusterTime" : Timestamp(1589532403, 5),
        "signature" : {
            "hash" : BinData(0,"UkLPinHxT7rZ9EOxSNVTB4EHi+Q="),
            "keyId" : NumberLong("6826969049499435034")
        }
    }
}

指定數據庫lugotest里需要分片的集合test和片鍵id

db.runCommand( { shardcollection : "lugotest.moive",key : {points: "hashed"} } )

查看分片狀態

mongos> sh.status();
--- Sharding Status --- 
  sharding version: {
    "_id" : 1,
    "minCompatibleVersion" : 5,
    "currentVersion" : 6,
    "clusterId" : ObjectId("5ebe442e2923573eb3ecbdea")
  }
  shards:
        {  "_id" : "lugotestrepl",  "host" : "lugotestrepl/127.0.0.1:27017,127.0.0.1:27018,127.0.0.1:27019",  "state" : 1 }
        {  "_id" : "shard2",  "host" : "shard2/127.0.0.1:37017,127.0.0.1:37018,127.0.0.1:37019",  "state" : 1 }
  active mongoses:
        "3.6.18" : 1
  autosplit:
        Currently enabled: yes
  balancer:
        Currently enabled:  yes
        Currently running:  no
        Failed balancer rounds in last 5 attempts:  0
        Migration Results for the last 24 hours: 
                1 : Success
  databases:
        {  "_id" : "config",  "primary" : "config",  "partitioned" : true }
                config.system.sessions
                        shard key: { "_id" : 1 }
                        unique: false
                        balancing: true
                        chunks:
                                lugotestrepl    1
                        { "_id" : { "$minKey" : 1 } } -->> { "_id" : { "$maxKey" : 1 } } on : lugotestrepl Timestamp(1, 0) 
        {  "_id" : "lugotest",  "primary" : "lugotestrepl",  "partitioned" : true }
                lugotest.test
                        shard key: { "id" : "hashed" }
                        unique: false
                        balancing: true
                        chunks:
                                lugotestrepl    2
                                shard2  2
                        { "id" : { "$minKey" : 1 } } -->> { "id" : NumberLong("-4611686018427387902") } on : lugotestrepl Timestamp(3, 1) 
                        { "id" : NumberLong("-4611686018427387902") } -->> { "id" : NumberLong(0) } on : lugotestrepl Timestamp(3, 2) 
                        { "id" : NumberLong(0) } -->> { "id" : NumberLong("4611686018427387902") } on : shard2 Timestamp(3, 3) 
                        { "id" : NumberLong("4611686018427387902") } -->> { "id" : { "$maxKey" : 1 } } on : shard2 Timestamp(3, 4) 

插入數據

mongos> for (var i = 1; i <=20; i++){
... db.test.insert({id: i, username: "U"});
... }
WriteResult({ "nInserted" : 1 })

mongos> db.test.stats();
{
    "sharded" : true,
    "capped" : false,
    "warning" : "indexes don't all match - ok if ensureIndex is running",
    "ns" : "lugotest.test",
    "count" : 20,
    "size" : 1000,
    "storageSize" : 20480,
    "totalIndexSize" : 24576,
    "indexSizes" : {
        "_id_" : 20480,
        "id_hashed" : 4096
    },
    "avgObjSize" : 50,
    "nindexes" : 2,
    "nchunks" : 4,
    "shards" : {
        "lugotestrepl" : {
            "ns" : "lugotest.test",
            "size" : 300,

"shard2" : {
            "ns" : "lugotest.test",
            "size" : 700,
            "count" : 14,
            "avgObjSize" : 50,
            "storageSize" : 4096,
            "capped" : false,
            "wiredTiger" : {
                "metadata" : {
                    "formatVersion" : 1

可以看到,一個分片是6條數據,一個分片是14條數據

關閉第二個分片的所有節點,并驗證數據庫的可用性。

[root@test02 run]# systemctl stop mongod37017
[root@test02 run]# systemctl stop mongod37018
[root@test02 run]# systemctl stop mongod37019
mongos> use lugotest
switched to db lugotest
mongos> db.movie.find();
{ "_id" : ObjectId("5ebe402573a048240cf0e087"), "moviename" : "大偵探福爾摩斯", "points" : "9.5" }
{ "_id" : ObjectId("5ebe404173a048240cf0e088"), "moviename" : "掠奪", "points" : "9.2" }
{ "_id" : ObjectId("5ebe405073a048240cf0e089"), "moviename" : "搖滾黑幫", "points" : "9.9" }
{ "_id" : ObjectId("5ebe405b73a048240cf0e08a"), "moviename" : "兩桿大煙槍", "points" : "9.1" }
{ "_id" : ObjectId("5ebe406873a048240cf0e08b"), "moviename" : "殺死比爾", "points" : "9.0" }
mongos> db.test.find();
Error: error: {
    "ok" : 0,
    "errmsg" : "Could not find host matching read preference { mode: \"primary\" } for set shard2",
    "code" : 133,
    "codeName" : "FailedToSatisfyReadPreference",
    "operationTime" : Timestamp(1589534845, 1),
    "$clusterTime" : {
        "clusterTime" : Timestamp(1589534867, 1),
        "signature" : {
            "hash" : BinData(0,"3pklCXoPiE8B7ateFALSmCsDdfM="),
            "keyId" : NumberLong("6826969049499435034")
        }
    }
}

未分片的集合,且數據在未關閉的分片上,數據能正常使用
已分片的集合,數據不可查詢

重新啟用第二個分片的所有節點,使用 mongodump 備份數據庫中的測試數據。

備份

mongodump -h 127.0.0.1:22222 -u lugo -p test123 -d lugotest -o /data/mongodb_bak/20200512

清空lugotest的兩個集合

mongos> show collections;
movie
test
mongos> db.movie.drop()
true
mongos> db.test.drop()
true

還原數據

mongorestore -h 127.0.0.1:22222 -u lugo -p test123 --collection movie --db lugotest /data/mongodb_bak/20200512/lugotest/movie.bson
mongorestore -h 127.0.0.1:22222 -u lugo -p test123 --collection test --db lugotest /data/mongodb_bak/20200512/lugotest/test.bson

檢查數據

mongos> show collections;
movie
test
mongos> db.test.find();
{ "_id" : ObjectId("5ebe5eb9825f7f2e238543c8"), "id" : 2, "username" : "U" }
{ "_id" : ObjectId("5ebe5eb9825f7f2e238543d6"), "id" : 16, "username" : "U" }
{ "_id" : ObjectId("5ebe5eb9825f7f2e238543d7"), "id" : 17, "username" : "U" }
{ "_id" : ObjectId("5ebe5eb9825f7f2e238543c7"), "id" : 1, "username" : "U" }
{ "_id" : ObjectId("5ebe5eb9825f7f2e238543ca"), "id" : 4, "username" : "U" }
{ "_id" : ObjectId("5ebe5eb9825f7f2e238543cb"), "id" : 5, "username" : "U" }
{ "_id" : ObjectId("5ebe5eb9825f7f2e238543cd"), "id" : 7, "username" : "U" }
{ "_id" : ObjectId("5ebe5eb9825f7f2e238543cf"), "id" : 9, "username" : "U" }
{ "_id" : ObjectId("5ebe5eb9825f7f2e238543d0"), "id" : 10, "username" : "U" }
{ "_id" : ObjectId("5ebe5eb9825f7f2e238543d3"), "id" : 13, "username" : "U" }
{ "_id" : ObjectId("5ebe5eb9825f7f2e238543d4"), "id" : 14, "username" : "U" }
{ "_id" : ObjectId("5ebe5eb9825f7f2e238543d5"), "id" : 15, "username" : "U" }
{ "_id" : ObjectId("5ebe5eb9825f7f2e238543d8"), "id" : 18, "username" : "U" }
{ "_id" : ObjectId("5ebe5eb9825f7f2e238543d9"), "id" : 19, "username" : "U" }
{ "_id" : ObjectId("5ebe5eb9825f7f2e238543c9"), "id" : 3, "username" : "U" }
{ "_id" : ObjectId("5ebe5eb9825f7f2e238543cc"), "id" : 6, "username" : "U" }
{ "_id" : ObjectId("5ebe5eb9825f7f2e238543ce"), "id" : 8, "username" : "U" }
{ "_id" : ObjectId("5ebe5eb9825f7f2e238543d1"), "id" : 11, "username" : "U" }
{ "_id" : ObjectId("5ebe5eb9825f7f2e238543d2"), "id" : 12, "username" : "U" }
{ "_id" : ObjectId("5ebe5eb9825f7f2e238543da"), "id" : 20, "username" : "U" }

全部還原

?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市,隨后出現的幾起案子,更是在濱河造成了極大的恐慌,老刑警劉巖,帶你破解...
    沈念sama閱讀 228,546評論 6 533
  • 序言:濱河連續發生了三起死亡事件,死亡現場離奇詭異,居然都是意外死亡,警方通過查閱死者的電腦和手機,發現死者居然都...
    沈念sama閱讀 98,570評論 3 418
  • 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人,你說我怎么就攤上這事。” “怎么了?”我有些...
    開封第一講書人閱讀 176,505評論 0 376
  • 文/不壞的土叔 我叫張陵,是天一觀的道長。 經常有香客問我,道長,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 63,017評論 1 313
  • 正文 為了忘掉前任,我火速辦了婚禮,結果婚禮上,老公的妹妹穿的比我還像新娘。我一直安慰自己,他們只是感情好,可當我...
    茶點故事閱讀 71,786評論 6 410
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著,像睡著了一般。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發上,一...
    開封第一講書人閱讀 55,219評論 1 324
  • 那天,我揣著相機與錄音,去河邊找鬼。 笑死,一個胖子當著我的面吹牛,可吹牛的內容都是我干的。 我是一名探鬼主播,決...
    沈念sama閱讀 43,287評論 3 441
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了?” 一聲冷哼從身側響起,我...
    開封第一講書人閱讀 42,438評論 0 288
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后,有當地人在樹林里發現了一具尸體,經...
    沈念sama閱讀 48,971評論 1 335
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內容為張勛視角 年9月15日...
    茶點故事閱讀 40,796評論 3 354
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發現自己被綠了。 大學時的朋友給我發了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 42,995評論 1 369
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖,靈堂內的尸體忽然破棺而出,到底是詐尸還是另有隱情,我是刑警寧澤,帶...
    沈念sama閱讀 38,540評論 5 359
  • 正文 年R本政府宣布,位于F島的核電站,受9級特大地震影響,放射性物質發生泄漏。R本人自食惡果不足惜,卻給世界環境...
    茶點故事閱讀 44,230評論 3 347
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧,春花似錦、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 34,662評論 0 26
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至,卻和暖如春,著一層夾襖步出監牢的瞬間,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 35,918評論 1 286
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人。 一個月前我還...
    沈念sama閱讀 51,697評論 3 392
  • 正文 我出身青樓,卻偏偏與公主長得像,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當晚...
    茶點故事閱讀 47,991評論 2 374