您的位置:首页 > 数据库 > Mongodb

mongodb主从配置

2015-08-24 15:05 489 查看
环境是在一台服务器,多个实例(多实例配置/article/7857524.html)间配置主从。

主库端口:27017,从库端口:27018

配置(就是在启动的时候添加几个参数,没有什么特别的配置)

#主库:
mongod --fork --master --oplogSize=2048 -f /usr/local/mongodb/mongodb.cnf
#从库
mongod --fork --slave --source 192.168.7.221:27017 -f /home/mongo/27018/mongo27018.cnf

查看状态

#主
> db.runCommand({"isMaster":1})db.runCommand({"isMaster":1})
{
"ismaster" : true,
"maxBsonObjectSize" : 16777216,
"maxMessageSizeBytes" : 48000000,
"maxWriteBatchSize" : 1000,
"localTime" : ISODate("2015-08-24T03:25:29.664Z"),
"maxWireVersion" : 3,
"minWireVersion" : 0,
"ok" : 1
}
#从
> db.runCommand({"isMaster":1}) db.runCommand({"isMaster":1})
{
"ismaster" : false,
"maxBsonObjectSize" : 16777216,
"maxMessageSizeBytes" : 48000000,
"maxWriteBatchSize" : 1000,
"localTime" : ISODate("2015-08-24T06:36:23.158Z"),
"maxWireVersion" : 3,
"minWireVersion" : 0,
"ok" : 1
}
从库查询主库地址
> use local
switched to db local
> db.sources.find()
{ "_id" : ObjectId("55da87e443a5b11af6d5f968"), "host" : "192.168.7.221:27017", "source" : "main", "syncedTo" : Timestamp(1440398765, 1) }
测试
#主
> use cric
switched to db cric
> db.c1.insert({"name":"test","age":30})
WriteResult({ "nInserted" : 1 })
> db.c1.find()
{ "_id" : ObjectId("55dac1063e9805d2c4d8ae37"), "name" : "test", "age" : 30 }
#从
> use cric
switched to db cric
> db.c1.find()
{ "_id" : ObjectId("55dac1063e9805d2c4d8ae37"), "name" : "test", "age" : 30 }
测试过程可以查看从库同步过程
tail -f /home/mongo/27018/log/mong
2015-08-24T15:00:22.553+0800 I REPL     [replslave] resync: cloning database cric to get an initial copy
2015-08-24T15:00:22.562+0800 I INDEX    [replslave] allocating new ns file /home/mongo/27018/data/cric.ns, filling with zeroes...
2015-08-24T15:00:22.966+0800 I STORAGE  [FileAllocator] allocating new datafile /home/mongo/27018/data/cric.0, filling with zeroes...
2015-08-24T15:00:22.968+0800 I STORAGE  [FileAllocator] done allocating datafile /home/mongo/27018/data/cric.0, size: 64MB,  took 0.001 secs
2015-08-24T15:00:22.986+0800 I INDEX    [replslave] build index on: cric.c1 properties: { v: 1, key: { _id: 1 }, name: "_id_", ns: "cric.c1" }
2015-08-24T15:00:22.986+0800 I INDEX    [replslave]      building index using bulk method
2015-08-24T15:00:22.986+0800 I INDEX    [replslave] build index done.  scanned 1 total records. 0 secs
2015-08-24T15:00:22.986+0800 I STORAGE  [replslave] copying indexes for: { name: "c1", options: {} }
2015-08-24T15:00:22.986+0800 I REPL     [replslave] resync: done with initial clone for db: cric
2015-08-24T15:00:30.177+0800 I REPL     [replslave] repl: syncing from host:192.168.7.221:27017
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: