标签:
$ cat /etc/redhat-release CentOS Linux release 7.0.1406 (Core) $ uname -a Linux 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 --version MongoDB shell version: 3.0.6
$ sudo mkdir -p /data/mongodb/{data/db0,backup/db0,log/db0,conf/db0}
$ sudo vim /data/mongodb/conf/db0/mongodb.conf # base port = 27017 maxConns = 800 filePermissions = 0700 fork = true noauth = true dbpath = /data/mongodb/data/db0 pidfilepath = /data/mongodb/data/db0/mongodb.pid journal = true # security nohttpinterface = true rest = false # log logpath = /data/mongodb/log/db0/mongodb.log logRotate = rename logappend = true slowms = 50 master = true
2)slave:
$ sudo vim /data/mongodb/conf/db0/mongodb.conf # base port = 27017 maxConns = 800 filePermissions = 0700 fork = true noauth = true dbpath = /data/mongodb/data/db0 pidfilepath = /data/mongodb/data/db0/mongodb.pid journal = true # security nohttpinterface = true rest = false # log logpath = /data/mongodb/log/db0/mongodb.log logRotate = rename logappend = true slowms = 50 slave = true source = 172.30.2.201:27017
$ sudo /opt/mongodb/bin/mongod --config /data/mongodb/conf/db0/mongodb.conf about to fork child process, waiting until server is ready for connections. forked process: 48583 child process started successfully, parent exiting $ mongo MongoDB shell version: 3.0.6 connecting 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
$ sudo /opt/mongodb/bin/mongod --config /data/mongodb/conf/db0/mongodb.conf about to fork child process, waiting until server is ready for connections. forked process: 36715 child process started successfully, parent exiting $ mongo MongoDB shell version: 3.0.6 connecting 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
$ mongo MongoDB shell version: 3.0.6 connecting to: test > use aaa; switched to db aaa > db.createCollection("test"); { "ok" : 1 } > show collections; system.indexes system.profile test >
$ mongo MongoDB shell version: 3.0.6 connecting to: test > rs.slaveOk(); > show dbs; aaa 0.078GB local 0.078GB > use aaa; switched to db aaa > show collections; system.indexes test >
由上可知,master是可读写的
在从库上写测试:
$ mongo MongoDB shell version: 3.0.6 connecting 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 (<anonymous>) 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.indexes test >
证明从库是不能写,默认也是不可读的,需要执行rs.slaveOk();或者db.getMongo().setSlaveOk();并且只对当前session有效,所以每次连接从库都需要执行。
$ mongo MongoDB shell version: 3.0.6 connecting to: test > use admin; switched to db admin > db.shutdownServer(); 2015-09-23T16:02:16.983+0800 I NETWORK DBClientCursor::init call() failed server should be down... 2015-09-23T16:02:16.986+0800 I NETWORK trying reconnect to 127.0.0.1:27017 (127.0.0.1) failed 2015-09-23T16:02:16.987+0800 I NETWORK reconnect 127.0.0.1:27017 (127.0.0.1) ok 2015-09-23T16:02:16.992+0800 I NETWORK Socket recv() errno:104 Connection reset by peer 127.0.0.1:27017 2015-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
$ mongo MongoDB shell version: 3.0.6 connecting to: test > rs.slaveOk(); > show dbs; aaa 0.078GB local 0.078GB > use aaa; switched to db aaa > show collections; system.indexes test > db.createCollection("test2"); { "note" : "from execCommand", "ok" : 0, "errmsg" : "not master" }
可见slave还是只读,不能写
将slave停机,变为master后启动
> use admin; switched to db admin > db.shutdownServer(); 2015-09-23T16:05:28.355+0800 I NETWORK DBClientCursor::init call() failed server should be down... 2015-09-23T16:05:28.357+0800 I NETWORK trying reconnect to 127.0.0.1:27017 (127.0.0.1) failed 2015-09-23T16:05:28.357+0800 W NETWORK Failed to connect to 127.0.0.1:27017, reason: errno:111 Connection refused 2015-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.conf master = true #slave = true #source = 172.30.2.201:27017 $ sudo /opt/mongodb/bin/mongod --config /data/mongodb/conf/db0/mongodb.conf about to fork child process, waiting until server is ready for connections. forked process: 36988 child process started successfully, parent exiting $ mongo MongoDB shell version: 3.0.6 connecting to: test > show dbs; aaa 0.078GB local 50.054GB > use aaa; switched to db aaa > db.createCollection("test2"); { "ok" : 1 } > show collections; system.indexes system.profile test test2 > 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了
$ sudo rm -fr /data/mongodb/data/db0/* $ sudo vim /data/mongodb/conf/db0/mongodb.conf #master = true slave = true source = 172.30.2.202:27017 $ sudo /opt/mongodb/bin/mongod --config /data/mongodb/conf/db0/mongodb.conf about to fork child process, waiting until server is ready for connections. forked process: 48779 child process started successfully, parent exiting $ mongo MongoDB shell version: 3.0.6 connecting 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 }
> use admin; switched to db admin > db.shutdownServer(); 2015-09-23T16:15:31.329+0800 I NETWORK DBClientCursor::init call() failed server should be down... 2015-09-23T16:15:31.331+0800 I NETWORK trying reconnect to 127.0.0.1:27017 (127.0.0.1) failed 2015-09-23T16:15:31.332+0800 I NETWORK reconnect 127.0.0.1:27017 (127.0.0.1) ok 2015-09-23T16:15:31.333+0800 I NETWORK Socket recv() errno:104 Connection reset by peer 127.0.0.1:27017 2015-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
$ mongo MongoDB shell version: 3.0.6 connecting to: test > show dbs; aaa 0.078GB local 50.054GB > use aaa; switched to db aaa > db.createCollection("test3"); { "ok" : 1 } > show collections; system.indexes system.profile test test2 test3 > bye
可见master是可以正常读写的,只需要再制作从库即可
标签:
原文地址:http://my.oschina.net/u/2426299/blog/510183