mongodb学习(五) 搭建mongodb集群

准备两台机器192.168.100.87,192.168.100.91
1.搭建分片,副本集rep1,rep2
分别在每个机器创建目录/root/mongo/rep1/log,/root/mongo/rep1/data,/root/mongo/rep1/conf,conf中创建文件mongo.cfg

systemLog:
  destination: file
  path: /root/mongo/repl1/log/mongodb.log
  logAppend: true
storage:
  journal:
    enabled: true
  dbPath: /root/mongo/repl1/data
  directoryPerDB: true
  wiredTiger:
    engineConfig:
      directoryForIndexes: true
    collectionConfig:
      blockCompressor: zlib
    indexConfig:
      prefixCompression: true
processManagement:
  fork: true
net:
  port: 37017
  bindIp: 0.0.0.0
replication:
  oplogSizeMB: 2048
  replSetName: rep1
sharding:
    clusterRole: shardsv

./mongod -f /root/mongo/repl1/conf/mongo.cf分别启动每个mongo服务
mongo --port 37017连接一台机器

config = {_id: 'rep1', members: [
                          {_id: 0, host: '192.168.100.87:37017'},
                          {_id: 1, host: '192.168.100.91:37017'}
                          ]
          }
          
rs.initiate(config)
rs.conf()
rs.status()

rep2同rep1配置
2. 配置config副本,按下面的配置三个服务
分别在每个机器创建目录/root/mongo/config1/log,/root/mongo/config1/data,/root/mongo/config1/conf,conf中创建文件mongo.cfg

# where to write logging data.
systemLog:
  destination: file
  logAppend: true
  path: /root/mongo/config1/log/config.log
 
# Where and how to store data.
storage:
  dbPath: /root/mongo/config1/data
  journal:
    enabled: true
 
# how the process runs
processManagement:
  fork: true
 
# network interfaces
net:
  port: 47017
  bindIp: 0.0.0.0
 
#operationProfiling:
replication:
    replSetName: configs       
 
sharding:
    clusterRole: configsvr

同rep1配置副本

3.配置路由
分别在每个机器创建目录/root/mongo/mongos/log,/root/mongo/mongos/conf,conf中创建文件mongo.cfg

分别配置

systemLog:
  destination: file
  logAppend: true
  path: /root/mongo/mongos/log/mongo.log
processManagement:
  fork: true
net:
  port: 57017
  bindIp: 0.0.0.0
sharding:
  configDB: configs/192.168.100.87:47017,192.168.100.87:47018,192.168.100.91:47017

登陆任意一个路由

use admin
sh.addShard("rep1/192.168.100.87:37017,192.168.100.91:37017")
sh.addShard("rep2/192.168.100.87:37018,192.168.100.91:37018")

sh.status()

4.使用分片

use testShard
for(i=1;i<=50000;i++){db.user.insert({"name":"zhangsan"+i,"age":18})}
sh.enableSharding("testShard")
sh.status()能查看到分片信息

参考:https://blog.csdn.net/hezhixiang/article/details/82905786
https://blog.csdn.net/kevinmcy/article/details/82712074

上一篇 搭建mongodb副本集

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值