一、环境
$ cat /etc/redhat-release CentOS Linux release 7.0.1406 (Core) $ uname -aLinux zhaopin-2-201 3.10.0-123.el7.x86_64 #1 SMP Mon Jun 30 12:09:22 UTC 2014 x86_64 x86_64 x86_64 GNU/Linux$ mongo --versionMongoDB shell version: 3.0.6
二、准备
1.创建目录
$ sudo mkdir -p /data/mongodb/{data/db0,backup/db0,log/db0,conf/db0}
2.编写配置文件
1)master:
$ sudo vim /data/mongodb/conf/db0/mongodb.conf# baseport = 27017maxConns = 800 filePermissions = 0700fork = truenoauth = truedirectoryperdb = truedbpath = /data/mongodb/data/db0pidfilepath = /data/mongodb/data/db0/mongodb.pidjournal = true# securitynohttpinterface = truerest = false# log logpath = /data/mongodb/log/db0/monodb.loglogRotate = renamelogappend = trueslowms = 50master = true
2)slave:
$ sudo vim /data/mongodb/conf/db0/mongodb.conf# baseport = 27017maxConns = 800filePermissions = 0700fork = truenoauth = truedirectoryperdb = truedbpath = /data/mongodb/data/db0pidfilepath = /data/mongodb/data/db0/mongodb.pidjournal = true# securitynohttpinterface = truerest = false# loglogpath = /data/mongodb/log/db0/mongodb.loglogRotate = renamelogappend = trueslowms = 50slave = truesource = 172.30.2.201:27017
三、启动
1.先启动master
$ sudo /opt/mongodb/bin/mongod --config /data/mongodb/conf/db0/mongodb.confabout to fork child process, waiting until server is ready for connections.forked process: 48583child process started successfully, parent exiting$ mongoMongoDB shell version: 3.0.6connecting to: test> db.isMaster();{ "ismaster" : 0, "info" : "dead: data too stale halted replication", "maxBsonObjectSize" : 16777216, "maxMessageSizeBytes" : 48000000, "maxWriteBatchSize" : 1000, "localTime" : ISODate("2015-09-23T07:47:52.957Z"), "maxWireVersion" : 3, "minWireVersion" : 0, "ok" : 1}>bye
2.启动slave
$ sudo /opt/mongodb/bin/mongod --config /data/mongodb/conf/db0/mongodb.confabout to fork child process, waiting until server is ready for connections.forked process: 36715child process started successfully, parent exiting$ mongoMongoDB shell version: 3.0.6connecting to: test> db.isMaster();{ "ismaster" : false, "maxBsonObjectSize" : 16777216, "maxMessageSizeBytes" : 48000000, "maxWriteBatchSize" : 1000, "localTime" : ISODate("2015-09-23T07:49:32.934Z"), "maxWireVersion" : 3, "minWireVersion" : 0, "ok" : 1}>bye
3. 设置开机启动
$ sudo vim /etc/rc.local/opt/mongodb/bin/mongod --config /data/mongodb/conf/db0/mongodb.conf
四、验证
1.数据同步
1) master:
$ mongoMongoDB shell version: 3.0.6connecting to: test> use aaa;switched to db aaa> db.createCollection("test");{ "ok" : 1 }> show collections;system.indexessystem.profiletest>
2) slave:
$ mongoMongoDB shell version: 3.0.6connecting to: test> rs.slaveOk();> show dbs;aaa 0.078GBlocal 0.078GB> use aaa;switched to db aaa> show collections;system.indexestest>
2.读写验证
由上可知,master是可读写的
在从库上写测试:
$ mongoMongoDB shell version: 3.0.6connecting to: test> use aaa;switched to db aaa> db.createCollection("test2");{ "note" : "from execCommand", "ok" : 0, "errmsg" : "not master" }> show collections;2015-09-23T15:57:49.240+0800 E QUERY Error: listCollections failed: { "note" : "from execCommand", "ok" : 0, "errmsg" : "not master" } at Error () at DB._getCollectionInfosCommand (src/mongo/shell/db.js:646:15) at DB.getCollectionInfos (src/mongo/shell/db.js:658:20) at DB.getCollectionNames (src/mongo/shell/db.js:669:17) at shellHelper.show (src/mongo/shell/utils.js:625:12) at shellHelper (src/mongo/shell/utils.js:524:36) at (shellhelp2):1:1 at src/mongo/shell/db.js:646> rs.slaveOk();> show collections;system.indexestest>
证明从库是不能写,默认也是不可读的,需要执行rs.slaveOk();或者db.getMongo().setSlaveOk();并且只对当前session有效,所以每次连接从库都需要执行。
五、故障测试及解决方案
1.master宕机
1) 将master停机
$ mongoMongoDB shell version: 3.0.6connecting to: test> use admin;switched to db admin> db.shutdownServer();2015-09-23T16:02:16.983+0800 I NETWORK DBClientCursor::init call() failedserver should be down...2015-09-23T16:02:16.986+0800 I NETWORK trying reconnect to 127.0.0.1:27017 (127.0.0.1) failed2015-09-23T16:02:16.987+0800 I NETWORK reconnect 127.0.0.1:27017 (127.0.0.1) ok2015-09-23T16:02:16.992+0800 I NETWORK Socket recv() errno:104 Connection reset by peer 127.0.0.1:270172015-09-23T16:02:16.992+0800 I NETWORK SocketException: remote: 127.0.0.1:27017 error: 9001 socket exception [RECV_ERROR] server [127.0.0.1:27017]2015-09-23T16:02:16.992+0800 I NETWORK DBClientCursor::init call() failed>bye$ ps aux | grep mongo | grep -v grep
2) 查看slave状态
$ mongoMongoDB shell version: 3.0.6connecting to: test> rs.slaveOk();> show dbs;aaa 0.078GBlocal 0.078GB> use aaa;switched to db aaa> show collections;system.indexestest> db.createCollection("test2");{ "note" : "from execCommand", "ok" : 0, "errmsg" : "not master" }
可见slave还是只读,不能写
3) 解决方法
将slave停机,变为master后启动
> use admin;switched to db admin> db.shutdownServer();2015-09-23T16:05:28.355+0800 I NETWORK DBClientCursor::init call() failedserver should be down...2015-09-23T16:05:28.357+0800 I NETWORK trying reconnect to 127.0.0.1:27017 (127.0.0.1) failed2015-09-23T16:05:28.357+0800 W NETWORK Failed to connect to 127.0.0.1:27017, reason: errno:111 Connection refused2015-09-23T16:05:28.358+0800 I NETWORK reconnect 127.0.0.1:27017 (127.0.0.1) failed failed couldn't connect to server 127.0.0.1:27017 (127.0.0.1), connection attempt failed>bye$ ps aux | grep mongo | grep -v grep$ sudo vim /data/mongodb/conf/db0/mongodb.confmaster = true#slave = true#source = 172.30.2.201:27017$ sudo /opt/mongodb/bin/mongod --config /data/mongodb/conf/db0/mongodb.confabout to fork child process, waiting until server is ready for connections.forked process: 36988child process started successfully, parent exiting$ mongoMongoDB shell version: 3.0.6connecting to: test> show dbs;aaa 0.078GBlocal 50.054GB> use aaa;switched to db aaa> db.createCollection("test2");{ "ok" : 1 }> show collections;system.indexessystem.profiletesttest2> db.isMaster();{ "ismaster" : true, "maxBsonObjectSize" : 16777216, "maxMessageSizeBytes" : 48000000, "maxWriteBatchSize" : 1000, "localTime" : ISODate("2015-09-23T08:07:41.812Z"), "maxWireVersion" : 3, "minWireVersion" : 0, "ok" : 1}>bye
这样slave就变为新的master了
2.slave宕机
1) 制作新的slave
$ sudo rm -fr /data/mongodb/data/db0/*$ sudo vim /data/mongodb/conf/db0/mongodb.conf#master = trueslave = truesource = 172.30.2.202:27017$ sudo /opt/mongodb/bin/mongod --config /data/mongodb/conf/db0/mongodb.confabout to fork child process, waiting until server is ready for connections.forked process: 48779child process started successfully, parent exiting$ mongoMongoDB shell version: 3.0.6connecting to: test> db.isMaster();{ "ismaster" : false, "maxBsonObjectSize" : 16777216, "maxMessageSizeBytes" : 48000000, "maxWriteBatchSize" : 1000, "localTime" : ISODate("2015-09-23T08:14:51.283Z"), "maxWireVersion" : 3, "minWireVersion" : 0, "ok" : 1}
2) 将新的slave停机
> use admin;switched to db admin> db.shutdownServer();2015-09-23T16:15:31.329+0800 I NETWORK DBClientCursor::init call() failedserver should be down...2015-09-23T16:15:31.331+0800 I NETWORK trying reconnect to 127.0.0.1:27017 (127.0.0.1) failed2015-09-23T16:15:31.332+0800 I NETWORK reconnect 127.0.0.1:27017 (127.0.0.1) ok2015-09-23T16:15:31.333+0800 I NETWORK Socket recv() errno:104 Connection reset by peer 127.0.0.1:270172015-09-23T16:15:31.333+0800 I NETWORK SocketException: remote: 127.0.0.1:27017 error: 9001 socket exception [RECV_ERROR] server [127.0.0.1:27017]2015-09-23T16:15:31.333+0800 I NETWORK DBClientCursor::init call() failed>bye$ ps aux | grep mongo | grep -v grep
3) 查看新master状况
$ mongoMongoDB shell version: 3.0.6connecting to: test> show dbs;aaa 0.078GBlocal 50.054GB> use aaa;switched to db aaa> db.createCollection("test3");{ "ok" : 1 }> show collections;system.indexessystem.profiletesttest2test3>bye
4) 结论
可见master是可以正常读写的,只需要再制作从库即可