MongoDB一般情况下都可以支持主主复制,但是在大部分情况下官方不推荐使用
运行的master - master的准备工作是:
新建存放数据库文件的路径
$mkdir mongodata/mm_28050 mongodata/mm_28051
运行mongodb数据库,一个端口为:28050,一个为:28051
mongodb/bin/mongod --port 28050 --dbpath ~/mongodata/mm_28050 --master --slave --source localhost:28051 > /dev/null & mongodb/bin/mongod --port 28051 --dbpath ~mongodata/mm_28051 --master --slave --source localhost:28050 > /dev/null &
可以通过ps -ef|grep mongod 或 netstat -an -t来检查是否运行功能
测试master - master模式:
$ mongodb/bin/mongo --port 28050
MongoDB shell version: 1.2.4-
url: test
connecting to: 127.0.0.1:28050/test
type "help" for help
> show dbs
admin
local
> db
test
> db.user.insert({_id:1,username:"Falcon.C",age:25,sex:"M"});
> db.user.find();
{ "_id" : 1, "username" : "Falcon.C", "age" : 25, "sex" : "M" }
> db.user.find(); //在28051端口插入数据后,再来查询,看数据是否同步
{ "_id" : 1, "username" : "Falcon.C", "age" : 25, "sex" : "M" }
{ "_id" : 2, "username" : "NetOne", "age" : 24, "sex" : "F" }
>
MongoDB shell version: 1.2.4-
url: test
connecting to: 127.0.0.1:28050/test
type "help" for help
> show dbs
admin
local
> db
test
> db.user.insert({_id:1,username:"Falcon.C",age:25,sex:"M"});
> db.user.find();
{ "_id" : 1, "username" : "Falcon.C", "age" : 25, "sex" : "M" }
> db.user.find(); //在28051端口插入数据后,再来查询,看数据是否同步
{ "_id" : 1, "username" : "Falcon.C", "age" : 25, "sex" : "M" }
{ "_id" : 2, "username" : "NetOne", "age" : 24, "sex" : "F" }
>
$ mongodb/bin/mongo --port 28051
MongoDB shell version: 1.2.4-
url: test
connecting to: 127.0.0.1:28051/test
type "help" for help
> db
test
> show collections 端口28050已经新建了一个user表并插入了一条数据,这里多出2表
system.indexes
user
> db.user.find(); //查询表user发现数据已经同步
{ "_id" : 1, "username" : "Falcon.C", "age" : 25, "sex" : "M" }
> db.user.insert({_id:2,username:"NetOne",age:24,sex:"F"});在此插入数据看数据是否双向同步
> db.user.find();
{ "_id" : 1, "username" : "Falcon.C", "age" : 25, "sex" : "M" }
{ "_id" : 2, "username" : "NetOne", "age" : 24, "sex" : "F" }
>
MongoDB shell version: 1.2.4-
url: test
connecting to: 127.0.0.1:28051/test
type "help" for help
> db
test
> show collections 端口28050已经新建了一个user表并插入了一条数据,这里多出2表
system.indexes
user
> db.user.find(); //查询表user发现数据已经同步
{ "_id" : 1, "username" : "Falcon.C", "age" : 25, "sex" : "M" }
> db.user.insert({_id:2,username:"NetOne",age:24,sex:"F"});在此插入数据看数据是否双向同步
> db.user.find();
{ "_id" : 1, "username" : "Falcon.C", "age" : 25, "sex" : "M" }
{ "_id" : 2, "username" : "NetOne", "age" : 24, "sex" : "F" }
>
本文转自 不得闲 博客园博客,原文链接:http://www.cnblogs.com/DxSoft/archive/2010/10/21/1857359.html ,如需转载请自行联系原作者