Mongodb的replication主要有两种:主从和副本集(replica set)。主从的原理和mysql类似,主节点记录在其上的所有操作oplog,从节点定期轮询主节点获取这些操作,然后对自己的数据副本执行这些操作,从而保证从节点的数据与主节点一致。
现在mongodb官方建议用副本集替代主从复制,但是这不妨碍我们从主从复制入门,了解mongdob的replication。
什么是oplog:
MongoDB 的Replication是通过一个日志来存储写操作的,这个日志就叫做oplog。 在默认情况下,oplog分配的是5%的空闲磁盘空间。通常而言,这是一种合理的设置。可以通过mongod --oplogSize来改变oplog的日志大小。 oplog是capped collection,因为oplog的特点(不能太多把磁盘填满了,固定大小)需要,MongoDB才发明了capped collection(the oplog is actually the reason capped collections were invented)。
oplog的位置 oplog在local库: master/slave 架构下 local.oplog.$main。
主数据库配置(master.conf )
# 数据库文件存储位置
dbpath= /data/db/master
# log文件存储位置
logpath= /data/log/mongodb/master/mongodb.log
# 使用追加的方式写日志
logappend= true# 是否以守护进程方式运行
fork= true# 端口号
port= 27017# 是否启用认证
auth= true# 集群的私钥的完整路径,只对于Replica Set 架构有效(noauth = true时不用配置此项)
keyFile= /usr/local/mongodb/mongodb-keyfile
# diaglog选项0=off 1=W 2=R 3=both 7=W+some reads
#diaglog= 0# 设置主从服务器
master= true# 设置oplog的大小(MB)
oplogSize=2048
从数据库配置(slave1.conf)
# 数据库文件存储位置
dbpath= /data/db/slave1
# log文件存储位置
logpath= /data/log/mongodb/slave1/mongodb.log
# 使用追加的方式写日志
logappend= true# 是否以守护进程方式运行
fork= true# 端口号
port= 27027# 是否启用认证
auth= true# 集群的私钥的完整路径,只对于Replica Set 架构有效(noauth = true时不用配置此项)
keyFile = /usr/local/mongodb/mongodb-keyfile
# diaglog选项0=off 1=W 2=R 3=both 7=W+some reads
#diaglog= 0# 设置主从服务器
slave= true# 用于从节点,指定从节点的复制来源(主节点的IP+端口),格式为:<:port>source= 127.0.0.1:27017
注意:
如果使用认证的方式启动服务(auth = true),那么要在主从服务器配置私钥。
//在一台服务器上生成私钥
openssl rand -base64 745 > mongodb-keyfile//修改私钥的权限为可读写
chmod 600 mongodb-keyfile//将私钥拷贝到其余的服务器上
scp mongodb-keyfile root@xxx.xxx.xxx.xxx:/usr/local/mongodb/
然后配置私钥路径,启动服务。
启动数据库:
/usr/local/mongodb/bin/mongod --config /usr/local/mongodb/master.conf
/usr/local/mongodb/bin/mongod --config /usr/local/mongodb/slave1.conf
当启动slave后,slave端会自动进行初始化resync同步,可以在日志查看到同步信息:
2018-01-03T11:39:46.396+0800 I NETWORK [thread1] waiting for connections on port 27027
2018-01-03T11:39:47.415+0800 I REPL [replslave] syncing from host:127.0.0.1:27017
2018-01-03T11:39:47.458+0800I REPL [replslave] resync: dropping database admin2018-01-03T11:39:47.458+0800I REPL [replslave] resync: cloning database admin to get an initial copy2018-01-03T11:39:47.481+0800 I COMMAND [replslave] setting featureCompatibilityVersion to 3.4
2018-01-03T11:39:47.482+0800 I STORAGE [replslave] copying indexes for: { name: "system.version", type: "collection", options: {}, info: { readOnly: false }, idIndex: { v: 1, key: { _id: 1 }, name: "_id_", ns: "admin.system.version"} }2018-01-03T11:39:47.500+0800 I INDEX [replslave] build index on: admin.system.version properties: { v: 2, key: { version: 1 }, name: "incompatible_with_version_32", ns: "admin.system.version"}2018-01-03T11:39:47.500+0800 I INDEX [replslave] building index using bulk method; build may temporarily use up to 500megabytes of RAM2018-01-03T11:39:47.504+0800 I INDEX [replslave] build index done. scanned 1 total records. 0secs2018-01-03T11:39:47.504+0800 I REPL [replslave] resync: done with initial clone fordb: admin2018-01-03T11:39:47.505+0800 I REPL [replslave] syncing from host:127.0.0.1:27017
2018-01-03T11:39:48.506+0800 I REPL [replslave] sleep 1sec before next pass2018-01-03T11:39:49.506+0800 I REPL [replslave] syncing from host:127.0.0.1:27017
2018-01-03T11:39:50.507+0800 I REPL [replslave] syncing from host:127.0.0.1:27017
2018-01-03T11:39:51.507+0800 I REPL [replslave] syncing from host:127.0.0.1:27017
2018-01-03T11:39:52.508+0800 I REPL [replslave] syncing from host:127.0.0.1:27017
2018-01-03T11:39:53.509+0800 I REPL [replslave] syncing from host:127.0.0.1:27017
2018-01-03T11:39:54.509+0800 I REPL [replslave] syncing from host:127.0.0.1:27017
2018-01-03T11:39:55.510+0800 I REPL [replslave] syncing from host:127.0.0.1:27017
2018-01-03T11:39:56.510+0800 I REPL [replslave] syncing from host:127.0.0.1:27017
2018-01-03T11:39:58.493+0800 I REPL [replslave] syncing from host:127.0.0.1:27017
2018-01-03T11:39:59.494+0800 I REPL [replslave] syncing from host:127.0.0.1:27017
2018-01-03T11:40:00.495+0800 I REPL [replslave] syncing from host:127.0.0.1:27017
2018-01-03T11:40:01.495+0800 I REPL [replslave] syncing from host:127.0.0.1:27017
2018-01-03T11:40:02.496+0800 I REPL [replslave] syncing from host:127.0.0.1:27017
resync:
当slave端落后过多的时候需要进行resync。resync会在master上加一个全局的写锁,会阻塞其它的写操作,直到resync结束。可以使用数据库命令进行resync:
use admin
db.runCommand({"resync":1})