MongoDB 基于文档的分布式非关系数据库
分片:体现水平扩展性
副本集:体现高可用性
四台机器
M:172.31.140.161 mongos
A:172.31.140.157 configserver shard1 shard2 shard3
B:172.31.140.158 configserver shard1 shard2 shard3
C:172.31.140.159 configserver shard1 shard2 shard3
configserver shard1 shard2 shard3均为三节点副本集。
1、下载安装文件
cd /root/data/soft
wget https://fastdl.mongodb.org/linux/mongodb-linux-x86_64-rhel70-3.4.4.tgz
tar -xzvf mongodb-linux-x86_64-rhel70-3.4.4.tgz
2、环境变量
vim ~/.bash_profile
export MONGODB=/root/data/soft/mongodb-linux-x86_64-rhel70-3.4.4/bin
export PATH=$MONGODB:$PATH
source ~/.bash_profile
mongod --version
3、configserver配置文件与启动方法
mongodb.conf
dbpath=/root/data/soft/mongodb/configsvr/db/ logpath=/root/data/soft/mongodb/configsvr/log/mongodb.log logappend=true port = 27101 fork = true maxConns = 1000 journal = true # PID File 的完整路径,如果没有设置,则没有PID文件 pidfilepath = /root/data/soft/mongodb/configsvr/m.pid #数据写入磁盘的时间秒数 syncdelay = 60 #设置oplog的大小(MB) oplogSize = 1000 #设置信数据库.ns文件大小(MB) nssize = 16 auth = false configsvr = true replSet=config_repl
分别在A B C配置一份,启动三个节点,命令
mongod -f /root/data/soft/mongodb/configsvr/mongodb.conf
登入
mongo --port 27101
配置configserver的副本集
config = { _id : "config_repl", members : [ {_id : 0, host : "172.31.140.157:27101" }, {_id : 1, host : "172.31.140.158:27101" }, {_id : 2, host : "172.31.140.159:27101" } ] }
初始化
> > config = { ... _id : "config_repl", ... members : [ ... {_id : 0, host : "172.31.140.157:27101" }, ... {_id : 1, host : "172.31.140.158:27101" }, ... {_id : 2, host : "172.31.140.159:27101" } ... ] ... } { "_id" : "config_repl", "members" : [ { "_id" : 0, "host" : "172.31.140.157:27101" }, { "_id" : 1, "host" : "172.31.140.158:27101" }, { "_id" : 2, "host" : "172.31.140.159:27101" } ] } >
> rs.initiate(config)
{ "ok" : 1 }
config_repl:SECONDARY>
config_repl:PRIMARY>
config_repl:PRIMARY>
config_repl:PRIMARY> rs.status()
4、分片shard1的配置文件与启动方法
dbpath=/root/data/soft/mongodb/shard1/db/ logpath=/root/data/soft/mongodb/shard1/log/mongodb.log logappend=true port = 27201 fork = true maxConns = 1000 journal = true # PID File 的完整路径,如果没有设置,则没有PID文件 pidfilepath = /root/data/soft/mongodb/shard1/m.pid #数据写入磁盘的时间秒数 syncdelay = 60 #设置oplog的大小(MB) oplogSize = 1000 #设置信数据库.ns文件大小(MB) nssize = 16 auth = false shardsvr = true replSet=shard1_repl
分别在 A B C各配一次,启动
mongod -f /root/data/soft/mongodb/shard1/mongodb.conf
登录
mongo --port 27201
初始化副本集
> > rs.status() { "info" : "run rs.initiate(...) if not yet done for the set", "ok" : 0, "errmsg" : "no replset config has been received", "code" : 94, "codeName" : "NotYetInitialized" } > > > > > > > config = { ... _id : "shard1_repl", ... members : [ ... {_id : 0, host : "172.31.140.157:27201" }, ... {_id : 1, host : "172.31.140.158:27201" }, ... {_id : 2, host : "172.31.140.159:27201" } ... ] ... } { "_id" : "shard1_repl", "members" : [ { "_id" : 0, "host" : "172.31.140.157:27201" }, { "_id" : 1, "host" : "172.31.140.158:27201" }, { "_id" : 2, "host" : "172.31.140.159:27201" } ] } > > rs.initiate(config) { "ok" : 1 } shard1_repl:OTHER> rs.status()
以同样的方式启动、初始化 shard2、shard3。
5、mongos配置文件与启动方法
在M机器配置
mongodb.conf
logpath=/root/data/soft/mongodb/mongos/log/mongodb.log logappend=true port = 27017 fork = true maxConns = 1000 pidfilepath = /root/data/soft/mongodb/mongos/m.pid configdb=config_repl/172.31.140.157:27101,172.31.140.158:27101,172.31.140.159:27101
mongos只有log,不记录数据,从configserver取得数据存入内存,作路由使用
其中 configdb为 configserver的三节点副本集
启动
mongos -f /root/data/soft/mongodb/mongos/mongodb.conf
登录
mongo --port 27017
mongos> sh.status() --- Sharding Status --- sharding version: { "_id" : 1, "minCompatibleVersion" : 5, "currentVersion" : 6, "clusterId" : ObjectId("5efa2a6d6e156de96ae022a6") } shards: active mongoses: "3.4.4" : 1 autosplit: Currently enabled: yes balancer: Currently enabled: yes Currently running: no Balancer lock taken at Tue Jun 30 2020 01:52:49 GMT+0800 (CST) by ConfigServer:Balancer Failed balancer rounds in last 5 attempts: 0 Migration Results for the last 24 hours: No recent migrations databases: mongos> mongos>
添加分片,每个分片都是一个副本集
sh.addShard("shard1_repl/172.31.140.157:27201,172.31.140.158:27201,172.31.140.159:27201") sh.addShard("shard2_repl/172.31.140.157:27202,172.31.140.158:27202,172.31.140.159:27202") sh.addShard("shard3_repl/172.31.140.157:27203,172.31.140.158:27203,172.31.140.159:27203")
mongos> sh.status() --- Sharding Status --- sharding version: { "_id" : 1, "minCompatibleVersion" : 5, "currentVersion" : 6, "clusterId" : ObjectId("5efa2a6d6e156de96ae022a6") } shards: { "_id" : "shard1_repl", "host" : "shard1_repl/172.31.140.157:27201,172.31.140.158:27201,172.31.140.159:27201", "state" : 1 } { "_id" : "shard2_repl", "host" : "shard2_repl/172.31.140.157:27202,172.31.140.158:27202,172.31.140.159:27202", "state" : 1 } { "_id" : "shard3_repl", "host" : "shard3_repl/172.31.140.157:27203,172.31.140.158:27203,172.31.140.159:27203", "state" : 1 } active mongoses: "3.4.4" : 1 autosplit: Currently enabled: yes balancer: Currently enabled: yes Currently running: no Balancer lock taken at Tue Jun 30 2020 01:52:49 GMT+0800 (CST) by ConfigServer:Balancer Failed balancer rounds in last 5 attempts: 0 Migration Results for the last 24 hours: No recent migrations databases: { "_id" : "loginserver", "primary" : "shard1_repl", "partitioned" : false } { "_id" : "loginsaveserver", "primary" : "shard2_repl", "partitioned" : false } mongos> mongos> mongos>
至此,基于分片、副本集的MongoDB集群搭完成。
6、启用分片功能
对库启用分片
sh.enableSharding("loginserver")
mongos> mongos> mongos> sh.status() --- Sharding Status --- sharding version: { "_id" : 1, "minCompatibleVersion" : 5, "currentVersion" : 6, "clusterId" : ObjectId("5efa2a6d6e156de96ae022a6") } shards: { "_id" : "shard1_repl", "host" : "shard1_repl/172.31.140.157:27201,172.31.140.158:27201,172.31.140.159:27201", "state" : 1 } { "_id" : "shard2_repl", "host" : "shard2_repl/172.31.140.157:27202,172.31.140.158:27202,172.31.140.159:27202", "state" : 1 } { "_id" : "shard3_repl", "host" : "shard3_repl/172.31.140.157:27203,172.31.140.158:27203,172.31.140.159:27203", "state" : 1 } active mongoses: "3.4.4" : 1 autosplit: Currently enabled: yes balancer: Currently enabled: yes Currently running: no Balancer lock taken at Tue Jun 30 2020 01:52:49 GMT+0800 (CST) by ConfigServer:Balancer Failed balancer rounds in last 5 attempts: 0 Migration Results for the last 24 hours: No recent migrations databases: { "_id" : "loginserver", "primary" : "shard1_repl", "partitioned" : true } { "_id" : "loginsaveserver", "primary" : "shard2_repl", "partitioned" : true } mongos>