MongoDB 搭建分片集群

MongoDB About 5,530 words

下载社区版

查看系统版本,判断是32位还是64

uname -a

查看RedHat版本

cat /etc/redhat-release

可在package选择server(安装方式为rpm)或者tgz压缩包。此处选择的是tgz方式。

下载页面:https://www.mongodb.com/try/download/community

创建各节点文件夹

创建存放configlogdata的文件夹

mkdir -p /home/fendoudebb/mongodb/mongodb/node{1,2,3}/config
mkdir -p /home/fendoudebb/mongodb/mongodb/node{1,2,3}/log
mkdir -p /home/fendoudebb/mongodb/mongodb/node{1,2,3}/data/shard{1,2,3}

编写各节点配置文件

shard服务配置文件

systemLog:
    destination: file
    path: "/home/fendoudebb/mongodb/mongodb/node1/log/shard1.log"
    logAppend: true
    logRotate: "reopen"
storage:
    journal:
        enabled: true
    dbPath: "/home/fendoudebb/mongodb/mongodb/node1/data/shard1"
processManagement:
    fork: true
net:
    bindIp: 0.0.0.0
    port: 21001
setParameter:
    enableLocalhostAuthBypass: true
replication:
    replSetName: "rs1"
sharding:
    clusterRole: shardsvr

config服务配置文件

systemLog:
    destination: file
    path: "/home/fendoudebb/mongodb/mongodb/node1/log/csvr.log"
    logAppend: true
    logRotate: "reopen"
storage:
    journal:
        enabled: true
    dbPath: "/home/fendoudebb/mongodb/mongodb/node1/data/csvr"
processManagement:
    fork: true
net:
    bindIp: 0.0.0.0
    port: 24001
setParameter:
    enableLocalhostAuthBypass: true
replication:
    replSetName: "csvr"
sharding:
    clusterRole: configsvr

mongos服务配置文件

systemLog:
    destination: file
    path: "/home/fendoudebb/mongodb/mongodb/node1/log/mongos.log"
    logAppend: true
    logRotate: "reopen"
processManagement:
    fork: true
net:
    bindIp: 0.0.0.0
    port: 20001
setParameter:
    enableLocalhostAuthBypass: true
sharding:
    configDB: csvr/127.0.0.1:24001,127.0.0.1:24002,127.0.0.1:24003

配置 shard 节点

启动shard服务

/home/fendoudebb/mongodb/mongodb/bin/mongod -f /home/fendoudebb/mongodb/mongodb/node1/config/shard1.yaml
/home/fendoudebb/mongodb/mongodb/bin/mongod -f /home/fendoudebb/mongodb/mongodb/node1/config/shard2.yaml
/home/fendoudebb/mongodb/mongodb/bin/mongod -f /home/fendoudebb/mongodb/mongodb/node1/config/shard3.yaml

/home/fendoudebb/mongodb/mongodb/bin/mongod -f /home/fendoudebb/mongodb/mongodb/node2/config/shard1.yaml
/home/fendoudebb/mongodb/mongodb/bin/mongod -f /home/fendoudebb/mongodb/mongodb/node2/config/shard2.yaml
/home/fendoudebb/mongodb/mongodb/bin/mongod -f /home/fendoudebb/mongodb/mongodb/node2/config/shard3.yaml

/home/fendoudebb/mongodb/mongodb/bin/mongod -f /home/fendoudebb/mongodb/mongodb/node3/config/shard1.yaml
/home/fendoudebb/mongodb/mongodb/bin/mongod -f /home/fendoudebb/mongodb/mongodb/node3/config/shard2.yaml
/home/fendoudebb/mongodb/mongodb/bin/mongod -f /home/fendoudebb/mongodb/mongodb/node3/config/shard3.yaml

进入shard1服务

/home/fendoudebb/mongodb/mongodb/bin/mongo --port 21001

初始化shard1的副本集

rs.initiate( {
   _id : "rs1",
   members: [
      { _id: 0, host: "127.0.0.1:21001" },
      { _id: 1, host: "127.0.0.1:22001" },
      { _id: 2, host: "127.0.0.1:23001" }
   ]
})

进入shard2服务

