Mongo集群搭建

单节点安装

  1. 复制文件到目录D:\CFile\MongoDB

  2. 注册windows服务(可能需要以管理员运行cmd)

    新建D:\CFile\MongoDB\data D:\CFile\MongoDB\log\mongo.log

> mongod --dbpath "D:\CFile\MongoDB\data" --logpath "D:\CFile\MongoDB\log\mongo.log" --install --serviceName "MongoDB"
> net start mongodb
> net stop mongodb
> mongod --remove --serviceName "MongoDB"
> mongod --auth --dbpath "D:\CFile\MongoDB\data" --logpath "D:\CFile\MongoDB\log\mongo.log" --reinstall --serviceName "MongoDB"
  1. 初始化
> mongo 127.0.0.1:27017
> use admin
> db.createUser({
    user: "admin",
    pwd: "123456",
    // 此角色只有管理用户的权限
    roles: [ { role: "userAdminAnyDatabase", db: "admin" } ]
  })
// 授予root权限
db.grantRolesToUser(
    "admin",
    [{ role: "root", db: "admin" }]
)

复制集群

旧版本采用properties的方式进行配置(现在的版本也兼容)

  • 配置并启动主库
#master.conf
port=27017
dbpath=../data
logpath=../log/mongodb.log
logappend=true
journal=true

master=true
> call mongod.exe -f ../conf/master.conf
  • 配置并启动从库
#slave.conf
port=27018
dbpath=../data
logpath=../log/mongodb.log
logappend=true
journal=true

slave=true
source=127.0.0.1:27017
> call mongod.exe -f ../conf/slave.conf

3.0之后采用yaml方式配置

  • 配置并启动3个普通点
# config in yaml
systemLog:
   destination: file
   quiet: true
   path: "../log/mongodb.log"
   logAppend: true
storage:
   dbPath: "../data"
   journal:
      enabled: true
net:
   bindIp: 127.0.0.1
   port: 27017
setParameter:
   enableLocalhostAuthBypass: false
security:
    # enabled后各种校验问题
   authorization: "disabled"
replication:  
    oplogSizeMB: 100  
    replSetName: rs0
call mongod.exe -f ../conf/mongo.conf
  • 启动三个独立节点后,初始化每个节点的角色
use admin;

var cfg={_id:"rs0",
    members:[
        {_id:1,host:"127.0.0.1:27017",priority:1},
        {_id:2,host:"127.0.0.1:27018",priority:2},
        {_id:3,host:"127.0.0.1:27019",arbiterOnly:true}
    ]};
rs.initiate(cfg);

之后操作主节点,备份节点即可看到变化。初始化结束后便可以在主节点上建立用户。

分片集群

  • 配置并启动shard server(2个点以上)
# config in yaml
systemLog:
   destination: file
   quiet: true
   path: "../log/mongodb.log"
   logAppend: true
storage:
   dbPath: "../data"
   journal:
      enabled: true
   wiredTiger:
      engineConfig:
         cacheSizeGB: 1
net:
   bindIp: 127.0.0.1
   port: 27020
setParameter:
   enableLocalhostAuthBypass: false
security:
   authorization: "disabled"
sharding:
    # configsvr
   clusterRole: "shardsvr"

批处理

title shard1
cd bin
call mongod.exe -f ../conf/mongo.conf
pause
  • 配置config server
# config in yaml
systemLog:
   destination: file
   quiet: true
   path: "../log/config.log"
   logAppend: true
storage:
   dbPath: "../configData"
   journal:
      enabled: true
net:
   bindIp: 127.0.0.1
   port: 37017
setParameter:
   enableLocalhostAuthBypass: false
security:
   authorization: "disabled"
sharding:
    # configsvr
   clusterRole: "configsvr"

​ 启动config

title config
cd bin
call mongod.exe -f ../conf/mongo-config.conf
pause
  • 配置mongos
systemLog:
   destination: file
   quiet: true
   path: "../log/mongos.log"
   logAppend: true
net:
   bindIp: 127.0.0.1
   port: 30000
sharding:
   configDB: 127.0.0.1:37017

​ 启动mongos(router)

title monogs
cd bin
call mongos.exe -f ../conf/mongos.conf
pause

​ 也可以通过如下方式指定cofigDB

mongos.exe --logpath=../log/mongos.log --configdb=127.0.0.1:27019 --logappend --port 30000
  • 在mongos中配置shard
use admin
mongos> db.runCommand({ addshard:"127.0.0.1:27020" })
mongos> db.runCommand({ addshard:"127.0.0.1:27021" })

mongos> db.runCommand( { enablesharding :"testdb"});
//  使集合users以片键id来分片
mongos> db.runCommand( { shardcollection : "testdb.user",key : {id: 1} } )
mongos> db.runCommand( { shardcollection : "testdb.user",key : {id: "hashed"} } )

sh.status()
printShardingStatus()
db.runCommand( { listshards : 1 } );
// 修改chunk size,默认64M
use config
db.settings.find();
db.stats();
db.serverStatus();
db.currentOP();
db.user.stats();

// 构造数据
mongos> use testdb;
mongos> for (var i = 1; i <= 1000; i++){
    db.user.save({id:i,"name":"vv"+i});
}
  • 临时增加或者删除shard
use admin
db.runCommand({ addshard:"127.0.0.1:27020" })
db.runCommand({ removeshard:"127.0.0.1:27022" })

FAQ

  • 第一次创建用户时,若没有权限就将校验关了,用户建好后开启权限校验。

  • 多个文档形成一个chunk,当各个sharding上的chunk差距>=3时就会重新移动。这样可能导致chunk来回移动

  • 手动预先分块,指定分块点
mongos> for(var i=1; i<10; i++){
  // chunk的分界点在1万, 2万...位置
    sh.splitAt('testdb.user', {userId: i*10000})
}
  • 注意yaml配置文件中冒号后有空格,如 cacheSizeGB: 1
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值