MongoDB 集羣(二)

MongoDB 主從

主mongodb.config配置mongodb

  • dbpath=/usr/data/mongodb/master/master/data/db
  • logpath=/usr/data/mongodb/master/master/log/mongodb.log
  • logappend=true
  • fork=true
  • bind_ip=192.168.1.1
  • port=27001
  • master=true
  • source=192.168.1.1:27002

從mongodb.config配置服務器

  • dbpath=/usr/data/mongodb/master/slave/data/db
  • logpath=/usr/data/mongodb/master/slave/log/mongodb.log
  • logappend=true
  • fork=true
  • bind_ip=192.168.1.1
  • port=27002
  • slave=true
  • source=192.168.1.1:27001

mongoDB官方已經不建議使用主從模式了,替代方案是採用副本集的模式app

mongo --host 192.168.1.1 -port 27001spa

  • > use test
  • switched to db test
  • > db.createCollection("my")
  • { "ok" : 1 }
  • > for(var i=0;i<100;i++)db.my.insert({name:"zhangsan"+i})
  • WriteResult({ "nInserted" : 1 })
  • > show collections
  • my
  • system.indexes
  • > db.my.find();
  • { "_id" : ObjectId("5833003a0c9c1401998c51ea"), "name" : "zhangsan0" }
  • { "_id" : ObjectId("5833003a0c9c1401998c51eb"), "name" : "zhangsan1" }
  • { "_id" : ObjectId("5833003a0c9c1401998c51ec"), "name" : "zhangsan2" }
  • { "_id" : ObjectId("5833003a0c9c1401998c51ed"), "name" : "zhangsan3" }
  • mongo --host 192.168.1.1 -port 27002
  • > show dbs
  • error: { "$err" : "not master and slaveOk=false", "code" : 13435 }

須要注意的是,從服務器默認狀況下是不支持讀寫的,可是會把數據同步到從服務器,不支持客戶端讀寫。須要客戶端鏈接從服務器時用命令支持讀:rs.slaveOk()code

  • > rs.slaveOk()
  • > show dbs
  • local  0.078GB
  • test   0.078GB
  • > use test
  • switched to db test
  • > show collections
  • my
  • system.indexes
  • > db.my.find()
  • { "_id" : ObjectId("5833003a0c9c1401998c51ea"), "name" : "zhangsan0" }
  • { "_id" : ObjectId("5833003a0c9c1401998c51eb"), "name" : "zhangsan1" }
  • { "_id" : ObjectId("5833003a0c9c1401998c51ec"), "name" : "zhangsan2" }
相關文章
相關標籤/搜索