/home/fendoudebb/mongodb/mongodb/bin/mongo --port 21002

初始化shard2的副本集

rs.initiate( {
   _id : "rs2",
   members: [
      { _id: 0, host: "127.0.0.1:21002" },
      { _id: 1, host: "127.0.0.1:22002" },
      { _id: 2, host: "127.0.0.1:23002" }
   ]
})

进入shard3服务

/home/fendoudebb/mongodb/mongodb/bin/mongo --port 21003

初始化shard3的副本集

rs.initiate( {
   _id : "rs3",
   members: [
      { _id: 0, host: "127.0.0.1:21003" },
      { _id: 1, host: "127.0.0.1:22003" },
      { _id: 2, host: "127.0.0.1:23003" }
   ]
})

配置 config 节点

启动config服务

/home/fendoudebb/mongodb/mongodb/bin/mongod -f /home/fendoudebb/mongodb/mongodb/node1/config/csvr.yaml
/home/fendoudebb/mongodb/mongodb/bin/mongod -f /home/fendoudebb/mongodb/mongodb/node2/config/csvr.yaml
/home/fendoudebb/mongodb/mongodb/bin/mongod -f /home/fendoudebb/mongodb/mongodb/node3/config/csvr.yaml

进入config服务

/home/fendoudebb/mongodb/mongodb/bin/mongo --port 24001

初始化config的副本集

rs.initiate( {
   _id : "csvr",
   configsvr: true,
   members: [
      { _id: 0, host: "127.0.0.1:24001" },
      { _id: 1, host: "127.0.0.1:24002" },
      { _id: 2, host: "127.0.0.1:24003" }
   ]

})

配置 mongos 节点

启动mongos服务

注意:使用mongos命令启动,如果使用mongod启动会报Unrecognized option: sharding.configDB错误。

/home/fendoudebb/mongodb/mongodb/bin/mongos -f /home/fendoudebb/mongodb/mongodb/node1/config/mongos.yaml
/home/fendoudebb/mongodb/mongodb/bin/mongos -f /home/fendoudebb/mongodb/mongodb/node2/config/mongos.yaml
/home/fendoudebb/mongodb/mongodb/bin/mongos -f /home/fendoudebb/mongodb/mongodb/node3/config/mongos.yaml

进入mongos服务

/home/fendoudebb/mongodb/mongodb/bin/mongo --port 20001

初始化mongos的副本集

sh.addShard( "rs1/127.0.0.1:21001,127.0.0.1:22001,127.0.0.1:23001")
sh.addShard( "rs2/127.0.0.1:21002,127.0.0.1:22002,127.0.0.1:23002")
sh.addShard( "rs3/127.0.0.1:21003,127.0.0.1:22003,127.0.0.1:23003")

设置分片数据库、分片集合以及分片的key

admin数据库下进行,否则会报shardCollection may only be run against the admin database.错误。

use admin
db.runCommand( { enablesharding : "testdb" } )
db.runCommand( { shardcollection : "testdb.users",key : {id: 1} } )

测试分片

模拟数据

var arr=[];
for(var i=0;i<200;i++){
    var uid = i;
    var name = "mongodb"+i;
    arr.push({"id":uid,"name":name});
}
db.users.insertMany(arr);

查看分片状态

sh.status();

查看集群间的连接情况

db.adminCommand("connPoolStats");

服务降级

Primary节点降级为Secondary节点并维持60s,如果这段时间内没有新的Primary被选举出来,这个节点可以要求重新进行选举。

rs.stepDown()

手动指定时间

rs.stepDown(30)

参考

MongoDB分片集群部署:https://docs.mongodb.com/manual/tutorial/deploy-shard-cluster

MongoDB配置文件选项:https://docs.mongodb.com/manual/reference/configuration-options

MongoDB常见FAQ:https://docs.mongodb.com/manual/faq/sharding

Views: 4,075 · Posted: 2020-09-30

————        END        ————

Give me a Star, Thanks:)

https://github.com/fendoudebb/LiteNote

扫描下方二维码关注公众号和小程序↓↓↓

扫描下方二维码关注公众号和小程序↓↓↓


Today On History
Browsing Refresh