我们专注攀枝花网站设计 攀枝花网站制作 攀枝花网站建设
成都网站建设公司服务热线:400-028-6601

网站建设知识

十年网站开发经验 + 多家企业客户 + 靠谱的建站团队

量身定制 + 运营维护+专业推广+无忧售后,网站问题一站解决

mongodb主从复制安装文档-创新互联

  • 环境说明

    创新互联-专业网站定制、快速模板网站建设、高性价比习水网站开发、企业建站全套包干低至880元,成熟完善的模板库,直接使用。一站式习水网站制作公司更省心,省钱,快速模板网站建设找我们,业务覆盖习水地区。费用合理售后完善,10余年实体公司更值得信赖。

IPRole
10.240.216.151master
10.240.216.152slave(standby master)
10.240.216.153slave

  • mongodb下载与安装

  • 在10.240.216.151(主库)上

# wgethttp://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}'`   关闭数据库

  • 在10.240.216.152 和 10.240.216.153(备库)上

# wgethttp://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 } }

  • 针对以上操作复杂的过程,我们作如下优化

  • 在主库10.240.216.151上执行

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

  • 在从库(备主)10.240.216.152上执行

# /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 } }

  • 在从库10.240.216.153上执行

#/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进程,并且从库无需作任何更改。

  • 验证

  • 在主库10.240.216.151上

> 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"})

  • 在从库(备主)10.240.216.152上

> 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" }

  • 在从库10.240.216.153上

> 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" }

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

另外有需要云服务器可以了解下创新互联cdcxhl.cn,海内外云服务器15元起步,三天无理由+7*72小时售后在线,公司持有idc许可证,提供“云服务器、裸金属服务器、高防服务器、香港服务器、美国服务器、虚拟主机、免备案服务器”等云主机租用服务以及企业上云的综合解决方案,具有“安全稳定、简单易用、服务可用性高、性价比高”等特点与优势,专为企业上云打造定制,能够满足用户丰富、多元化的应用场景需求。


网页题目:mongodb主从复制安装文档-创新互联
文章起源:http://shouzuofang.com/article/pcjpp.html

其他资讯