1:master-slave replication
主从复制:一个服务启动的时候加上--master参数,另一个服务启动时加上--slave和--source参数就可以实现同步了。
一个master服务可以有一个或者多个slave,每一个salve都知道master的地址,如图:
举例:
启动master
$ mkdir -p ~/dbs/master
$ ./mongod --dbpath ~/dbs/master --port 10000 --master
建立相关目录,启动的时候加上--master即可
启动slave
$ mkdir -p ~/dbs/slave
$ ./mongod --dbpath ~/dbs/slave --port 10001 --slave --source localhost:10000
建立目录,启动这里的localhost就是上面master的地址,不管是一个slave还是多个,都是从master进行复制
Adding and Removing Sources:
$ ./mongod --slave --dbpath ~/dbs/slave --port 27018 --没有指定source
启动之后如果要加入一个slave,localhost:27017为master
> use local
> db.sources.insert({"host" : "localhost:27017"})
2:replica sets 复制集
这个是1.6开发的新功能,与之前的主从复制比起来,功能更加强大,多了故障转移和自动修复节点,每个DB的数据完全一致。
建立相关目录:
数据目录:mkdir -p /mongodb1.8/data1/
mkdir -p /mongodb1.8/data2/
mkdir -p /mongodb1.8/data3/
日志文件:touch /mongodb1.8/RS.log /mongodb1.8/RS1.log /mongodb1.8/RS2.log
创建主从KEY文件:mkdir -p /mongodb1.8/key
echo "this is rs1 super secret key" > /mongodb1.8/key/r1
echo "this is rs1 super secret key" > /mongodb1.8/key/r2
echo "this is rs1 super secret key" > /mongodb1.8/key/r3
这里KEY的权限400
chmod 400 /mongodb1.8/key/r1 /mongodb1.8/key/r2 /mongodb1.8/key/r3
启动一个server,记得/etc/hosts文件里面包含localhost记录,否则无法解析
mongod --replSet rs1/localhost:20001 --keyFile /mongodb1.8/key/r1 --fork --port 20001 --dbpath /mongodb1.8/data1/ --logpath=/mongodb1.8/RS.log
mongod --replSet rs1/localhost:20002 --keyFile /mongodb1.8/key/r2 --fork --port 20002 --dbpath /mongodb1.8/data2/ --logpath=/mongodb1.8/RS1.log
<wbr style="line-height:25px"></wbr>
mongod --replSet rs1/localhost:20003 --keyFile /mongodb1.8/key/r3 --fork --port 20003 --dbpath /mongodb1.8/data3/ --logpath=/mongodb1.8/RS2.log
mongo localhost:20001/admin
config_rs1 = {_id: 'rs1', members: [
... {_id: 0, host: 'localhost:20001', priority:1}, --成员IP 及端口,priority=1 指PRIMARY
... {_id: 1, host: 'localhost:20002'},
... {_id: 2, host: 'localhost:20003'}]
... }
初始化配置:
rs.initiate(config_rs1);
或者db.runCommand({"replSetInitiate" : {
... "_id" : "rs1",
... "members" : [
... {
... "_id" : 0,
... "host" : "localhost:20001"
... },
... {
... "_id" : 1,
... "host" : "localhost:20002"
... },
... {
... "_id" : 2,
... "host" : "localhost:20003"
... }
... ]}})
rs1:PRIMARY> rs.help()
rs.status() { replSetGetStatus : 1 } checks repl set status
rs.initiate() { replSetInitiate : null } initiates set with default settings
rs.initiate(cfg) { replSetInitiate : cfg } initiates set with configuration cfg
rs.conf() get the current configuration object from local.system.replset
rs.reconfig(cfg) updates the configuration of a running replica set with cfg (disconnects)
rs.add(hostportstr) add a new member to the set with default attributes (disconnects)
rs.add(membercfgobj) add a new member to the set with extra attributes (disconnects)
rs.addArb(hostportstr) add a new member which is arbiterOnly:true (disconnects)
rs.stepDown([secs]) step down as primary (momentarily) (disconnects)
rs.freeze(secs) make a node ineligible to become primary for the time specified
rs.remove(hostportstr) remove a host from the replica set (disconnects)
rs.slaveOk() shorthand for db.getMongo().setSlaveOk()
db.isMaster() check who is primary
或者使用命令:
db.runCommand({})
* { isMaster : 1 }
* { replSetGetStatus : 1 }
* { replSetInitiate : <config> }
* { replSetReconfig: <config> }
* { replSetStepDown : <seconds> }
* { replSetFreeze : <seconds> }
查看状态:
rs.status()
rs.isMaster()
主从日志oplpg
rs就是通过oplog的记录写操作,他是一个固定长度的copped collection,存放于local数据库下,oplog的参数是可以通过--oplogSize来调整的,可以查看db.oplog.rs.find(),看到操作记录。
db.printReplicationInfo()查看元数据信息
db.printSlaveReplicationInfo()查看slave的同步状态
在local库下db.system.replset.find(),可以查询到主从库的信息=rs.conf()
默认从库是不可读的,如果需要来分担主库压力,可以打开读,举例:
[mongo@172_16_3_216 ~]$ mongo 127.0.0.1:20002
MongoDB shell version: 1.8.4
connecting to: 127.0.0.1:20002/test
myrs:SECONDARY> show collections
Mon Nov 14 15:20:30 uncaught exception: error: { "$err" : "not master and slaveok=false", "code" : 13435 }
myrs:SECONDARY> db.get
db.getCollection( db.getLastErrorCmd( db.getName( db.getProfilingStatus( db.getSisterDB(
db.getCollectionNames( db.getLastErrorObj( db.getPrevError( db.getReplicationInfo(
db.getLastError( db.getMongo( db.getProfilingLevel( db.getSiblingDB(
myrs:SECONDARY> db.getMongo().setSlaveOk()
not master and slaveok=false
myrs:SECONDARY> show collections
hank
system.indexes
myrs:SECONDARY> db.hank.find()
{ "_id" : ObjectId("4ec0bdf9a9888d970cdc6197"), "name" : "zhang" }
{ "_id" : ObjectId("4ec0bdfba9888d970cdc6198"), "name" : "zhang" }
{ "_id" : ObjectId("4ec0bdfba9888d970cdc6199"), "name" : "zhang" }
{ "_id" : ObjectId("4ec0bdfca9888d970cdc619a"), "name" : "zhang" }
{ "_id" : ObjectId("4ec0bdfca9888d970cdc619b"), "name" : "zhang" }
{ "_id" : ObjectId("4ec0bdfca9888d970cdc619c"), "name" : "zhang" }
{ "_id" : ObjectId("4ec0be05a9888d970cdc619d"), "name" : "zhang", "age" : 80 }
{ "_id" : ObjectId("4ec0be06a9888d970cdc619e"), "name" : "zhang", "age" : 80 }
{ "_id" : ObjectId("4ec0be06a9888d970cdc619f"), "name" : "zhang", "age" : 80 }
{ "_id" : ObjectId("4ec0be07a9888d970cdc61a0"), "name" : "zhang", "age" : 80 }
{ "_id" : ObjectId("4ec0be07a9888d970cdc61a1"), "name" : "zhang", "age" : 80 }
这样就可以读了
分享到:
相关推荐
mongoDB Replication 复制模式官网指导文档
MongoDB 安装包 ,包含了单服务器的副本集(Replication)配置(单服务器:Windows)
mongdb副本集结构的配置,公司线上服务器使用的配置方案,分享给大家参考参考。
MongoDB-replication-guide.pdf
1. 环境说明 主机名称 IP地址 OS版本 node1 172.16.100.52 CentOS7(kernel: 3.10.0-514.el7.x86_64) node2 172.16.100.53 CentOS7(kernel: 3.10.0-514.el7.x86_64) ...MongoDB Replication 2. 在node1,node2
mongo replication guide mongo replication guide mongo replication guide
Addressing the limitations of SQL schema-based databases, MongoDB pioneered a shift of focus for DevOps and offered sharding and replication maintainable by DevOps teams. The book is based on MongoDB...
视频目录: 01-mongodb文档型数据库特点介绍 01-NoSQL简介 02-mongodb安装过程 02-mongodb操作1 03-mongodb操作2 ...10-replication复制集 10-主从复制 11-shard分片 11-分片 12-使用java操作mongodb
目录: 1.MongoDB 成为最好 NoSQL 数据库...8.分片(sharding)和复制(replication)是怎样工作的? 9.我怎么查看 Mongo 正在使用的链接? 10.MongoDB 在 A:{B,C}上建立索引,查询 A:{B,C}和 A:{C,B}都会使用索引吗? ......
The Definitive Guide to MongoDB: A complete guide to dealing with Big Data using MongoDB The Definitive Guide to MongoDB, Third Edition, is updated for ...Chapter 11: Replication Chapter 12: Sharding
Set up master-slave and automatic failover replication in MongoDB Use sharding to scale MongoDB horizontally, and learn how it impacts applications Get example applications written in Java, PHP, ...
MongoDB 3.4 MongoDB 3.6 MongoDB 4.0 MongoDB的4.2 MongoDB 4.4 Ubuntu 16.04 :no_entry: :check_mark_button: :check_mark_button: :check_mark_button: :cross_mark: Ubuntu 18.04 :no_entry: :check_mark_button...
MongoDB, a cross-platform NoSQL database, is the fastest-growing new database in the world. MongoDB provides a rich document-oriented structure with...Master MongoDB administration, including replication
mongodb的基本管理手册,开发人员及数据库管理人员可参考。英文文档,可以很容易看明白。 第一章 介绍mongodo 第二章 安装mongodb 第三章 The Data Model ...第十一章 Replication 第十二章 Sharding
《MongoDB权威指南》第9章 复制 试译 MongoDB是一种NoSQL数据库,具备MySQL单表的大部分功能。web2.0的应用往往具有高写负载,单表数据量不断增长等特点。MongoDB是专门为解决这些问题而设计的。
Its schema-free design encourages rapid application development, and built-in replication and auto-sharding architecture allow for massive parallel distribution. Production deployments at SourceForge...
MongoDB in Action introduces you to MongoDB and the document-oriented database model. This perfectly paced book provides both the big picture you'll need as a developer and enough low-level detail to ...
MongoDB副本集的配置示例,过程简明易懂
5.2Availability through Replication 5.3 Sharding Chapter 6: NewsWatcher App Development 6.1 Create the Database and Collection 6.2 Data Model Document Design 6.3Trving Out Some Queries 5.4Indexing ...