文章详情

短信预约-IT技能 免费直播动态提醒

请输入下面的图形验证码

提交验证

短信预约提醒成功

mongodb主从复制安装文档

2024-04-02 19:55

关注

IPRole
10.240.216.151master
10.240.216.152slave(standby master)
10.240.216.153slave


# wget http://downloads.mongodb.org/linux/mongodb-linux-x86_64-2.0.4.tgz

(如果不能正常下载,可以直接从浏览器下载下来,然后传到服务器上)

# tar -zxf mongodb-linux-x86_64-2.0.4.tgz-C /usr/local/

# mkdir -p /data/{db/geomaster/,log}

# touch /data/log/geomaster.log

# cd /usr/local/

# mv mongodb-linux-x86_64-2.0.4 mongodb

# /usr/local/mongodb/bin/mongod --fork--port 4000 --master --dbpath /data/db/geomaster/ --logpath /data/log/geomaster.log--logappend   启动数据库

# kill -2 `ps -ef |grep mongod |grep -vgrep |awk '{print $2}'`    关闭数据库

# wget http://downloads.mongodb.org/linux/mongodb-linux-x86_64-2.0.4.tgz

(如果不能正常下载,可以直接从浏览器下载下来,然后传到服务器上)

# tar -zxf mongodb-linux-x86_64-2.0.4.tgz-C /usr/local/

# mkdir -p /data/{db/geoslave/,log}

# touch /data/log/geosalve.log

# cd /usr/local/

# mv mongodb-linux-x86_64-2.0.4 mongodb

# /usr/local/mongodb/bin/mongod --fork--port 4000 --slave --source 10.240.216.151:4000 --dbpath /data/db/geoslave/--logpath /data/log/geosalve.log --logappend --nojournal      启动数据库

# kill -2 `ps -ef |grep mongod |grep -vgrep |awk '{print $2}'`    关闭数据库

 

# /usr/local/mongodb/bin/mongo127.0.0.1:4000

> db.runCommand ( {"isMaster": 1 } )

{ "ismaster" : true,"maxBsonObjectSize" : 16777216, "ok" : 1 }

# /usr/local/mongodb/bin/mongo127.0.0.1:4000

> db.runCommand ( {"isMaster": 1 } )

{

        "ismaster" : 0,

        "info" : "dead: data toostale halted replication",

        "maxBsonObjectSize" :16777216,

        "ok" : 1

}

> use local

switched to db local

> db.sources.find()

{ "_id" :ObjectId("55ebfa53ecf735439d6bafeb"), "host" :"10.240.216.151:4000", "source" : "main","syncedTo" : { "t" : 1441528400000, "i" : 1 } }

 

> use mydb

switched to db mydb

> db

mydb

>db.mydb.user.insert({"username":"zdh"});

> db.mydb.user.find();

{ "_id" :ObjectId("55ebfc2d92e237c3cb30ef9a"), "username" :"zdh" }

> show dbs;

local   6.2001953125GB

mydb    0.203125GB

> show dbs;

local   0.203125GB

mydb    0.203125GB

> use mydb

switched to db mydb

> db.mydb.user.find()

{"_id" : ObjectId("55ebfc2d92e237c3cb30ef9a"),"username" : "zdh" }


以上输出结果表示OK

 

对于上面这种master-slave结构有一个很重要的缺陷就是当主库挂掉后,从库要晋升为主库时,需要手动做大量的工作才行,具体步骤有:

1. 停掉其中一台slave,然后重新以master的身份启动(晋升10.240.216.152为主库)

# kill -2 `ps -ef |grep mongod |grep -vgrep |awk '{print $2}'`

# /usr/local/mongodb/bin/mongod --fork--port 4000 --master --dbpath /data/db/geoslave/ --logpath/data/log/geosalve.log –logappend

2. 如果不能找到研发修改代码指向新的主库IP,那么可以将原主库关机并将其的IP切到新主库服务器上(切IP场景,这种情况下slave端不需要重启mongodb进程,此时原主库将不能启动,这是一个问题)

# ifconfig bond0:1 10.240.216.151 netmask255.255.255.0 up

# arping -I bond0 -c 3 -s 10.240.216.15110.240.216.254

3. 将从库10.240.216.153指向新的主库,需要先停掉mongodb,然后source指向新主库启动(在不切IP的情况下操作)

# kill -2 `ps -ef |grep mongod |grep -vgrep |awk '{print $2}'`

# rm -f /data/db/geoslave/mongod.lock /data/db/geoslave/local.*

# /usr/local/mongodb/bin/mongod --fork--port 4000 --slave --source 10.240.216.152:4000 --dbpath /data/db/geoslave/--logpath /data/log/geosalve.log --logappend –nojournal

# /usr/local/mongodb/bin/mongo127.0.0.1:4000

> use local

switched to db local

> db.sources.find()

{ "_id" :ObjectId("55ec04dadfd5d65f2f7fe404"), "host" :"10.240.216.152:4000", "source" : "main","syncedTo" : { "t" : 1441531234000, "i" : 1 } }

 

# /usr/local/mongodb/bin/mongod--fork --port 4000 --dbpath /data/db/geomaster/ --logpath/data/log/geomaster.log --logappend --master

# /usr/local/mongodb/bin/mongod--fork --port 4000 --master --slave --dbpath /data/db/geoslave/ --logpath/data/log/geosalve.log –logappend

