mongodb 分片Replication

来源:互联网 发布:unity3d游戏开发实例 编辑:程序博客网 时间:2024/06/07 11:40

Mongodb 官方网站:https://www.mongodb.com/

摘抄官网对replication的解释:

A replica set in MongoDB is a group of mongod processesthat maintain the same data set. Replica sets provide redundancy andhigh availability, and are the basis for all production deployments.

Redundancy and Data Availability

Replication provides redundancy and increases data availability. Withmultiple copies of data on different database servers, replicationprovides a level of fault tolerance against the loss of a singledatabase server.

In some cases, replication can provide increased read capacity asclients can send read operations to different servers. Maintainingcopies of data in different data centers can increase data localityand availability for distributed applications. You can also maintainadditional copies for dedicated purposes, such as disaster recovery,reporting, or backup.


Replication有primary节点,secondary节点:

启动多个mongod服务,由于资源有限,在一台电脑上进行,也可以多台电脑:

1.此处启动三个mongod服务,分别对应27017(primary节点),27018(secondary节点),27019端口(secondary节点):

进入mongodb目录:

./bin/mongod --dbpath   /mongodb/data/17  --logpath  /mongodb/log/17.log  --fork --smallfiles --port 27017 --replSet rs1

./bin/mongod --dbpath   /mongodb/data/18  --logpath  /mongodb/log/18.log  --fork --smallfiles --port 27018--replSet rs1

./bin/mongod --dbpath   /mongodb/data/19  --logpath  /mongodb/log/19.log  --fork --smallfiles --port 27019--replSet rs1

(--replSet 设置复制集的名称)

2.连接17服务:./bin/mongo --port 27017


3.admin数据库:use admin

4.设置复制集:

 var rsConf={_id:"rs1",members:[{_id:0,host:"ip:27017"},{_id:1,host:"ip:27018"},{_id:2,host:"ip:27019"}]} 回车

  rs.initiate(rsConf)

5.新建文档:


6.连接18服务:./bin/mongo --port 27018

7.use test

8.对于replSet中的Secondary节点默认是不可读的,直接查询,会报错如下:


设置:db.getMongo().setSlaveOk();在secondary节点18上查询成功


0 0