mongo主从 副本集
一,mongo 主从
mongo的主从配置
1,主从同步原理,从库通过去读取主库的oplog来实现数据的同步
弊端: 主从不能自动切换(副本集可以)
环境:(在一个服务器上启用多个mongo进程,监听在不同端口,也可以实现)
master :192.168.78.130
slave : 192.168.78.131
步骤一:
在master 130上,启用mongod :
[root@rac1 bin]# ./mongod --dbpath=/root/data --logpath=/root/data/mongo.log --logappend --fork --port=10001 --master
forked process: 6488
all output going to: /root/data/mongo.log
child process started successfully, parent exiting
这里主要是一个--master参数,告诉mongo 自己此时是master
步骤二:
在slave 131 上,启用mongod
[root@rac2 bin]# ./mongod --dbpath=/root/data --logpath=/root/data/mongo.log --logappend --fork --port=10002 --slave --source 192.168.78.130:10001
forked process: 5440
all output going to: /root/data/mongo.log
child process started successfully, parent exiting
这里--slave指定本机为slave 通过--source 指向master服务器 130:10001
搭建成功
测试:
在master 130上插入数据
登陆 --host指定登陆服务器ip地址,--port指定端口
[root@rac1 bin]# ./mongo --host 192.168.78.130 --port 10001
MongoDB shell version: 2.2.2
connecting to: 192.168.78.130:10001/test
Welcome to the MongoDB shell.
For interactive help, type "help".
For more comprehensive documentation, see
http://docs.mongodb.org/
Questions? Try the support group
http://groups.google.com/group/mongodb-user
> show dbs
local 0.078125GB
> use pincer
switched to db pincer
> db.pincer.insert({"test":111})
> show collections
pincer
system.indexes
> db.pincer.find()
{ "_id" : ObjectId("52100534f11fdb125391724e"), "test" : 111 }
master上已经在pincer库中的pincer集合中插入了test:111
到131 slave上去查看
> show dbs
local 0.078125GB
pincer 0.0625GB
> use pincer
switched to db pincer
> show collections
pincer
system.indexes
> db.pincer.find()
{ "_id" : ObjectId("52100534f11fdb125391724e"), "test" : 111 }
可以看到数据已经同步了
在master(或slave)上查看
> db.printReplicationInfo()
configured oplog size: 47.6837158203125MB
log length start to end: 97secs (0.03hrs)
oplog first event time: Thu Aug 08 2013 09:06:22 GMT+0800 (CST)
oplog last event time: Thu Aug 08 2013 09:07:59 GMT+0800 (CST)
now: Thu Aug 08 2013 09:27:59 GMT+0800 (CST)
这里的oplog 47M 就是指oplog的日志大小(如果从去同步主时,因为之前主的数据太大了,那么要设置大点oplog,--oplogSize)
查看主从信息
在master上
> use local
switched to db local
> db.slaves.find()
{ "_id" : ObjectId("5202f30721f1c7b358a9c53f"), "host" : "192.168.78.131", "ns" : "local.oplog.$main", "syncedTo" : Timestamp(1376782169000, 1) }
可以看到这里的从是131
在slave上
> db.slaves.find()
{ "_id" : ObjectId("5202ef26b15304f923e5b3ce"), "host" : "192.168.78.131", "ns" : "local.oplog.rs", "syncedTo" : Timestamp(1375924079000, 1) }
> db.sources.find()
{ "host" : "192.168.78.130:10001", "source" : "main", "syncedTo" : Timestamp(1376782899000, 1) }
从集合sources中可以看到,master是130
从集合slaves中可以看懂,slave是131