部署MongoDB主从复制:
1.规划如下:
|
服务器IP |
服务器角色 |
|
172.23.214.47 |
Master |
|
172.23.214.50 |
Slave |
2.配置主从
编辑Master配置文件:
root@namenode2:/var/log/mongo# grep -v "#" /etc/mongod.conf |grep -v "^$"
logpath=/var/log/mongo/mongod.log
logappend=true
fork = true
port = 27017
dbpath=/mongo/data
pidfilepath = /var/run/mongodb/mongod.pid
rest = true
maxConns=1024
master=true #将指定其为master
oplogSize=2048
编辑Slave配置文件
root@namenode1:/etc# cat mongod.conf.bak_slave | grep -v "#" | grep -v"^$"
logpath=/var/log/mongo/mongod.log
logappend=true
fork = true
port = 27017
dbpath=/mongo/data
maxConns = 1024
slave = true #指定其为slave
source = 172.23.214.47:27017 #指定master的主机地址及端口号
autoresync = true
pidfilepath = /var/run/mongodb/mongod.pid
编辑好配置文件,双方启动mongodb
root@namenode1: mongod --f /etc/mongod.conf
主库查看日志信息:
root@namenode2:/var/log/mongo# cat mongod.log
Tue Mar 18 13:24:32.115 [initandlisten] connection accepted from172.23.214.50:57356 #2 (1 connection now open)
从库查看日志信息
root@namenode1:/var/log/mongo# tail mongod.log | grep 47
Tue Mar 18 13:24:30.025 [initandlisten] options: { autoresync:"true", config: "/etc/mongod.conf", dbpath:"/mongo/data", fork: "true", logappend: "true",logpath: "/var/log/mongo/mongod.log", maxConns: 1024, pidfilepath:"/var/run/mongodb/mongod.pid", port: 27017, slave: "true",source: "172.23.214.47:27017" }
Tue Mar 18 13:24:31.188 [replslave] repl: syncingfrom host:172.23.214.47:27017
验证:主库创建库,并在从库查看
> db.testcoll.find()
> show collections
> db.testcoll.insert ( {name:"tom"})
> show collections
system.indexes
testcoll
> db.testcoll.find()
{ "_id" : ObjectId("5327dabc9d62778b7a74ccab"),"name" : "tom" }
在从库上查看
>show dbs;
local 0.078125GB
testdb 0.203125GB
> use testdb;
switched to db testdb
> db.testcoll.find()
{ "_id" : ObjectId("5327dabc9d62778b7a74ccab"),"name" : "tom" }
查看从库日志
root@namenode1:/var/log/mongo# tail mongod.log
Tue Mar 18 13:33:48.042 [replslave] build index testdb.testcoll { _id: 1 }
Tue Mar 18 13:33:48.042 [replslave] fastBuildIndexdupsToDrop:0
Tue Mar 18 13:33:48.043 [replslave] build index done. scanned 1 totalrecords. 0 secs
Tue Mar 18 13:33:48.062 [replslave] resync: done with initial clone for db:testdb
Tue Mar 18 13:34:55.757 [initandlisten] connection accepted from127.0.0.1:32942 #1 (1 connection now open)
Tue Mar 18 13:35:53.979 [replslave] repl: checkpoint applied 15operations
Tue Mar 18 13:35:53.979 [replslave] repl: syncedTo: Mar 18 13:35:445327db30:1
Tue Mar 18 13:38:06.018 [conn1] end connection 127.0.0.1:32942 (0 connectionsnow open)
Tue Mar 18 13:38:23.981 [replslave] repl: checkpoint applied 15operations
Tue Mar 18 13:38:23.982 [replslave] repl: syncedTo: Mar 18 13:38:145327dbc6:1
登录主库管理页面,查看其状态信息(master:1)

