欢迎光临
我们一直在努力

MongoDB Master/Slaver配置

MongoDB快速入门

本文主要介绍MongoDB Master/Slaver配置

  1. 首先创建Mongo Master/home/hrj/mongodb-linux-i686-static-1.6.5/bin/mongod –master –dbpath /home/hrj/mongodb_data –auth –maxConns 50    –port 6688
  2. 其次创建Mongo Slaver/home/hrj/mongodb-linux-i686-static-1.6.5/bin/mongod –slave –source myna5.sds.cnb.yahoo.com:6688 –auth –maxConns 50 –auth    –port 6689 –fastsync –autoresync –dbpath /home/hrj/mongodb_slave_data
  3. 创建Master、Slaver帐号~/mongodb-linux-i686-static-1.6.5/bin/mongo 127.0.0.1:6689 ### 在Slaver上创建帐号
    > use local
    switched to db local
    > db.addUser(‘hrj’,’xxx’)
    {
                    “_id” : ObjectId(“4d6f6527013fcbcc74575c20”),
                    “user” : “hrj”,
                    “readOnly” : false,
                    “pwd” : “b27edaa5a8858aa3d46b60698fce1359”

     ~/mongodb-linux-i686-static-1.6.5/bin/mongo 127.0.0.1:6688 ### 在Master上创建帐号
    MongoDB shell version: 1.6.5
    connecting to: 127.0.0.1:6688/test
    >use local
    switched to db local
    > db.addUser(‘hrj’,’xxx’)
    {
                    “_id” : ObjectId(“4d6f6527013fcbcc74575c20”),
                    “user” : “hrj”,
                    “readOnly” : false,
                    “pwd” : “b27edaa5a8858aa3d46b60698fce1359”
    }
    >use test ###添加帐号认证
    switched to db local
    > db.auth(‘hrj’,’xxx’)
  4. 向Master加载数据,测试Slaver是否正常同步###Master
    ~/mongodb-linux-i686-static-1.6.5/bin/mongo 127.0.0.1:6688
    MongoDB shell version: 1.6.5
    connecting to: 127.0.0.1:6688/test                                
    > db.foo.save({‘mongodb’:’hello world’}) ###导入数据
    > db.foo.find()   ###查询数据                                             
    { “_id” : ObjectId(“4d6f72c6d807e8561b0f3db5”), “mongodb” : “hello world” }
    ###Slaver
    ~/mongodb-linux-i686-static-1.6.5/bin/mongo 127.0.0.1:6689
    MongoDB shell version: 1.6.5
    connecting to: 127.0.0.1:6689/test
    > db.foo.find() ###查询Slaver同步数据
    { “_id” : ObjectId(“4d6f72c6d807e8561b0f3db5”), “mongodb” : “hello world” }
    > db.foo.save({‘mongodb’:’hello world test 1′}) ###Slaver导入数据,提示”not master”
    not master

至此Master/Slaver大致调试成功。在这里大致提一下Master/Slaver特别参数:

Master

    –master                            master模式
    –oplogSize arg             size limit (in MB) for op log

Slave

    –slave                             slave模式
    –source arg                    source指定master位置
    –only arg                        单独指定备份某一database
    –slavedelay arg            指定与Master延迟时间(秒)
    –autoresync                    当Slave数据过时后自动重连

特别推荐:
     很可能会出现Master服务器Down掉之后,需要用Slave服务器来顶替Master提供服务器的情况,这个时候就需要做如下操作:

  1. 停止Slave进程(mongod)
  2. 删除Slave数据目录中的local.*
  3. 以–master模式启动Slave

这个时候,Slave就可以作为一个Master来运行了。

赞(0)
【声明】:本博客不参与任何交易,也非中介,仅记录个人感兴趣的主机测评结果和优惠活动,内容均不作直接、间接、法定、约定的保证。访问本博客请务必遵守有关互联网的相关法律、规定与规则。一旦您访问本博客,即表示您已经知晓并接受了此声明通告。