mongodb 分片集群安装 -- 二进制文件安装

一、安装前准备

机器分配: 三台机器 A :192.168.19.101 B: 192.168.19.102 C: 192.168.19.103
A机器:mongos 、config server、shard1主节点、shard2仲裁、shard3从节点
B机器:mongos 、config server、shard1从节点、shard2主节点、shard3仲裁
C机器:mongos 、config server、shard1仲裁、shard2从节点、shard3主节点
端口分配:mongos 20000、config 21000 shard1 27001 shard2 27002、shard3 27003
三台机器全部关闭firewalld服务和selinunx,或者增加相应端口规则

2.目录结构,使basedir 和 datadir 不在一个分区上
basedir: /usr/local/mongodb/
datadir: /data/mongodb/

3.禁用大内存页面

二、下载 解压 创建相关目录

# wget https://fastdl.mongodb.org/linux/mongodb-linux-x86_64-rhel70-4.0.9.tgz
# tar zxvfmongodb-linux-x86_64-rhel70-4.0.9.tgz
# mv mongodb-linux-x86_64-rhel70-4.0.9 /usr/local/mongodb

# 创建相关目录
# mkdir -p /data/mongodb/mongos/log ;mkdir -p /data/mongodb/config/{data,log};mkdir -p /data/mongodb/shard1/{data,log};mkdir -p /data/mongodb/shard2/{data,log};mkdir -p /data/mongodb/shard3/{data,log}

# 设置环境变量
# cat /etc/profile.d/mongodb.sh
#!/bin/bash
MONGODB_HOME=/usr/local/mongodb/
PATH=$PATH:$MONGODB_HOME/bin
export MONGODB_HOME PATH

# . /etc/profile.d/mongodb.sh		#使环境变量生效

三、分片集群搭建
1.config server的配置,三台机器都需要操作

# pwd
/usr/local/mongodb/conf
# mkdir  /usr/local/mongodb/pids		#存放服务的pid 目录

# cat config.conf 			# config 配置文件
pidfilepath = /usr/local/mongodb/pids/configsrv.pid
dbpath = /data/mongodb/config/data
logpath = /data/mongodb/config/log/configsrv.log
logappend = true
bind_ip = 0.0.0.0
port = 21000
fork = true
configsvr = true
replSet = configs
maxConns = 20000

#启动
# /usr/local/mongodb/bin/mongod -f /usr/local/mongodb/conf/config.conf 

#初始化副本集,任意一台机器都可以
# mongo --host 192.168.19.101 --port 21000
> config = {_id:"configs", members:[{_id:101, host:"192.168.19.101:21000"},{_id:102, host:"192.168.19.102:21000"},{_id:103, host:"192.168.19.103:21000"}]}
> rs.initiate(config)
configs:SECONDARY> rs.status()			#等一会 当前机器 状态会变为 PRIMARY
configs:PRIMARY> 

2.shard的配置,三台机器都需要操作

###############   以下是shard1的配置   ##############
# cat /usr/local/mongodb/conf/shard1.conf 
pidfilepath = /usr/local/mongodb/pids/shard1.pid
dbpath = /data/mongodb/shard1/data
logpath = /data/mongodb/shard1/log/shard1.log
logappend = true
bind_ip = 0.0.0.0
port = 27001
fork = true
replSet = shard1
shardsvr = true
maxConns = 20000

# 启动,三个节点均需要启动
# /usr/local/mongodb/bin/mongod -f /usr/local/mongodb/conf/shard1.conf
#连接mongo 配置shard1
# mongo --host 192.168.19.101 --port 27001	
> config = {_id:"shard1",members:[{_id:101,host:"192.168.19.101:27001"},{_id:102,host:"192.168.19.102:27001"},{_id:103,host:"192.168.19.103:27001",arbiterOnly:true}]}
> rs.initiate(config)
shard1:SECONDARY> rs.status()
shard1:PRIMARY> 


###############   以下是shard2的配置   ##############
# cat /usr/local/mongodb/conf/shard2.conf 
pidfilepath = /usr/local/mongodb/pids/shard2.pid
dbpath = /data/mongodb/shard2/data
logpath = /data/mongodb/shard2/log/shard2.log
logappend = true
bind_ip = 0.0.0.0
port = 27002
fork = true
replSet = shard2
shardsvr = true
maxConns = 20000

# 启动,三个节点均需要启动
# mongod -f /usr/local/mongodb/conf/shard2.conf
#连接mongo 配置shard2
# mongo --host 192.168.19.102 --port 27002
> config = {_id:"shard2", members:[{_id:101, host:"192.168.19.101:27002",arbiterOnly:true}, {_id:102, host:"192.168.19.102:27002"},{_id:103, host:"192.168.19.103:27002"}]}
> rs.initiate(config)
shard2:SECONDARY> rs.status()
shard2:PRIMARY> 


###############   以下是shard3的配置   ##############
# cat /usr/local/mongodb/conf/shard3.conf
pidfilepath = /usr/local/mongodb/pids/shard3.pid
dbpath = /data/mongodb/shard3/data
logpath = /data/mongodb/shard3/log/shard3.log
logappend = true
bind_ip = 0.0.0.0
port = 27003
fork = true
replSet = shard3
shardsvr = true
maxConns = 20000

