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阅读 212,332评论 6 493
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 90,508评论 3 385
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 157,812评论 0 348
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 56,607评论 1 284
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 65,728评论 6 386
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 49,919评论 1 290
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 39,071评论 3 410
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 37,802评论 0 268
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 44,256评论 1 303
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 36,576评论 2 327
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 38,712评论 1 341
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 34,389评论 4 332
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 40,032评论 3 316
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 30,798评论 0 21
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 32,026评论 1 266
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 46,473评论 2 360
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 43,606评论 2 350