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
创建各节点文件夹
创建存放config
、log
、data
的文件夹
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
————        END        ————
Give me a Star, Thanks:)
https://github.com/fendoudebb/LiteNote扫描下方二维码关注公众号和小程序↓↓↓