# /usr/local/mongodb/bin/mongo127.0.0.1:4000

> use local

switched to db local

> db.sources.find()

>db.sources.insert({"host":"10.240.216.151:4000"});

> db.sources.find()

{ "_id" :ObjectId("55ec120751fd14c6b2f23a18"), "host" :"10.240.216.151:4000" }

> db.sources.find()       隔30秒左右

{ "_id" :ObjectId("55ec120751fd14c6b2f23a18"), "host" :"10.240.216.151:4000", "source" : "main", "syncedTo": { "t" : 1441534596000, "i" : 1 } }

#/usr/local/mongodb/bin/mongod --fork --port 4000 --slave  --dbpath /data/db/geomaster/ --logpath/data/log/geosalve.log --logappend –nojournal

# /usr/local/mongodb/bin/mongo127.0.0.1:4000

> use local

switched to db local

> db.sources.find()

>db.sources.insert({"host":"10.240.216.151:4000"})

>db.sources.insert({"host":"10.240.216.152:4000"})

> db.sources.find()

{ "_id" :ObjectId("55ec127a1924006f4e8b2e97"), "host" : "10.240.216.151:4000","source" : "main", "syncedTo" : { "t" :1441534586000, "i" : 1 } }

{ "_id" :ObjectId("55ec127e1924006f4e8b2e98"), "host" :"10.240.216.152:4000", "source" : "main","syncedTo" : { "t" : 1441534593000, "i" : 1 } }

 

以上配置完成。就实现了当主库10.240.216.151挂掉后,备用主库10.240.216.152可以直接提供对外的服务,无需重启mongodb进程,并且从库无需作任何更改。

 

> show dbs

local   6.2001953125GB

mydb    0.203125GB

> use mydb

switched to db mydb

>db.mydb.user.insert({"username":"zhongguo","age":"26"})

> db.mydb.user.find()

{ "_id" :ObjectId("55ec12e4bab480b5485dd72e"), "username" :"zhongguo", "age" : "26" }

>db.mydb.user.insert({"username":"zhongguo2","age":"30"})

 

> use mydb

switched to db mydb

> db.mydb.user.find()

{ "_id" :ObjectId("55ec12e4bab480b5485dd72e"), "username" :"zhongguo", "age" : "26" }

> db.mydb.user.insert({"username":"finished","time":"2015-09-06"})

> db.mydb.user.find()

{ "_id" : ObjectId("55ec12e4bab480b5485dd72e"),"username" : "zhongguo", "age" : "26" }

{ "_id" : ObjectId("55ec133e51fd14c6b2f23a19"),"username" : "finished", "time" :"2015-09-06" }

> db.mydb.user.find()

{ "_id" :ObjectId("55ec12e4bab480b5485dd72e"), "username" :"zhongguo", "age" : "26" }

{ "_id" :ObjectId("55ec133e51fd14c6b2f23a19"), "username" :"finished", "time" : "2015-09-06" }

{ "_id" :ObjectId("55ec136abab480b5485dd72f"), "username" :"zhongguo2", "age" : "30" }

 

> use mydb

switched to db mydb

> db.mydb.user.find()

{ "_id" :ObjectId("55ec12e4bab480b5485dd72e"), "username" :"zhongguo", "age" : "26" }

> db.mydb.user.find()

{ "_id" : ObjectId("55ec12e4bab480b5485dd72e"),"username" : "zhongguo", "age" : "26" }

{ "_id" : ObjectId("55ec133e51fd14c6b2f23a19"),"username" : "finished", "time" :"2015-09-06" }

> db.mydb.user.find()

{ "_id" : ObjectId("55ec12e4bab480b5485dd72e"),"username" : "zhongguo", "age" : "26" }

{ "_id" :ObjectId("55ec133e51fd14c6b2f23a19"), "username" :"finished", "time" : "2015-09-06" }

{ "_id" :ObjectId("55ec136abab480b5485dd72f"), "username" :"zhongguo2", "age" : "30" }

上面的结果说明,备用从库是即可生效的,需要保证的是我们的写是确保从主库上写入的。


阅读原文内容投诉

免责声明:

① 本站未注明“稿件来源”的信息均来自网络整理。其文字、图片和音视频稿件的所属权归原作者所有。本站收集整理出于非商业性的教育和科研之目的,并不意味着本站赞同其观点或证实其内容的真实性。仅作为临时的测试数据,供内部测试之用。本站并未授权任何人以任何方式主动获取本站任何信息。

② 本站未注明“稿件来源”的临时测试数据将在测试完成后最终做删除处理。有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341

软考中级精品资料免费领

  • 历年真题答案解析
  • 备考技巧名师总结
  • 高频考点精准押题
  • 2024年上半年信息系统项目管理师第二批次真题及答案解析(完整版)

    难度     813人已做
    查看
  • 【考后总结】2024年5月26日信息系统项目管理师第2批次考情分析

    难度     354人已做
    查看
  • 【考后总结】2024年5月25日信息系统项目管理师第1批次考情分析

    难度     318人已做
    查看
  • 2024年上半年软考高项第一、二批次真题考点汇总(完整版)

    难度     435人已做
    查看
  • 2024年上半年系统架构设计师考试综合知识真题

    难度     224人已做
    查看

相关文章

发现更多好内容

猜你喜欢

AI推送时光机
位置:首页-资讯-数据库
咦!没有更多了?去看看其它编程学习网 内容吧
首页课程
资料下载
问答资讯