# 启动,三个节点均需要启动
# mongod -f /usr/local/mongodb/conf/shard3.conf
#连接mongo 配置shard3
# mongo --host 192.168.19.103 --port 27003
> config = {_id:"shard3", members:[{_id:101,host:"192.168.19.101:27003"},{_id:102,host:"192.168.19.102:27003",arbiterOnly:true},{_id:103,host:"192.168.19.103:27003"}]}
> rs.initiate(config)
shard3:SECONDARY> rs.status()
shard3:PRIMARY> 

3 mongs配置

# cat /usr/local/mongodb/conf/mongos.conf
pidfilepath = /usr/local/mongodb/pids/mongos.pid
logpath = /data/mongodb/mongos/log/mongos.log
logappend = true
bind_ip = 0.0.0.0
port = 20000
fork = true
configdb = configs/192.168.19.101:21000,192.168.19.102:21000,192.168.19.103:21000	# 监听的配置服务器,只能有1个或3个,configs为配置服务器的副本集名称
maxConns = 20000

# 启动,三个节点均需要启动
# mongos -f /usr/local/mongodb/conf/mongos.conf 

# 连接mongo 配置mongos  把所有的分片和路由器串联
# mongo --host 192.168.19.101 --port 20000
mongos> sh.addShard("shard1/192.168.19.101:27001,192.168.19.102:27001,192.168.19.103:27001")
mongos> sh.addShard("shard2/192.168.19.101:27002,192.168.19.102:27002,192.168.19.103:27002")
mongos> sh.addShard("shard3/192.168.19.101:27003,192.168.19.102:27003,192.168.19.103:27003")
mongos> sh.status()			# 查看状态

四、分片测试

mongos> use admin
mongos> db.runCommand( { enablesharding :"test"});
mongos> db.runCommand( { shardcollection : "test.table",key : {id: "hashed"} } )
mongos> use test
mongos> for (var i = 1; i <= 10000; i++) db.table.save({id:i,"test":"testval"})
mongos> db.table.stats()		#查看每个分区的数据量

https://www.oschina.net/question/3571801_2286249?sort=default

四、备份与恢复

#备份指定库
# mongodump --host 192.168.19.101 --port 20000 -d test -o /tmp/test
#备份所有库
# mongodump --host 192.168.19.101 --port 20000 -o /tmp/alldb
#备份指定集合
# mongodump --host 192.168.19.101 --port 20000 -d test -c table -o /tmp/test/


#恢复所有库
# mongorestore --host 192.168.19.101 --port 20000 --drop /tmp/alldb	 # --drop 可选 意思是恢复之前先把之前的数据删除,不建议使用
#恢复指定库
# mongorestore --host 192.168.19.101 --port 20000 -d test /tmp/test
#恢复集合
# mongorestore --host 192.168.19.101 --port 20000 -d test -c table /tmp/test/table.bson

五、其他
1 备份时过滤

# mongodump --host 192.168.19.101 --port 27017 -d touchless -c pousheng_validation_sdap  -q '{batch_id: { $in: [ "0001SU201", "0001SU202", "SU20A109210502316"] }, validate_status_flag: "Success"}' -o /cust/cig/data/mongodb/

2

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
以下是搭建MongoDB分布式集群的步骤: 1.下载MongoDB二进制文件并解压缩到三台服务器上。 2.在三台服务器上创建数据目录和日志目录,并分别启动三个配置服务器。 ```shell # 创建数据目录和日志目录 mkdir -p /data/mongodb/configdb /data/mongodb/shard1/rs0 /data/mongodb/shard2/rs0 /data/mongodb/shard3/rs0 mkdir -p /var/log/mongodb/configdb /var/log/mongodb/shard1/rs0 /var/log/mongodb/shard2/rs0 /var/log/mongodb/shard3/rs0 # 启动三个配置服务器 mongod -f /home/mongodb/conf/config.conf ``` 3.在三台服务器上启动三个路由器。 ```shell mongos --configdb configserver1:27019,configserver2:27019,configserver3:27019 ``` 4.在三台服务器上启动三个分片服务器。 ```shell # 启动分片服务器1 mongod --shardsvr --replSet rs0 --dbpath /data/mongodb/shard1/rs0 --logpath /var/log/mongodb/shard1/rs0.log --port 27017 # 启动分片服务器2 mongod --shardsvr --replSet rs0 --dbpath /data/mongodb/shard2/rs0 --logpath /var/log/mongodb/shard2/rs0.log --port 27018 # 启动分片服务器3 mongod --shardsvr --replSet rs0 --dbpath /data/mongodb/shard3/rs0 --logpath /var/log/mongodb/shard3/rs0.log --port 27019 ``` 5.在其中一个分片服务器上初始化副本集。 ```shell mongo --port 27017 rs.initiate() ``` 6.将其他两个分片服务器加入到副本集中。 ```shell rs.add("shard2:27018") rs.add("shard3:27019") ``` 7.将三个分片服务器添加到路由器中。 ```shell mongos> sh.addShard("rs0/shard1:27017,shard2:27018,shard3:27019") ``` 至此,MongoDB分布式集群搭建完成。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

sun_xuegang

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值