当前位置: 首页 > mongodb, 分布式系统, 缓存系统 > 正文

MongoDB Master/Slave 配置

1 星2 星3 星4 星5 星 (暂无评分)
Loading ... Loading ...
baidu_share

最近有项目用到了MongoDB,于是测试在本地虚拟机搭建一主两从,从库可做备份,也可防止主库挂掉后变从为主。

启动主库

1
mongod -master -dbpath=/data/mongodb/master/ -port=27017 &

启动两个从库的(5秒钟同步一次)

1
2
mongod -slave -source=127.0.0.1:27017 -dbpath=/data/mongodb/slave1/ -port=27018 -slavedelay 5 &
mongod -slave -source=127.0.0.1:27017 -dbpath=/data/mongodb/slave2/ -port=27019 -slavedelay 5 &

查看进程

1
2
3
4
5
[root@mms ~]# ps -ef|grep mongo
root 7712 7534 0 19:26 pts/4 00:00:00 mongod -master -dbpath=/data/mongodb/master/ -port=27017
root 7721 7506 0 19:26 pts/3 00:00:00 mongod -slave -source=127.0.0.1:27017 -dbpath=/data/mongodb/slave1/ -port=27018 -slavedelay 5
root 7733 7478 0 19:26 pts/2 00:00:00 mongod -slave -source=127.0.0.1:27017 -dbpath=/data/mongodb/slave2/ -port=27019 -slavedelay 5
root 7760 7416 0 19:28 pts/1 00:00:00 grep mongo

接下来就可以在Master库里插入数据,查看Slave库的同步情况
往主库插入一条记录

1
2
3
4
5
6
7
8
9
10
[root@mms ~]# mongo 127.0.0.1:27017
MongoDB shell version: 2.0.3
connecting to: 127.0.0.1:27017/test
> show dbs
local 0.125GB
mongodbtest (empty)
questiondb 0.0625GB
> use mongodbtest
switched to db mongodbtest
> db.mongotest.save({'title':'this is test title','item':{'user':'hello','city':'beijing'}})

从库27018的同步情况

1
2
3
4
5
6
7
8
9
10
11
12
[root@mms ~]# mongo 127.0.0.1:27018
MongoDB shell version: 2.0.3
connecting to: 127.0.0.1:27018/test
> show dbs
local 0.0625GB
mongodbtest 0.0625GB
questiondb 0.0625GB
test (empty)
> use mongodbtest
switched to db mongodbtest
> db.mongotest.find()
{ "_id" : ObjectId("4f7d08cabf1eaec59250a52b"), "title" : "this is test title", "item" : { "user" : "hello", "city" : "beijing" } }

从库27019的同步情况

1
2
3
4
5
6
7
8
9
10
11
[root@mms ~]# mongo 127.0.0.1:27019
MongoDB shell version: 2.0.3
connecting to: 127.0.0.1:27019/test
> show dbs
local 0.0625GB
mongodbtest 0.0625GB
questiondb 0.0625GB
> use mongodbtest
switched to db mongodbtest
> db.mongotest.find()
{ "_id" : ObjectId("4f7d08cabf1eaec59250a52b"), "title" : "this is test title", "item" : { "user" : "hello", "city" : "beijing" } }

本文固定链接: http://www.chepoo.com/mongodb-master-slave-config.html | IT技术精华网

MongoDB Master/Slave 配置:等您坐沙发呢!

发表评论