MongoDB主从复制(Master-Slave Replication)简单实现
MongoDB主从复制(Master-Slave Replication)其结构类似MySQL的主从复制。
os:CentOS 6.5
mongodb:3.2.10
〇 搭建
master上:
- $ mkdir -p /data/testdb
- $ mongod --dbpath=/data/testdb/ --logpath=/data/testdb/master.log --fork --master
slave上:
- $ mkdir -p /data/testdb
- $ mongod --dbpath=/data/testdb/ --logpath=/data/testdb/slave.log --fork --slave --source 192.168.0.1:27017
然后查看一下slave的log
- $ tail -f /data/testdb/slave.log
如看到如下输出,则表示已经开始同步
- 2016-11-09T16:19:46.695+0800 I REPL [replslave] syncing from host:192.168.0.1:27017
〇 测试
master上:
- > use test;
- switched to db test
- > db.test_tb.insert({"master":"123456"})
- WriteResult({ "nInserted" : 1 })
- > db.test_tb.find();
- { "_id" : ObjectId("5822d3507fedcba43d3b9278"), "master" : "123456" }
slave上:
- > use test;
- switched to db test
- > db.test_tb.find();
- Error: error: { "ok" : 0, "errmsg" : "not master and slaveOk=false", "code" : 13435 }
出现如上错误是因为默认slave是不可读写的:
可以执行
- > db.getMongo().setSlaveOk()
或者输入上命令同义词:
- > rs.slaveOk()
便可以了
- > db.test_tb.find();
- { "_id" : ObjectId("5822d3507fedcba43d3b9278"), "master" : "123456" }
在mongodb 3.2 文档里明确写道:
- WARNING
- Deprecated since version 3.2: MongoDB 3.2 deprecates the use of master-slave replication for components of sharded clusters.
- IMPORTANT
- Replica sets replace master-slave replication for most use cases. If possible, use replica sets rather than master-slave replication for all new production deployments. This documentation remains to support legacy deployments and for archival purposes only.
如果要用m-s结构做分片集群,可以取而代之的是使用更加流弊的Replica Set,然而Replica Set可以理解为“Master-Slave”带有自动failover等功能的高级解决方案。
〇 参考文档
MONGODB MANUAL 3.2 > Replication > Master Slave Replication
相关推荐
lbyd0 2020-11-17
BigYellow 2020-11-16
sushuanglei 2020-11-12
我心似明月 2020-11-09
zhushenghan 2020-11-09
sunnnyduan 2020-10-16
不要皱眉 2020-10-14
xiaohai 2020-09-29
songxiugongwang 2020-09-22
萌亖 2020-09-17
LuckyLXG 2020-09-08
sdmzhu 2020-09-01
mkhhxxttxs 2020-09-16
xiaohai 2020-09-16
newcome 2020-09-09
jaylong 2020-08-19
大秦铁骑 2020-08-19
thatway 2020-08-19