MongoDBã«ããã¬ããªã±ã¼ã·ã§ã³ã®ä¸ç¨®ãReplica Setsã«ã¤ãã¦ã®æ¦è¦
MongoDBã§ã¯å種RDBMSã§æ¡ç¨ããã¦ããMaster/Slaveã§ã®ã¬ããªã±ã¼ã·ã§ã³æ¹å¼ã®ä»ã«Replica Setsã¨ããä»çµã¿ãå©ç¨ãããã¨ãã§ããã
ããã¯è¤æ°ã®DBããã»ã¹ãã¯ã©ã¹ã¿ãªã³ã°ãããã¨ã§åé·æ§ã確ä¿ããä»çµã¿ã§ã
å¾æ¥ã®ã¬ããªã±ã¼ã·ã§ã³ã¨éã£ã¦èªåã§ã®FailOverãå®ç¾ãã¦ããã
å
·ä½çã«ã¯Primaryã§ããã¡ã³ããå種ã¯ã¨ãªãåä»ãRead,Writeãè¡ãã
æ¸ãè¾¼ã¾ããå
容ã¯Secondaryã«ãã©ã¼ãªã³ã°ãããã
ä¸è¨å³ãåç
§ã
å®éã«Replica Setsãæ§ç¯ããçºã®æé ã以ä¸ã«ç¤ºãã¾ãã
è¤æ°ã®mongodããã»ã¹ãç«ã¡ä¸ããã
ãã®çºã®ãã¼ã¿ãã£ã¬ã¯ããªãä½æã
$ mkdir -p ./data/repltest1 $ mkdir -p ./data/repltest2 $ mkdir -p ./data/repltest3
mongodããã»ã¹ã®ç«ã¡ä¸ã
$ mongod --replSet repltest --port 27017 --dbpath ./data/repltest1 --rest $ mongod --replSet repltest --port 27018 --dbpath ./data/repltest2 --rest $ mongod --replSet repltest --port 27019 --dbpath ./data/repltest3 --rest
--restãªãã·ã§ã³ãä»ä¸ããã¨HTTP Admin UIã§ã®è©³ç´°è¡¨ç¤ºãå¯è½ã¨ãªãã
Replica Setsã®è¨å®
$ mongo localhost:27017
以ä¸mongoã³ã³ã½ã¼ã«ã§å
¥å
ã¯ã©ã¹ã¿ãå®ç¾©
_idã«ã¯mongodç«ã¡ä¸ãæã®--replSetã®å¤ãã»ããããã
config = {_id: 'repltest', members: [ {_id: 0, host: 'localhost:27017'}, {_id: 1, host: 'localhost:27018'}, {_id: 2, host: 'localhost:27019'}] }
å®ç¾©ããã¯ã©ã¹ã¿ãrs.initiate()ã«æµãããã
> rs.initiate(config); { "info" : "Config now saved locally. Should come online in about a minute.", "ok" : 1 }
ç¶æ ã確èª
> rs.status(); { "set" : "repltest", "date" : "Tue Oct 05 2010 11:56:23 GMT+0900 (JST)", "myState" : 1, "members" : [ { "_id" : 0, "name" : "HOSTNAME.local:27017", "health" : 1, "state" : 1, "self" : true }, { "_id" : 1, "name" : "localhost:27018", "health" : 1, "state" : 2, "uptime" : 14, "lastHeartbeat" : "Tue Oct 05 2010 11:56:21 GMT+0900 (JST)" }, { "_id" : 2, "name" : "localhost:27019", "health" : 1, "state" : 2, "uptime" : 14, "lastHeartbeat" : "Tue Oct 05 2010 11:56:21 GMT+0900 (JST)" } ], "ok" : 1 }
詳細ã¯HTTP Admin UIã§ç¢ºèªã§ããã
ãã©ã¦ã¶ã§http://localhost:28017ãéãã
(Admin UIã¯ããã©ã«ãã§ã¯mongodã§ã®portã«1000ãå ãããã®ãå²ãå½ã¦ããã)
Replica Setsã®è©³ç´°ã¯ä¸è¨URLãã確èªã
http://localhost:28017/_replSet
Secondaryã®çå確èªã¯2ç§ééã§å®æ½ãããã
ãã®ç¶æ
ã§Primaryãctrl + cçã§åæ¢ãã¦ã¿ãã
ããã¨ä¸è¨ã®ç¶æ
ã«é·ç§»ããã
å¾æ¥ã®SecondaryãPrimaryã«ææ ¼ãã¦ããã®ããããã
åã³åæ¢ãã¦ããããã»ã¹ãç«ã¡ä¸ããã¨Secondaryã¨ãã¦ãã¼ã¿ã®åæãè¡ãããã
ãã®ããã«Primaryãè½ã¡ãå ´åã§ãèªåã§ä»ã®ããã»ã¹ãææ ¼ãããã¨ã§auto failoverãå®ç¾ãã¦ããã
åèï¼MongoDB ReplicaSets
* mkdir ã®ãªãã·ã§ã³ã大æåã ã£ãã®ãä¿®æ£