mongodb主从复制安装文档

发布时间:2020-07-19 06:51:17 作者:linuxblind
来源:网络 阅读:820

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

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


推荐阅读:
  1. MongoDB 主从复制集搭建
  2. MongoDB复制集 (即主从复制)

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

mongodb 主从 %d mong

上一篇:MVC3----筛选数据(BeginForm:输出form表单)

下一篇:dlib调用

相关阅读

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

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