Mongo集群搭建

来源:互联网 发布:招商黄金分析软件 编辑:程序博客网 时间:2024/04/26 15:41

单节点安装

  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.confport=27017dbpath=../datalogpath=../log/mongodb.loglogappend=truejournal=truemaster=true
> call mongod.exe -f ../conf/master.conf
  • 配置并启动从库
#slave.confport=27018dbpath=../datalogpath=../log/mongodb.loglogappend=truejournal=trueslave=truesource=127.0.0.1:27017
> call mongod.exe -f ../conf/slave.conf

3.0之后采用yaml方式配置

  • 配置并启动3个普通点
# config in yamlsystemLog:   destination: file   quiet: true   path: "../log/mongodb.log"   logAppend: truestorage:   dbPath: "../data"   journal:      enabled: truenet:   bindIp: 127.0.0.1   port: 27017setParameter:   enableLocalhostAuthBypass: falsesecurity:    # 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 yamlsystemLog:   destination: file   quiet: true   path: "../log/mongodb.log"   logAppend: truestorage:   dbPath: "../data"   journal:      enabled: true   wiredTiger:      engineConfig:         cacheSizeGB: 1net:   bindIp: 127.0.0.1   port: 27020setParameter:   enableLocalhostAuthBypass: falsesecurity:   authorization: "disabled"sharding:    # configsvr   clusterRole: "shardsvr"

批处理

title shard1cd bincall mongod.exe -f ../conf/mongo.confpause
  • 配置config server
# config in yamlsystemLog:   destination: file   quiet: true   path: "../log/config.log"   logAppend: truestorage:   dbPath: "../configData"   journal:      enabled: truenet:   bindIp: 127.0.0.1   port: 37017setParameter:   enableLocalhostAuthBypass: falsesecurity:   authorization: "disabled"sharding:    # configsvr   clusterRole: "configsvr"

​ 启动config

title configcd bincall mongod.exe -f ../conf/mongo-config.confpause
  • 配置mongos
systemLog:   destination: file   quiet: true   path: "../log/mongos.log"   logAppend: truenet:   bindIp: 127.0.0.1   port: 30000sharding:   configDB: 127.0.0.1:37017

​ 启动mongos(router)

title monogscd bincall mongos.exe -f ../conf/mongos.confpause

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

mongos.exe --logpath=../log/mongos.log --configdb=127.0.0.1:27019 --logappend --port 30000
  • 在mongos中配置shard
use adminmongos> 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,默认64Muse configdb.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 admindb.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
原创粉丝点击