温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

MongoDB Master/Slaver配置

发布时间:2020-06-19 19:51:48 来源:网络 阅读:2081 作者:richie_hu 栏目:MongoDB数据库
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来运行了。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI