ãŸããã¯ã©ã¹ã¿ãŒæ§æã®è¿œå /å€æŽã«é¢ããæšå¥šäºé ãšãèšäºèªäœãŸãã¯äž»é¡ã«é¢ãã質åãŸãã¯æ¹å€ã«éåžžã«æºè¶³ããŠããŸãã
ãšã³ããªãŒ
ã¯ã©ã¹ã¿ãŒãå°å ¥ããããããžã§ã¯ãã¯ãã¯ã©ã€ã¢ã³ãããã€ã¹ã®çµ±èšãåéãããµã€ããŸãã¯Rest-APIãä»ããŠãã®ããããžã§ãã³ã°ãéçŽãããµãŒãã¹ã§ãã ãããžã§ã¯ãã¯é·ãéãäœè² è·ã§å®å®ããŠåäœããŠããããã®çµæãããã®ãŸãŸãã€ã³ã¹ããŒã«ãããMongoDBãµãŒããŒã¯ïŒã·ã£ãŒãã£ã³ã°ãšããŒã¿è€è£œãªãã§ïŒå®ç§ã«æ©èœããå ã®ããŒã¿ããŒã¹ã®æ¯æ¥ã®ããã¯ã¢ããã«ãã£ãŠãéããªç¡ç ããæäŸãããŸããã Thunderã¯ãå€æ°ã®ããã€ã¹ãããŒã¿ãããã³ãªã¯ãšã¹ããæã€ããã€ãã®å€§èŠæš¡ãªã¯ã©ã€ã¢ã³ããå°çããåŸãéåžžã©ããçŽ æŽãããç¬éãè¿ããŸããã ãã®çµæãå€ãããŒã¿ããŒã¹ãžã®ã¯ãšãªã®èš±å®¹ã§ããªãã»ã©é·ãå®è¡ãè¡ããããã®çµæãããŒã¿ãã»ãšãã©å€±ããããšãã«ãµãŒããŒãäžæããŸããã
ãããã£ãŠãäžæ©ã§ãå°æ¥ã®ã¹ã±ãŒã©ããªãã£ã®å¯èœæ§ãšãšãã«ããã©ãŒã«ããã¬ã©ã³ã¹ãããŒã¿ã»ãã¥ãªãã£ãããã³çç£æ§ãåäžãããäœæ¥ãå®è¡ããå¿ èŠãçããŸããã MongoDBã®æ¢åã®å¯èœæ§ã䜿çšããŠãçºçããåé¡ãæé€ããããšãã€ãŸããã¬ããªã±ãŒã·ã§ã³ã䜿çšããŠã·ã£ãŒãã¯ã©ã¹ã¿ãŒãæŽçããæ¢åã®ããŒã¿ãããã«ç§»è¡ããããšã決å®ãããŸããã
çè«ã®ããã
ãŸããShardedCluster MongoDBãšãã®äž»èŠã³ã³ããŒãã³ãã«ã€ããŠå°ãç解ããŠãã ããã ã·ã£ãŒãã£ã³ã°èªäœã¯ãããŒã¿ãä¿åããŠããŒã¿ãžã®ã¢ã¯ã»ã¹ãæäŸããã³ã³ãã¥ãŒãã£ã³ã°ã·ã¹ãã ã®æ°Žå¹³ã¹ã±ãŒãªã³ã°ã®æ¹æ³ã§ãã åçŽã¹ã±ãŒãªã³ã°ãšã¯ç°ãªããããšãã°ããã匷åãªCPUã«åãæ¿ãããã䜿çšå¯èœãªRAMãŸãã¯ãã£ã¹ã¯é åãè¿œå ãããªã©ãåäžãµãŒããŒã®ããã©ãŒãã³ã¹ãåäžãããããšã§ã·ã¹ãã ããã©ãŒãã³ã¹ãåäžã§ããå Žåãã·ã£ãŒãã£ã³ã°ã¯ããŒã¿ã»ãããšè² è·ãè€æ°ã®ãµãŒããŒã«åæ£ããããšã§æ©èœããŸãå¿ èŠã«å¿ããŠæ°ãããµãŒããŒãè¿œå ããŸãïŒããã¯åãªãã±ãŒã¹ã§ãïŒã
ãã®ãããªã¹ã±ãŒãªã³ã°ã®å©ç¹ã¯ãæ¡åŒµã®å¯èœæ§ãã»ãŒç¡éã§ããããšã§ããäžæ¹ãåçŽæ¹åã«ã¹ã±ãŒã©ãã«ãªã·ã¹ãã ã¯ãããšãã°ãã¹ãã£ã³ã°ãããã€ããŒããå©çšå¯èœãªããŒããŠã§ã¢ã«ãã£ãŠæå³çã«å¶éãããŸãã
MongoDBã·ã£ãŒãã¯ã©ã¹ã¿ãŒãžã®åãæ¿ãããäœãæåŸ ãããŸããïŒ ãŸããã¯ã©ã¹ã¿ãŒã®ã·ã£ãŒãéã§èªã¿åã/æžã蟌ã¿æäœã®è² è·åæ£ãååŸããå¿ èŠããããŸãã次ã«ãé«ããã©ãŒã«ããã¬ã©ã³ã¹ïŒäžå®ã®ããŒã¿å¯çšæ§ïŒãšéå°ãªã³ããŒïŒã¬ããªã±ãŒã·ã§ã³ïŒã«ããããŒã¿ã®å®å šæ§ãå®çŸããå¿ èŠããããŸãã
MongoDBã®å ŽåãããŒã¿ã·ã£ãŒãã£ã³ã°ã¯ã³ã¬ã¯ã·ã§ã³ã¬ãã«ã§çºçããŸããã€ãŸããæ¢åã®ã¯ã©ã¹ã¿ãŒã·ã£ãŒãéã§ã©ã®ã³ã¬ã¯ã·ã§ã³ããŒã¿ãåæ£ããããæ瀺çã«æå®ã§ããŸãã ããã¯ãåå²å¯èœãªã³ã¬ã¯ã·ã§ã³ã®ããã¥ã¡ã³ãã»ããå šäœãåããµã€ãºã®éšåïŒãã£ã³ã¯ïŒãã£ã³ã¯ïŒã«åå²ãããããšãæå³ããŸãããã£ã³ã¯ïŒãã£ã³ã¯ïŒã¯ãã¢ã³ã¬ãã©ã³ãµãŒã«ãã£ãŠã¯ã©ã¹ã¿ãŒã®ã·ã£ã¬ãŒãéã§ã»ãŒåçã«ç§»è¡ãããŸãïŒã
ãã¹ãŠã®ããŒã¿ããŒã¹ãšã³ã¬ã¯ã·ã§ã³ã®ã·ã£ãŒãã£ã³ã°ã¯ããã©ã«ãã§ç¡å¹ã«ãªã£ãŠããã adminãconfigãªã©ã®ã¯ã©ã¹ã¿ãŒã·ã¹ãã ããŒã¿ããŒã¹ãã·ã£ãŒãããããšã¯ã§ããŸããã ãããå®è¡ããããšãããšãMongaããæ確ãªæåŠãåŸãããŸãã
mongos> sh.enableSharding("admin") { "ok" : 0, "errmsg" : "can't shard admin database" }
ã·ã£ããã«ãããMongoDBã¯ã©ã¹ã¿ãŒã«ã¯ã3ã€ã®åææ¡ä»¶ããããŸããå®éã ã·ã£ãŒãã®ååšãã¯ã©ã¹ã¿ãŒãšãã®ã¯ã©ã€ã¢ã³ãéã®ãã¹ãŠã®éä¿¡ã¯æä»çã«mongosã«ãŒã¿ãŒãä»ããŠå®è¡ãããå¿ èŠããããŸããç¬ç°ïŒ
mongodbã®å ¬åŒããã¥ã¡ã³ãã«ã¯ã ãå®çšŒåç°å¢ã§ã¯ããã¹ãŠã®ã·ã£ãŒãã¯ã¬ããªã«ã»ããã§ããå¿ èŠããããŸãããšæžãããŠããŸãã ã¬ããªã«ã»ããã§ãããããåã·ã£ãŒãã¯ãããŒã¿ã®è€æ°ã®ã³ããŒã«ããããã©ãŒã«ããã¬ã©ã³ã¹ãåäžããïŒã¬ããªã«ã®ä»»æã®ã€ã³ã¹ã¿ã³ã¹ã§ã®ããŒã¿å¯çšæ§ã®é¢ã§ïŒããããŠãã¡ãããæé«ã®ä¿åãä¿èšŒããŸãã
ã¬ããªã«ã»ããã¯ãåãããŒã¿ã»ããã®ã³ããŒãä¿åããè€æ°ã®å®è¡äžã®mongodã€ã³ã¹ã¿ã³ã¹ã®çµã¿åããã§ãã ã·ã£ãŒãã¬ããªã«ã®å Žåãããã¯mongaãã©ã³ãµãŒã«ãã£ãŠãã®ã·ã£ãŒãã«æž¡ããããã£ã³ã¯ã®ã»ããã«ãªããŸãã
ã¬ããªã«ã€ã³ã¹ã¿ã³ã¹ã®1ã€ãã¡ã€ã³ã€ã³ã¹ã¿ã³ã¹ïŒ PRIMARY ïŒãšããŠå²ãåœãŠããããã¹ãŠã®ããŒã¿æžã蟌ã¿æäœãïŒä¿å®ããã³èªã¿åãäžã«ïŒåãå ¥ããæ®ãã®ã¢ã³ãŽããã¯SECONDARYãšå®£èšãããPRIMARYãšã®éåæéä¿¡ã§ãããŒã¿ã»ããã®ã³ããŒãæŽæ°ããŸãã ããŒã¿ã®èªã¿åãã«ã䜿çšã§ããŸãã äœããã®çç±ã§PRIMARYãã¢ã¯ã»ã¹äžèœã«ãªããã¬ããªã«ã®ä»ã®ã¡ã³ããŒãšã®ããåããããããšãã¬ããªã«ã®äœ¿çšå¯èœãªãã¹ãŠã®ã¡ã³ããŒã®ãã¡ãæ°ããPRIMARYã®åœ¹å²ã«å¯Ÿããæ祚ãçºè¡šãããŸãã å®éãPRIMARYããã³SECONDARYã«å ããŠãã¬ããªã«ã»ããã«ã¯ç¬¬3ã®çš®é¡ã®åå è ãååšããå¯èœæ§ããããŸãããããã¢ãŒãã¿ãŒïŒ ARBITER ïŒã§ãã
ã¬ããªã«ã®ã¢ãŒãã¿ãŒã¯ãããŒã¿ã»ãããã³ããŒãã圹å²ãæãããã代ããã«æ祚ã®éèŠãªã¿ã¹ã¯ã解決ããæ祚ã®è¡ãæ¢ãŸãããã¬ããªã«ãä¿è·ããããã«èšèšãããŠããŸãã ã¬ããªã«ã«å¶æ°äººã®åå è ãããŠãåãåèšæ祚æ°ã§2人ã®å¿åè ã«åæ°ã§æ祚ããç¶æ³ãæ³åããŠãã ããããã®ãããªãå¶æ°ãã®çºèšã«ä»²è£äººãè¿œå ãããšã圌ã¯ãããŒã¿ã»ããã®å¥ã®ã³ããŒãåŠçããããã®ãªãœãŒã¹ãå¿ èŠãšããã«ããPRIMARYãã®äœçœ®ã
ã¬ããªã«ã»ããã¯ããŸãã«mongodã€ã³ã¹ã¿ã³ã¹ã®çµåã§ããããšã«æ³šæããŠãã ãããã€ãŸãã1å°ã®ãµãŒããŒã«ã¬ããªã«ãæ§ç¯ããç°ãªãç©çã¡ãã£ã¢ã«ãããã©ã«ããŒãããŒã¿ã¹ãã¬ãŒãžãšããŠæå®ããããŒã¿ã»ãã¥ãªãã£ãå®çŸããããšã劚ããŸããããããã§ãçæ³çãªãªãã·ã§ã³ã§ããããã¯ãç°ãªããµãŒããŒã§mongodãèµ·åããã¬ããªã«çµç¹ã§ãã äžè¬çã«ãããã«é¢ããŠãMongoDBã·ã¹ãã ã¯éåžžã«æè»ã§ãããå³ããå¶éã課ãããšãªããããŒãºãšæ©èœã«åºã¥ããŠå¿ èŠãªæ§æãçµã¿ç«ãŠãããšãã§ããŸãã ã·ã£ãŒãã¯ã©ã¹ã¿ãŒã®ã³ã³ããã¹ãå€ã®ã¬ããªã«ã»ããã¯ãå žåçãªMongoDBãµãŒããŒçµç¹ã¹ããŒã ã®1ã€ã§ãããé«åºŠãªãã©ãŒã«ããã¬ã©ã³ã¹ãšããŒã¿ä¿è·ãæäŸããŸãã ãã®å Žåãã¬ããªã«ã®ååå è ã¯ãã·ã£ãŒããã£ã³ã¯ã®ã»ããã§å®çŸ©ãããéšåã§ã¯ãªããããŒã¿ããŒã¹ããŒã¿ã»ããå šäœã®å®å šãªã³ããŒãä¿åããŸãã
ã€ã³ãã©
以äžã®ã¯ã©ã¹ã¿ãŒæ§æã¯ã3ã€ã®OpenVZä»®æ³ã³ã³ãããŒïŒVBOïŒã§æ§ç¯ãããŠããŸãã åä»®æ³ãã·ã³ã¯ãåå¥ã®å°çšãµãŒããŒã«é 眮ãããŸãã
2ã€ã®ä»®æ³ãã·ã³ïŒä»¥éserver1.cluster.comããã³server2.cluster.com ïŒã«ã¯ããå€ãã®ãªãœãŒã¹ããããŸã -ãããã¯ã¯ã©ã€ã¢ã³ããžã®ããŒã¿ã®è€è£œãåå²ãæäŸãæ åœããŸãã 3çªç®ã®ãã·ã³ïŒ server3.cluster.com ïŒã®æ§æã¯ãã匱ãããã®ç®çã¯mongodã¢ãŒãã¿ãŒã®ã€ã³ã¹ã¿ã³ã¹ã®åäœãä¿èšŒããããšã§ãã
æ§ç¯ãããã¯ã©ã¹ã¿ãŒã«ã¯ã3ã€ã®ã·ã£ã¬ãŒãããããŸãã ãã®ã¹ããŒã ã§ã¯ãã¬ããªã«ã»ããã«åºã¥ããŠã·ã£ãŒããæ§ç¯ãããšããæšå¥šäºé ã«åŸããŸããããããã€ãã®ä»®å®ããããŸããã ã¯ã©ã¹ã¿ãŒã®åã·ã£ãŒãã¬ããªã«ã»ããã«ã¯ã3ã€ã®ç°ãªããµãŒããŒã§å®è¡ãããç¬èªã®PRIMARYãSECONDARYãããã³ARBITERããããŸãã ãŸããããŒã¿è€è£œã䜿çšããŠæ§ç¯ãããæ§æãµãŒããŒããããŸãã
ãã ãããµãŒããŒã¯3ã€ãããªãããã®ãã¡ã®1ã€ã¯ããŒã¿è€è£œæ©èœãå®è¡ããŸããïŒæ§æã¬ããªã«ã®å Žåã®ã¿ïŒããããã£ãŠã3ã€ã®æçã¯ãã¹ãŠ2ã€ã®ãµãŒããŒã«å®éã«é 眮ãããŸãã
Mongiããã¥ã¡ã³ãã®å³ã§ã¯ãMongosã¯ã¢ããªã±ãŒã·ã§ã³ãµãŒããŒäžã«æãããŠããŸãã ç§ã¯ãã®ã«ãŒã«ãç ŽããMongosïŒ2ã€ãããŸãïŒãããŒã¿ãµãŒããŒã«é 眮ããããšã«ããŸããïŒserver1.cluster.comãšserver2.cluster.comãã¢ããªã±ãŒã·ã§ã³ãµãŒããŒäžã®mongodbã®è¿œå ã®æ§æãåãé€ããã¢ããªã±ãŒã·ã§ã³ãµãŒããŒã«é¢é£ããç¹å®ã®å¶éã®ããã ã¢ããªã±ãŒã·ã§ã³ãµãŒããŒã¯2ã€ã®Mongoã®ããããã«æ¥ç¶ã§ãããããäžæ¹ã«åé¡ãçºçããå Žåãçãã¿ã€ã ã¢ãŠãåŸã«ããäžæ¹ã«åæ¥ç¶ããŸãã 次ã«ãã¢ããªã±ãŒã·ã§ã³ãµãŒããŒã¯ãã©ãŠã³ãããã³ãæ§æãããŠããDNSçªç®ã®èåŸã«é 眮ãããŸãã 亀äºã«2ã€ã®ã¢ãã¬ã¹ã®ãããããçºè¡ããæ¥ç¶ïŒã¯ã©ã€ã¢ã³ãèŠæ±ïŒã®åºæ¬çãªãã©ã³ã¹ãæäŸããŸãã ãããããçš®ã®ãã¹ããŒããªãDNSã«çœ®ãæããèšç»ããããŸãïŒèª°ããã³ã¡ã³ãã§è¯ã解決çãæããŠããããããããŸãããæè¬ããŸãïŒïŒã¯ã©ã€ã¢ã³ãã®äœçœ®æ å ±ã«é©åãªãµãŒããŒãçºè¡ããããã
æ確ã«ããããã«ã圢æãããã¯ã©ã¹ã¿ãŒã®äžè¬çãªå³ã«ããµãŒããŒãšãããã§å®è¡ãããŠããã¢ããªã±ãŒã·ã§ã³ã®ååã瀺ããŸãã ã³ãã³ã¯ãæå®ãããã¢ããªã±ãŒã·ã§ã³ããŒãã瀺ããŸãã
åæèšå®
server1.cluster.comã«ã¢ã¯ã»ã¹ããå ¬åŒãªããžããªããææ°ã®MongoDB Community Editionããã±ãŒãžãã€ã³ã¹ããŒã«ããŸãã ã¢ã»ã³ããªã®æç¹ã§ãã¯ã©ã¹ã¿ãŒã¯ããŒãžã§ã³3.2.8ã§ãã ç§ã®å ŽåãDebian 8ãªãã¬ãŒãã£ã³ã°ã·ã¹ãã ã¯ãã¹ãŠã®ã¯ã©ã¹ã¿ãŒãã·ã³ã«ã€ã³ã¹ããŒã«ãããŠããŸããOSã«ã€ã³ã¹ããŒã«ããããã®è©³çŽ°ãªæé ã¯ãå ¬åŒããã¥ã¡ã³ãã§èŠã€ããããšãã§ããŸãã
å ¬éããŒãã·ã¹ãã ã«ã€ã³ããŒãããããã±ãŒãžã®ãªã¹ããæŽæ°ããäžé£ã®ãŠãŒãã£ãªãã£ã䜿çšããŠmongodbãµãŒããŒãã€ã³ã¹ããŒã«ããŸãã
server1.cluster.com:~# apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv EA312927 server1.cluster.com:~# echo "deb http://repo.mongodb.org/apt/debian wheezy/mongodb-org/3.2 main" | sudo tee /etc/apt/sources.list.d/mongodb-org-3.2.list server1.cluster.com:~# apt-get update server1.cluster.com:~# apt-get install -y mongodb-org
ã§ããïŒ å®è¡ãããã¢ã¯ã·ã§ã³ã®çµæãšããŠããã·ã³MongoDBã§ãã§ã«çšŒåããŠãããµãŒããŒãååŸããŸãã ä»ã®ãšãããmongodãµãŒãã¹ãç¡å¹ã«ããŸãïŒããã«æ»ããŸãïŒã
server1.cluster.com:~# service mongod stop
次ã«ãã¯ã©ã¹ã¿ãŒã®ãã¹ãŠã®ããŒã¿ãä¿åãããã£ã¬ã¯ããªãäœæããŸããããããã¹ã/ root / mongodbãã«é 眮ããŸãã å éšã§ã¯ã次ã®ãã£ã¬ã¯ããªæ§é ã圢æããŸãã
. âââ cfg âââ data â âââ config â âââ rs0 â âââ rs1 â âââ rs2 âââ keys âââ logs
dataãµããã£ã¬ã¯ããªã«ãã¬ããªã«ïŒæ§æã¬ããªã«ãå«ãïŒã®ããŒã¿ãçŽæ¥ä¿åããŸãã cfgã§ã¯ãå¿ èŠãªmongo {d / s}ã€ã³ã¹ã¿ã³ã¹ãå®è¡ããããã®æ§æãã¡ã€ã«ãäœæããŸãã ããŒã§ã¯ãã¯ã©ã¹ã¿ãŒã¡ã³ããŒã®x.509èªèšŒçšã®ããŒãšèšŒææžãã³ããŒããŸãã ãã°ãã©ã«ãã®ç®çã¯ã誰ããç解ããŠãããšæããŸãã
åæ§ã«ãã€ã³ã¹ããŒã«ãšãã£ã¬ã¯ããªã®æé ãæ®ãã®2å°ã®ãµãŒããŒã§ç¹°ãè¿ãå¿ èŠããããŸãã
ã¯ã©ã¹ã¿ãŒã®ã³ã³ããŒãã³ãã®æ§æãšãªã³ã¯ã«é²ãåã«ãå¿ èŠãªãã¹ãŠãæ©èœããããšã確èªããŠãã ããã ã/ root / mongodb / data / rs0ãã§ããŒã¿ã®ãã£ã¬ã¯ããªãæå®ããŠãããŒã27000ã§mongodã€ã³ã¹ã¿ã³ã¹ãå®è¡ããŸãã
mongod --port 27000 --dbpath /root/mongodb/data/rs0
åããµãŒããŒã§å¥ã®ã¿ãŒããã«ãéããå®è¡äžã®mongodã«æ¥ç¶ããŸãã
mongo --port 27000
ãã¹ãŠãããŸãããã°ãã·ã§ã«mongodbã«ãªããããã€ãã®ã³ãã³ããå®è¡ã§ããŸãã ããã©ã«ãã§ã¯ãmongã¯ãã¹ãããŒã¿ããŒã¹ã«åãæ¿ããŸããã³ãã³ããå ¥åããããšã§ããã確èªã§ããŸãã
> db.getName() test
次ã®ã³ãã³ãã§äžèŠãªããŒã¿ããŒã¹ãåé€ããŸãã
> db.dropDatabase() { "ok" : 1 }
ãããŠãæ°ããããŒã¿ããŒã¹ãåæåããŠãããã«åãæ¿ããŠå®éšããŸãã
> use analytics switched to db analytics
次ã«ãããŒã¿ãå ¥åããŠãã ããã æ ¹æ ã«ãªããªãããã«ããããã«ãç¹å®ã®çµ±èšãåéããã·ã¹ãã ã®äŸã䜿çšããŠãèšäºã§ãã以éã®ãã¹ãŠã®æäœãæ€èšããããšãææ¡ããŸãã
ããã€ãã®ããã€ã¹ãè¿œå ããŸãã
> db.sensors.insert({'s':1001, 'n': 'Sensor1001', 'o': true, 'ip': '192.168.88.20', 'a': ISODate('2016-07-20T20:34:16.001Z'), 'e': 0}) WriteResult({ "nInserted" : 1 }) > db.sensors.insert({'s':1002, 'n': 'Sensor1002', 'o': false, 'ip': '192.168.88.30', 'a': ISODate('2016-07-19T13:40:22.483Z'), 'e': 0}) WriteResult({ "nInserted" : 1 })
ããã«
sã¯ã»ã³ãµãŒã®ã·ãªã¢ã«çªå·ã§ãã
nã¯ãã®æååèå¥åã§ãã
o-çŸåšã®ã¹ããŒã¿ã¹ïŒãªã³ã©ã€ã³/ãªãã©ã€ã³ïŒ;
ip-ã»ã³ãµãŒã®IPã¢ãã¬ã¹ã
aã¯æåŸã®ã¢ã¯ãã£ããã£ã®æéã§ãã
eã¯ãšã©ãŒã®å åã§ãã
ãããŠä»ããã©ãŒã ã®ããã€ãã®çµ±èšã¬ã³ãŒãïŒ
> db.statistics.insert({'s':1001, 'ts': ISODate('2016-08-04T20:34:16.001Z'), 'param1': 123, 'param2': 23.45, 'param3': âOKâ, 'param4': True, 'param5': '-1000', 'param6': [1,2,3,4,5]) WriteResult({ "nInserted" : 1 })
sã¯ã»ã³ãµãŒã®çªå·ã§ãã
ts-ã¿ã€ã ã¹ã¿ã³ãã
param1..param6-ããã€ãã®çµ±èšã
çµ±èšåæãµãŒãã¹ã®ã¯ã©ã€ã¢ã³ãã¯ããã°ãã°éçŽãããã¯ãšãªãå®è¡ããŠãããã€ã¹ããåéãããçµ±èšã«é¢ãã代衚çãªããŒã¿ãååŸããŸãã ã»ãšãã©ãã¹ãŠã®ã¯ãšãªã§ã¯ããã»ã³ãµãŒã·ãªã¢ã«çªå·ãïŒãã£ãŒã«ãsïŒãé¢ä¿ããŠããŸãã ãœãŒããšã°ã«ãŒãåãããé©çšããããããæé©åïŒããã³ã·ã£ãŒãã£ã³ã°ïŒã®ããã«ãçµ±èšã³ã¬ã¯ã·ã§ã³ã«ã€ã³ããã¯ã¹ãè¿œå ããŸãã
mongos> db.statistics.ensureIndex({"s":1})
å¿ èŠãªã€ã³ããã¯ã¹ã®éžæãšäœæã¯å¥ã®è°è«ã®ãããã¯ã§ãããä»ã®ãšããã¯ããã«éå®ããŸãã
x.509蚌ææžã䜿çšããèªèšŒ
åé¡ãç解ããããã«ãå°ãé²ãã§ãç°ãªããµãŒããŒã§å®è¡ãããŠããmongodã€ã³ã¹ã¿ã³ã¹ãæ³åããŸããã¬ããªã«ã«çµåããmongosããããã«æ¥ç¶ãã圢æãããã¯ã©ã¹ã¿ãŒã«ã¯ã©ã€ã¢ã³ããå®å šã«æ¥ç¶ã§ããããã«ããŸãã ãã¡ãããããŒã¿äº€æã®åå è ã¯æ¥ç¶æã«èªèšŒãããå¿ èŠãããïŒä¿¡é ŒãããïŒãããŒã¿ãã£ãã«ãä¿è·ãããããšãæãŸããã§ãã ãã®å ŽåãMongoDBã«ã¯TSL / SSLã®ãµããŒããšããã€ãã®èªèšŒã¡ã«ããºã ããããŸãã ã¯ã©ã¹ã¿ãŒå ã®ããŒã¿äº€æã®åå è éã§ä¿¡é Œã確ç«ããããã®ãªãã·ã§ã³ã®1ã€ã¯ãããŒãšèšŒææžã®äœ¿çšã§ãã ãã®ãªãã·ã§ã³ã䜿çšããã¡ã«ããºã ã®éžæã«é¢ããŠã¯ãMongiããã¥ã¡ã³ãã«æšå¥šäºé ããããŸãã
ãããŒãã¡ã€ã«ã¯æäœéã®ã»ãã¥ãªãã£åœ¢åŒã§ããããã¹ãç°å¢ãŸãã¯éçºç°å¢ã«æé©ã§ãã å®çšŒåç°å¢ã§ã¯ã x.509蚌ææžã®äœ¿çšããå§ãããŸã ã
X.509ã¯ãå ¬ééµã€ã³ãã©ã¹ãã©ã¯ãã£ãšç¹æš©ç®¡çã®ããã®ITU-Tæšæºã§ãã ãã®èŠæ Œã¯ã眲åãããããžã¿ã«èšŒææžã䜿çšããŠã圢åŒãšå ¬éããŒã®é åžæ¹æ³ãå®çŸ©ããŸãã 蚌ææžã¯ãå ¬éããŒããµããžã§ã¯ãïŒèšŒææžã®ãŠãŒã¶ãŒïŒã«é¢é£ä»ããŸãã ãã®æ¥ç¶ã®ä¿¡é Œæ§ã¯ãä¿¡é Œã§ããèªèšŒå±ã«ãã£ãŠå®è¡ãããããžã¿ã«çœ²åã«ãã£ãŠå®çŸãããŸãã
ïŒx.509ã«å ããŠãMongoDBã«ã¯éåžžã«ä¿¡é Œæ§ã®é«ããšã³ã¿ãŒãã©ã€ãºã¬ãã«ã®ã¡ãœããããããŸã -ãããã¯KerberosèªèšŒãšLDAPãããã·èªèšŒå±èªèšŒã§ã ïŒãããã¯ç§ãã¡ã®å Žåã§ã¯ãªããããã§x.509èªèšŒã®èšå®ã«ã€ããŠèª¬æããŸãã
x.509蚌ææžã䜿çšããèªèšŒã¡ã«ããºã ã«ã¯ãã¯ã©ã¹ã¿ãŒãžã®ã»ãã¥ã¢ãªTSL / SSLæ¥ç¶ãå¿ èŠã§ããããã¯ã察å¿ããmongod startåŒæ°--sslMode ããŸãã¯æ§æãã¡ã€ã«ã®net.ssl.modeãã©ã¡ãŒã¿ãŒã«ãã£ãŠæå¹ã«ãªããŸãã ãã®å ŽåããµãŒããŒã«æ¥ç¶ããã¯ã©ã€ã¢ã³ãã®èªèšŒã¯ããã°ã€ã³ãšãã¹ã¯ãŒãã§ã¯ãªãã蚌ææžã®èªèšŒã«ãªããŸãã
ãã®ã¡ã«ããºã ã®ã³ã³ããã¹ãã§ã¯ãçæããã蚌ææžã¯2ã€ã®ã¿ã€ãã«åããããŸãïŒ ã¯ã©ã¹ã¿ãŒã¡ã³ããŒèšŒææž -ç°ãªããã·ã³äžã®mongodã€ã³ã¹ã¿ã³ã¹ã®å éšèªèšŒãç®çãšããç¹å®ã®ãµãŒããŒã«ãã€ã³ããã ã¯ã©ã€ã¢ã³ã蚌ææž -ã¯ã©ã¹ã¿ãŒå€éšã¯ã©ã€ã¢ã³ãã®èªèšŒãç®çãšããå¥ã®ãŠãŒã¶ãŒã«ãã€ã³ãããŸãã
x.509æ¡ä»¶ãæºããããã«ãåäžã®ããŒãããããã èªèšŒå± ã èªèšŒå±ïŒCAïŒãå¿ èŠã§ãã ããã«åºã¥ããŠãã¯ã©ã€ã¢ã³ã蚌ææžãšã¯ã©ã¹ã¿ãŒã¡ã³ããŒèšŒææžã®äž¡æ¹ãçºè¡ãããããããŸããCAã®ç§å¯ããŒãäœæããŸãã 次ã®ãã¹ãŠã®ã¢ã¯ã·ã§ã³ãæ£ããå®è¡ããç§å¯éµãå¥ã®ãã·ã³ã«ä¿åããŸããããã®èšäºã§ã¯æåã®ãµãŒããŒïŒserver1.cluster.comïŒã§ãã¹ãŠã®ã¢ã¯ã·ã§ã³ãå®è¡ããŸãã
server1.cluster.com:~/mongodb/keys# openssl genrsa -out mongodb-private.key -aes256 Generating RSA private key, 2048 bit long modulus .....................+++ ........................................................+++ e is 65537 (0x10001) Enter pass phrase for mongodb-private.key: Verifying - Enter pass phrase for mongodb-private.key:
ç§å¯ã®ãã¬ãŒãºãå°å ¥ããææ¡ã§ã¯ã ãtemporis $ filia $ veritasããªã©ã®ä¿¡é Œã§ããçµã¿åãããå ¥åããŠç¢ºèªããŸãïŒãã¡ãããå¥ã®è€éãªãã®ããããŸãïŒã ãã®ãã¬ãŒãºã¯èŠããŠããå¿ èŠããããŸããæ°ãã蚌ææžã«çœ²åããããã«å¿ èŠã«ãªããŸãã
次ã«ãCA蚌ææžãäœæããŸãïŒã³ãã³ããèµ·åããçŽåŸã«ãæå®ããããŒïŒãkeyããã©ã¡ãŒã¿ãŒã§ïŒããã·ãŒã¯ã¬ãããã¬ãŒãºãå ¥åããããæ±ããããŸãïŒïŒ
server1.cluster.com:~/mongodb/keys# openssl req -x509 -new -extensions v3_ca -key mongodb-private.key -days 36500 -out mongodb-CA-cert.crt
daysãã©ã¡ãŒã¿ã«æ³šæãä¿ããŸã-ããã¯èšŒææžã®æéã«è²¬ä»»ããããŸãã ç§ãçŸåšåãçµãã§ãããããžã§ã¯ãã«èª°ãã©ã®ãããåå ãããããããŸãããäžå¿«ãªé©ãããªããããã«ã36,500æ¥éã®äººçã®èšŒææžã瀺ããŸããããã¯100幎ã«çžåœããŸãïŒéåžžã«æ¥œèŠ³çã§ããïŒã
ãã¬ãŒãºã確èªããåŸã蚌ææžãææããçµç¹ã«é¢ããæ å ±ãå ¥åããããæ±ããããŸãã ç§ãã¡ã®å€§ããªçµç¹ããSomeSysyemsããšåŒã°ããã¢ã¹ã¯ã¯ã«ãããšæ³åããŠãã ããïŒå ¥åãããæ å ±ã¯ã³ãã³ã®åŸã«ç¶ããŸãïŒã
Country Name (2 letter code) [AU]: RU State or Province Name (full name) [Some-State]: MoscowRegion Locality Name (eg, city) []: Moscow Organization Name (eg, company) [Internet Widgits Pty Ltd]: SomeSystems Organizational Unit Name (eg, section) []: Statistics Common Name (eg server FQDN or YOUR name) []: CaServer Email Address []: info@SomeSystems.com
ãããïŒ CAã®æºåãæŽããããã䜿çšããŠãã¯ã©ã€ã¢ã³ã蚌ææžãšã¯ã©ã¹ã¿ãŒã¡ã³ããŒã®èšŒææžã«çœ²åã§ããŸãã å ¥åããããŒã¿ã®æå¹æ§ã¯CA蚌ææžèªäœã®æ©èœã«ã¯åœ±é¿ããŸãããã眲åããã蚌ææžã¯å ¥åãããå€ã«äŸåããããã«ãªããŸããããã«ã€ããŠã¯åŸã§èª¬æããŸãã
ã¯ã©ã¹ã¿ã¡ã³ããŒã®èšŒææžãäœæããæé ïŒå€éšã¯ã©ã€ã¢ã³ãã®èšŒææžã¯åå¥ã«æ€èšãããŸãïŒã¯æ¬¡ã®ãšããã§ãã
- ç§å¯éµïŒ* .key-ãã¡ã€ã«ïŒãšã蚌ææžèŠæ±ãïŒcsrãã¡ã€ã«ïŒãçæããŸãã CSRïŒèšŒææžçœ²åèŠæ±ïŒã¯ã蚌ææžãšå
¬éããŒãçºè¡ããçµç¹ã«é¢ãããšã³ã³ãŒããããæ
å ±ãå«ãããã¹ããã¡ã€ã«ã§ãã
- èªèšŒå±ã®ç§å¯éµãšå
¬é蚌ææžã䜿çšããŠãçŸåšã®ãµãŒããŒã®èšŒææžã«çœ²åããŸãã
- æ°ããããŒãšã¯ã©ã¹ã¿ãŒã¡ã³ããŒã®èšŒææžãããã¯ã©ã¹ã¿ãŒãžã®æ¥ç¶ã«äœ¿çšããPEMãã¡ã€ã«ãäœæããŸãã
æåã®ãµãŒããŒïŒserver1.cluster.comïŒã®ç§å¯ããŒãšèšŒææžèŠæ±ãäœæããŸãã CNïŒCommon NameïŒãé€ãããã¹ãŠã®ãã£ãŒã«ãã«ã«ãŒã蚌ææžã®å ŽåãšåããŸãŸã§ããå ŽåãéèŠãªè©³çŽ°ã«æ³šæãæããŸãã 蚌ææžããšã«äžæã«ããå¿ èŠããããŸãã ãã®å Žåãç¹å®ã®ãµãŒããŒã®å®å šä¿®é£Ÿãã¡ã€ã³å-FQDNïŒå®å šä¿®é£Ÿãã¡ã€ã³åïŒãå€ãšããŠç€ºãããŸãã
server1.cluster.com:~/mongodb/keys# openssl req -new -nodes -newkey rsa:2048 -keyout server1.key -out server1.csr Country Name (2 letter code) [AU]: RU State or Province Name (full name) [Some-State]: MoscowRegion Locality Name (eg, city) []: Moscow Organization Name (eg, company) [Internet Widgits Pty Ltd]: SomeSystems Organizational Unit Name (eg, section) []: Statistics Common Name (eg server FQDN or YOUR name) []: server1.cluster.com Email Address []: info@SomeSystems.com Please enter the following 'extra' attributes to be sent with your certificate request A challenge password []: An optional company name []:
空ã®ãŸãŸã«ããäœåãªãã£ãŒã«ãã è¿œå ã®ãã¹ã¯ãŒãïŒãã£ã¬ã³ãžãã¹ã¯ãŒã[] :)ãæå®ããå Žåãmongodæ§æã§ã¯ããã©ã¡ãŒã¿ãŒnet.ssl.PEMKeyPasswordããã³net.ssl.clusterPasswordãæ åœãããã®èšŒææžã®ãã¹ã¯ãŒããæå®ããå¿ èŠããããŸãã ïŒãããã®ãã©ã¡ãŒã¿ã®è©³çŽ°ã¯ã ãã¡ãã®ããã¥ã¡ã³ãã«ãããŸã ïŒã
次ã«ãCA蚌ææžã§CSRãã¡ã€ã«ã«çœ²åããå ¬é蚌ææžïŒ* .crtãã¡ã€ã«ïŒãååŸããŸãã
server1.cluster.com:~/mongodb/keys# openssl x509 -CA mongodb-CA-cert.crt -CAkey mongodb-private.key -CAcreateserial -req -days 36500 -in server1.csr -out server1.crt Signature ok subject=/C=RU/ST=MoscowRegion/L=Moscow/O=SomeSystems/OU=Statistics/CN=server1.cluster.com/emailAddress=info@SomeSystems.com Getting CA Private Key Enter pass phrase for mongodb-private.key:
次ã«ãPEMãã¡ã€ã«ãäœæããå¿ èŠããããŸãã
server1.cluster.com:~/mongodb/keys# cat server1.key server1.crt > server1.pem
mongodã€ã³ã¹ã¿ã³ã¹ãèµ·åãããšãã«PEMãã¡ã€ã«ãçŽæ¥äœ¿çšããæ§æã§æå®ããŸãã
次ã«ãæäœãç¹°ãè¿ããŠæ®ãã®ãµãŒããŒã®èšŒææžãäœæããå¿ èŠããããŸãã å®å šã«ç解ããããã«ããã¹ãŠã®ã³ãã³ããåŒçšããŸãã
server1.cluster.com:~/mongodb/keys# openssl req -new -nodes -newkey rsa:2048 -keyout server2.key -out server2.csr Country Name (2 letter code) [AU]: RU State or Province Name (full name) [Some-State]: MoscowRegion Locality Name (eg, city) []: Moscow Organization Name (eg, company) [Internet Widgits Pty Ltd]: SomeSystems Organizational Unit Name (eg, section) []: Statistics Common Name (eg server FQDN or YOUR name) []: server2.cluster.com Email Address []: info@SomeSystems.com Please enter the following 'extra' attributes to be sent with your certificate request A challenge password []: An optional company name []:
ïŒè¿œå ãã£ãŒã«ãã¯å ¥åãããŸããã§ããïŒ
CA蚌ææžã§CSRãã¡ã€ã«ã«çœ²åããŠã2çªç®ã®ãµãŒããŒã®ãããªãã¯èšŒææžïŒ* .crtãã¡ã€ã«ïŒãååŸããŸãã
server1.cluster.com:~/mongodb/keys# openssl x509 -CA mongodb-CA-cert.crt -CAkey mongodb-private.key -CAcreateserial -req -days 36500 -in server2.csr -out server2.crt Signature ok subject=/C=RU/ST=MoscowRegion/L=Moscow/O=SomeSystems/OU=Statistics/CN=server2.cluster.com/emailAddress=info@SomeSystems.com Getting CA Private Key Enter pass phrase for mongodb-private.key:
次ã«ãPEMãã¡ã€ã«ãäœæããå¿ èŠããããŸãã
server1.cluster.com:~/mongodb/keys# cat server2.key server2.crt > server2.pem
3çªç®ã®ãµãŒããŒèšŒææžã«ã€ããŠãåæ§ã§ãã
server1.cluster.com:~/mongodb/keys# openssl req -new -nodes -newkey rsa:2048 -keyout server3.key -out server3.csr Country Name (2 letter code) [AU]: RU State or Province Name (full name) [Some-State]: MoscowRegion Locality Name (eg, city) []: Moscow Organization Name (eg, company) [Internet Widgits Pty Ltd]: SomeSystems Organizational Unit Name (eg, section) []: Statistics Common Name (eg server FQDN or YOUR name) []: server3.cluster.com Email Address []: info@SomeSystems.com Please enter the following 'extra' attributes to be sent with your certificate request A challenge password []: An optional company name []:
ïŒè¿œå ãã£ãŒã«ãã¯å ¥åãããŸããã§ããïŒ
3çªç®ã®ãµãŒããŒã®å ¬é蚌ææžïŒ* .crtãã¡ã€ã«ïŒãååŸããã«ã¯ãCA蚌ææžã§CSRãã¡ã€ã«ã«çœ²åããŸãã
server1.cluster.com:~/mongodb/keys# openssl x509 -CA mongodb-CA-cert.crt -CAkey mongodb-private.key -CAcreateserial -req -days 36500 -in server3.csr -out server3.crt Signature ok subject=/C=RU/ST=MoscowRegion/L=Moscow/O=SomeSystems/OU=Statistics/CN=server3.cluster.com/emailAddress=info@SomeSystems.com Getting CA Private Key Enter pass phrase for mongodb-private.key:
PEMãã¡ã€ã«ãäœæããŸãã
server1.cluster.com:~/mongodb/keys# cat server3.key server3.crt > server3.pem
ãã¹ãŠã®ããŒãšèšŒææžãæåã®ãµãŒããŒã§äœæãããå¿ èŠã«å¿ããŠå¯Ÿå¿ãããµãŒããŒã«ç§»åãããããšãç¹°ãè¿ããŸãã ãããã£ãŠã3ã€ã®ãµãŒããŒã®ããããã«ã¯ããããªãã¯CA蚌ææžïŒmongodb-CA-cert.crtïŒãšãã®ãµãŒããŒã®PEMãã¡ã€ã«ïŒserver <$ N> .pemïŒãå¿ èŠã§ãã
Mongodã€ã³ã¹ã¿ã³ã¹ã®æ§æ
æ£ããèµ·åããã«ã¯ãããã€ãã®ãã©ã¡ãŒã¿ãŒãmongodã€ã³ã¹ã¿ã³ã¹ã«æž¡ãå¿ èŠããããŸãã ãããè¡ãã«ã¯ãæ§æãã¡ã€ã«ã䜿çšããããå¿ èŠãªãã¹ãŠã®å€ãåŒæ°ãšããŠç«¯æ«ã³ãã³ãã«æž¡ããŸãã ã»ãŒãã¹ãŠã®æ§æãã©ã¡ãŒã¿ãŒã¯ã察å¿ããã³ãã³ãã©ã€ã³åŒæ°ã«åæ ãããŸãã ç§ã®æèŠã§ã¯ãå¥ã®æ§é åãã¡ã€ã«ã®æ¹ãèªã¿ããããè£è¶³ãããããããæ§æãã¡ã€ã«ã®ãªãã·ã§ã³ã¯ããæ£åœåãããŸãããã®å Žåãããã°ã©ã ã®ã€ã³ã¹ã¿ã³ã¹ã®èµ·åã¯ãåäžã®åŒæ°-æ§æãã¡ã€ã«ã®å Žæãæž¡ãã ãã«ãªããŸãïŒ
mongod --config <path-to-config-file>
ãã®ãããæåã®ãµãŒããŒã§æåã®æçã¬ããªã«ïŒrs0ïŒã®mongodã€ã³ã¹ã¿ã³ã¹ã®æ§æãã¡ã€ã«ãäœæããŸãã
# # /root/mongodb/cfg/mongod-rs0.conf # replication: replSetName: "rs0" # net: port: 27000 ssl: mode: requireSSL # PEMKeyFile: /root/mongodb/keys/server1.pem clusterFile: /root/mongodb/keys/server1.pem CAFile: /root/mongodb/keys/mongodb-CA-cert.crt weakCertificateValidation: false # allowInvalidCertificates: false # security: authorization: enabled # clusterAuthMode: x509 # - MONGODB-X509 storage: dbPath : /root/mongodb/data/rs0 # systemLog: destination: file # path: /root/mongodb/logs/mongod-rs0.log # - logAppend: true # -
2çªç®ã®ã·ã£ãŒãã¬ããªã«ïŒrs1ïŒã«åæ§ã®ãã¡ã€ã«ãäœæããŸãããããŒããã¬ããªã«åãããŒã¿ãã£ã¬ã¯ããªã®å Žæãããã³ãã°ãã¡ã€ã«ãå€æŽããŸãã
# # /root/mongodb/cfg/mongod-rs1.conf # replication: replSetName: "rs1" net: port: 27001 ssl: mode: requireSSL PEMKeyFile: /root/mongodb/keys/server1.pem clusterFile: /root/mongodb/keys/server1.pem CAFile: /root/mongodb/keys/mongodb-CA-cert.crt weakCertificateValidation: false allowInvalidCertificates: false security: authorization: enabled clusterAuthMode: x509 storage: dbPath : /root/mongodb/data/rs1 systemLog: destination: file path: /root/mongodb/logs/mongod-rs1.log logAppend: true
ãããŠã3çªç®ã®ã¬ããªã«ïŒrs2ïŒã®äŸãã§ïŒ
# # /root/mongodb/cfg/mongod-rs2.conf # replication: replSetName: "rs2" net: port: 27002 ssl: mode: requireSSL PEMKeyFile: /root/mongodb/keys/server1.pem clusterFile: /root/mongodb/keys/server1.pem CAFile: /root/mongodb/keys/mongodb-CA-cert.crt weakCertificateValidation: false allowInvalidCertificates: false security: authorization: enabled clusterAuthMode: x509 storage: dbPath : /root/mongodb/data/rs2 systemLog: destination: file path: /root/mongodb/logs/mongod-rs2.log logAppend: true
3ã€ã®ã·ã£ãŒãã¬ããªã«ãç·šæããã€ã³ã¹ã¿ã³ã¹ã«å ããŠãã¯ã©ã¹ã¿ãŒã«ã¯ãã¬ããªã«ïŒrscfgïŒã«åºã¥ããŠæ§ç¯ãããæ§æãµãŒããŒã®æäœãæäŸããmongodãååšããŸãã
æ§æãµãŒããŒã®åœ¹å²ã¯1ã€ã®mongodã«ãã£ãŠïŒã·ã£ãŒããšåæ§ã«ïŒå®è¡ã§ããããšã説æãã䟡å€ããããŸãããä¿¡é Œæ§ãšãã©ãŒã«ããã¬ã©ã³ã¹ã確ä¿ããããã«ãæ§æãµãŒããŒãã¬ããªã«ã»ããã«åºã¥ããŠäœæããããšããå§ãããŸãã
ãµãŒãã¹ã¬ããªã«æ§æãã¡ã€ã«ã¯ãmongodã€ã³ã¹ã¿ã³ã¹ã«ãã®ç¹å¥ãªç®çãäŒãããsharding.clusterRoleããã©ã¡ãŒã¿ãŒã®ååšãããŒã¿ã¬ããªã«ãšç°ãªããŸãã
# # /root/mongodb/cfg/mongod-rscfg.conf # sharding: clusterRole: configsvr # - replication: replSetName: "rscfg" # net: port: 27888 ssl: mode: requireSSL PEMKeyFile: /root/mongodb/keys/server1.pem clusterFile: /root/mongodb/keys/server1.pem CAFile: /root/mongodb/keys/mongodb-CA-cert.crt weakCertificateValidation: false allowInvalidCertificates: false security: authorization: enabled clusterAuthMode: x509 storage: dbPath : /root/mongodb/data/config systemLog: destination: file path: /root/mongodb/logs/mongod-rscfg.log logAppend: true
次ã«ãäœæãããã¹ãŠã®æ§æãã¡ã€ã«ãä»ã®ãµãŒããŒã«ã³ããŒããå¿ èŠããããŸããã³ããŒåŸã察å¿ãããµãŒããŒã®èšŒææžãæå®ããå¿ èŠããããã©ã¡ãŒã¿ãŒnet.ssl.PEMKeyFileããã³net.ssl.clusterFileã®å€ãå€æŽããããšãå¿ããªãã§ãã ããïŒserver2.pemãserver3.pemïŒã
ã¬ããªã«ã»ããã®ã»ããã¢ãã
æåã®ãµãŒããŒã§ã¯ãããŒã27000ã§mongodãå®è¡ããŸãããæŠéãæ§æãã¡ã€ã«ãæå®ããã«ãããŒããšããŒã¿ãã£ã¬ã¯ããªã®ã¿ãæå®ããŸããããã¯ãèµ·åãããmongodã€ã³ã¹ã¿ã³ã¹ããŸã ã¬ããªã«ã®ã¡ã³ããŒã§ãããšã¯èŠãªãããæ§æãã¡ã€ã«ã§æå®ããæ¥ç¶ãšèªèšŒã®å³æ ŒãªèŠä»¶ã課ããªãããã«ããããã«è¡ãããŸãã
mongod --port 27000 --dbpath /root/mongodb/data/rs0
次ã«ãå®è¡äžã®mongodã«æ¥ç¶ããå°æ¥ã®ã¬ããªã«ã®ã¹ãŒããŒãŠãŒã¶ãŒãè¿œå ããå¿ èŠããããŸããããã«ãããå°æ¥ãæ§æãã¡ã€ã«ã§æå®ãããèš±å¯ãæå¹ã«ããåŸãåæåãå«ãã¬ããªã«ãå€æŽããæš©å©ãåŸãããŸããå®è·µã瀺ããŠããããã«ãx.509æ¿èªãå«ããŠããåŸæ¥ã®ãŠãŒã¶ãŒãããŒã¿ããŒã¹ã«è¿œå ããããšã¯çŠæ¢ãããŠããŸããïŒãã°ã€ã³ãšãã¹ã¯ãŒãã«ãã£ãŠèªèšŒãããŸãïŒãããã«ãããããããç§ã¯ãã®æ©äŒã«é Œãããã¯ã©ã¹ã¿ãŒã¬ãã«ãšã¬ããªã«åœ¢æäžã®äž¡æ¹ã§x.509ã¡ã«ããºã ã䜿çšããããšã«ããŸãããæ確ã«ããããã«ãçŸåšäœæããŠãããŠãŒã¶ãŒã¯ããã®ã¬ããªã«ã®ã¬ãã«ã®ãŠãŒã¶ãŒã§ãããšèšããŸããä»ã®ã¬ããªã«ãããããã³ã¯ã©ã¹ã¿ãŒã¬ãã«ã§ã¯ãå©çšã§ããŸããã
æ°ãããŠãŒã¶ãŒã«ã€ããŠã¯ããx.509èªèšŒãã»ã¯ã·ã§ã³ã§æ¢ã«è¡ã£ããã®ãšåæ§ã®å¥ã®èšŒææžãäœæããå¿ èŠããããŸãããã®èšŒææžã®éãã¯ãã¯ã©ã¹ã¿ã¡ã³ããŒïŒmongodã€ã³ã¹ã¿ã³ã¹ãŸãã¯ãµãŒããŒïŒã§ã¯ãªããã¢ã«ãŠã³ãã«é¢é£ä»ããããŠããããšã§ããã€ãŸããã¯ã©ã€ã¢ã³ã蚌ææžãäœæããŸãããã®èšŒææžã¯ãæåã®ã·ã£ãŒãïŒrs0ïŒã®ã»ããã®ã¹ãŒããŒãŠãŒã¶ãŒïŒã«ãŒãããŒã«ïŒã¬ããªã«ã«é¢é£ä»ããããŸããMongoDBã®çµã¿èŸŒã¿ããŒã«ã¯ãå ¬åŒããã¥ã¡ã³ãã®ãã®ã»ã¯ã·ã§ã³ã«ãããŸãã
CAãµãŒããŒã«ã¢ã¯ã»ã¹ããå¿ èŠããããŸãããããŠãå¥ã®ããŒãšèšŒææžçœ²åèŠæ±ãçæããŸãã
server1.cluster.com:~/mongodb/keys# openssl req -new -nodes -newkey rsa:2048 -keyout rsroot.key -out rsroot.csr Generating a 2048 bit RSA private key ........................................................................+++ .........................+++ writing new private key to 'rsroot.key' ----- You are about to be asked to enter information that will be incorporated into your certificate request. What you are about to enter is what is called a Distinguished Name or a DN. There are quite a few fields but you can leave some blank For some fields there will be a default value, If you enter '.', the field will be left blank. ----- Country Name (2 letter code) [AU]: RU State or Province Name (full name) [Some-State]: MoscowRegion Locality Name (eg, city) []: Moscow Organization Name (eg, company) [Internet Widgits Pty Ltd]: SomeSystems Organizational Unit Name (eg, section) []: StatisticsClient Common Name (eg server FQDN or YOUR name) []: rsroot Email Address []: Please enter the following 'extra' attributes to be sent with your certificate request A challenge password []: An optional company name []:
蚌ææžã«çœ²åããŸãïŒããã§ããCAããŒããã®ç§å¯ã®ãã¬ãŒãºãå¿ èŠã«ãªããŸãïŒã
server1.cluster.com:~/mongodb/keys# openssl x509 -CA mongodb-CA-cert.crt -CAkey mongodb-private.key -CAcreateserial -req -days 36500 -in rsroot.csr -out rsroot.crt Signature ok subject=/C=RU/ST=MoscowRegion/L=Moscow/O=SomeSystems/OU=StatisticsClient/CN=rsroot Getting CA Private Key Enter pass phrase for mongodb-private.key:
PEMãã¡ã€ã«ãäœæããŸãã
server1.cluster.com:~/mongodb/keys# cat rsroot.key rsroot.crt > rsroot.pem
ãã©ã¡ãŒã¿ãŒOrganization Unit NameïŒOUïŒã«æ³šæããŠãã ãããã€ãŸããã¯ã©ã€ã¢ã³ã蚌ææžãçæãããšãã¯ãã¯ã©ã¹ã¿ãŒã¡ã³ããŒã®èšŒææžãçæãããšãã«æå®ãããã®ãšã¯ç°ãªãå¿ èŠããããŸããããã§ãªãå Žåãã¯ã©ã¹ã¿ãŒã¡ã³ããŒã蚌ææžã«æã£ãŠãããã®ãšçããOUãæã€ãµããžã§ã¯ãïŒä»¥äžã§èª¬æïŒãå«ãã¯ã©ã¹ã¿ãŒã«ãŠãŒã¶ãŒãè¿œå ãããšãmongaã¯ãšã©ãŒã§æåŠããå ŽåããããŸãã
{ "ok" : 0, "errmsg" : "Cannot create an x.509 user with a subjectname that would be recognized as an internal cluster member.", "code" : 2 }
x.509ã¡ã«ããºã ã«ããæ¿èªã®ããã®ãŠãŒã¶ãŒã¯ããå€ãã£ãæ¹æ³ã§è¿œå ãããŸããååãšãã¹ã¯ãŒãã§ã¯ãªãããŠãŒã¶ãŒã«å¯Ÿå¿ãã蚌ææžã®èå¥åïŒãµããžã§ã¯ãïŒãæå®ããå¿ èŠããããŸãã次ã®ã³ãã³ããå®è¡ããŠãPEMãã¡ã€ã«ãã件åãååŸã§ããŸãã
server1.cluster.com:~/mongodb/keys# openssl x509 -in rsroot.pem -inform PEM -subject -nameopt RFC2253 subject= CN=rsroot,OU=StatisticsClient,O=SomeSystems,L=Moscow,ST=MoscowRegion,C=RU -----BEGIN CERTIFICATE-----
åºåã§ã¯ããsubject =ãã§å§ãŸãè¡ã®å 容ã«é¢å¿ããããŸãïŒãsubject =ãèªäœãšã¹ããŒã¹ãªãïŒãmongodã«æ¥ç¶ãããŠãŒã¶ãŒãè¿œå ããŸãã
mongo --port 27000
> db.getSiblingDB("$external").runCommand({createUser: "CN=rsroot,OU=StatisticsClient,O=SomeSystems,L=Moscow,ST=MoscowRegion,C=RU", roles: [{role: "root", db: "admin"}] })
$ externalã¯ãè³æ Œæ å ±ãMongoDBã®å€éšã«æ ŒçŽãããŠãããŠãŒã¶ãŒã®äœæã«äœ¿çšãããä»®æ³ããŒã¿ããŒã¹ã®ååã§ããããšãã°ããã®å Žåã®ããã«ïŒèªèšŒã«ã¯èšŒææžãã¡ã€ã«ã䜿çšãããŸãïŒã
次ã«ãé©åãªæ§æãã¡ã€ã«ã䜿çšããŠãmongaã·ã§ã«ãçµäºããmongodãåèµ·åããŸããåãããšã2çªç®ãš3çªç®ã®ãµãŒããŒã§è¡ãå¿ èŠããããŸãããããã£ãŠãæåã®ã¬ããªã«ïŒrs0ïŒã®ãã¹ãŠã®Mongodesãéå§ããå¿ èŠããããŸãã
ã¬ããªã«ã®äœæãããã¹ãŒããŒãŠãŒã¶ãŒïŒrsrootïŒã®èšŒææžã䜿çšããŠmongodã«æ¥ç¶ãã蚌ææžã®ãµããžã§ã¯ãããŠãŒã¶ãŒåãšããŠæå®ããŠèªèšŒãæž¡ããŸãã
server1.cluster.com:~/mongodb/keys# mongo admin --ssl --sslCAFile /root/mongodb/keys/mongodb-CA-cert.crt --sslPEMKeyFile /root/mongodb/keys/rsroot.pem --host server1.cluster.com --port 27000
> db.getSiblingDB("$external").auth({ mechanism:"MONGODB-X509", user: "CN=rsroot,OU=StatisticsClient,O=SomeSystems,L=Moscow,ST=MoscowRegion,C=RU" })
ã¬ããªã«ãåæåããŸãã
rs.initiate( { _id: "rs0", members: [ { _id: 0, host : "server1.cluster.com:27000" }, { _id: 1, host : "server2.cluster.com:27000" }, { _id: 2, host : "server3.cluster.com:27000", arbiterOnly: true }, ] } )
3çªç®ã®ãµãŒããŒã®arbiterOnlyãã©ã¡ãŒã¿ãŒã«æ³šæããŠãã ãããæåã®æ®µéã§ãã調åãµãŒããŒããäœæããããšã«åæããŸããã
mongodã«åæ¥ç¶ãããšãã·ã§ã«ã®ãã¬ãã£ãã¯ã¹ãrs0ãã«ãã£ãŠãåãååã®ã¬ããªã«ã«å±ããŠããããšãããããŸã
ãrs0ïŒPRIMARYïŒçŸåšã®ãµãŒããŒã¯SECONDARYãéžæã§ããŸãïŒã
åæ§ã®ãã¿ãŒã³ã§ãããã«2ã€ã®ããŒã¿ã¬ããªã«ããªã³ã¯ããå¿ èŠããããŸãã
1.æåã®ãµãŒããŒã§ãèšå®ãªãã§Mongodesãå®è¡ããŸãïŒããŒããšããŒã¿ãã£ã¬ã¯ããªãå€æŽãããŠããŸãïŒã
mongod --port 27001 --dbpath /root/mongodb/data/rs1
2.å®è¡äžã®mongodã«æ¥ç¶ããã¬ããªã«ã¹ãŒããŒãŠãŒã¶ãŒïŒrs1ïŒãè¿œå ããŸãããã¹ãŠã®ã¬ããªã«ã«åã蚌ææžã䜿çšããããããµããžã§ã¯ãã¯æåã®ã¬ããªã«ãšåãããã«äœ¿çšãããŸãã
mongo --port 27001
> db.getSiblingDB("$external").runCommand({createUser: "CN=rsroot,OU=StatisticsClient,O=SomeSystems,L=Moscow,ST=MoscowRegion,C=RU", roles: [{role: "root", db: "admin"}] })
3.æ§æãã¡ã€ã«ãæå®ããŠãæåã®ãµãŒããŒã§mongodãåèµ·åããŸãã2çªç®ãš3çªç®ã®ãµãŒããŒã§ã¯ã察å¿ããæ§æã䜿çšããŠMongodãäžããŸãã
root@server1.cluster.com# mongod --config /root/mongodb/cfg/mongod-rs1.conf root@server2.cluster.com# mongod --config /root/mongodb/cfg/mongod-rs1.conf root@server3.cluster.com# mongod --config /root/mongodb/cfg/mongod-rs1.conf
4.蚌ææžã䜿çšããŠMongodã«æ¥ç¶ããèªèšŒãæž¡ããrs1ã¬ããªã«ãåæåããŸãã
root@server1.cluster.com# mongo admin --ssl --sslCAFile /root/mongodb/keys/mongodb-CA-cert.crt --sslPEMKeyFile /root/mongodb/keys/rsroot.pem --host server1.cluster.com --port 27001
> db.getSiblingDB("$external").auth({ mechanism:"MONGODB-X509", user: "CN=rsroot,OU=StatisticsClient,O=SomeSystems,L=Moscow,ST=MoscowRegion,C=RU" }) > rs.initiate( { _id: "rs1", members: [ { _id: 0, host : "server1.cluster.com:27001" }, { _id: 1, host : "server2.cluster.com:27001" }, { _id: 2, host : "server3.cluster.com:27001", arbiterOnly: true }, ] } )
3çªç®ã®ã¬ããªã«ïŒrs2ïŒã«å¯ŸããŠæé ãç¹°ãè¿ããŸãã
1.æåã®ãµãŒããŒã§èšå®ãªãã§Mongodesãå®è¡ããŸãïŒããŒããšããŒã¿ãã£ã¬ã¯ããªã®å€æŽãå¿ããªãã§ãã ããïŒã
mongod --port 27002 --dbpath /root/mongodb/data/rs2
2. Mongodã«æ¥ç¶ããã¬ããªã«ã¹ãŒããŒãŠãŒã¶ãŒïŒrs2ïŒãè¿œå ããŸãã
mongo --port 27002
> db.getSiblingDB("$external").runCommand({createUser: "CN=rsroot,OU=StatisticsClient,O=SomeSystems,L=Moscow,ST=MoscowRegion,C=RU", roles: [{role: "root", db: "admin"}] })
3.æ§æãã¡ã€ã«ã䜿çšããŠãæåã®ãµãŒããŒã§Mongodãåèµ·åããŸãã2çªç®ãš3çªç®ã®ãµãŒããŒã§ã察å¿ããæ§æã䜿çšããŠMongodãäžããŸãã
root@server1.cluster.com# mongod --config /root/mongodb/cfg/mongod-rs2.conf root@server2.cluster.com# mongod --config /root/mongodb/cfg/mongod-rs2.conf root@server3.cluster.com# mongod --config /root/mongodb/cfg/mongod-rs2.conf
4.蚌ææžã䜿çšããŠMongodã«æ¥ç¶ããèªèšŒãæž¡ããrs2ã¬ããªã«ãåæåããŸãã
root@server1.cluster.com# mongo admin --ssl --sslCAFile /root/mongodb/keys/mongodb-CA-cert.crt --sslPEMKeyFile /root/mongodb/keys/rsroot.pem --host server1.cluster.com --port 27002
> db.getSiblingDB("$external").auth({ mechanism:"MONGODB-X509", user: "CN=rsroot,OU=StatisticsClient,O=SomeSystems,L=Moscow,ST=MoscowRegion,C=RU" }) > rs.initiate( { _id: "rs2", members: [ { _id: 0, host : "server1.cluster.com:27002" }, { _id: 1, host : "server2.cluster.com:27002" }, { _id: 2, host : "server3.cluster.com:27002", arbiterOnly: true }, ] } )
æ§æãµãŒããŒ
æ§æãµãŒããŒã®æ§æã¬ããªã«ã»ããã匷調衚瀺ããããšã«ããŸãããããã«ã¯ãããã€ãã®è¿œå æé ãå¿ èŠãªæ©èœãããã€ãããããã§ãããŸããã¬ããªã«ãæ§æã«è¿œå ãããã¹ãŠã®ãŠãŒã¶ãŒãMongosã䜿çšããŠã¯ã©ã¹ã¿ãŒã¬ãã«ã§å©çšã§ããããã«ãªããããåå¥ã®èšŒææžã«é¢é£ä»ããããåå¥ã®ãŠãŒã¶ãŒãäœæããŸãã第äºã«ãmongaã§ã¯ãæ§æã¬ããªã«ã®äžéšãšããŠã¢ãŒããã¬ãŒã¿ãŒãäœæã§ããŸããããããè¡ãããšãããšããšã©ãŒã¡ãã»ãŒãžã衚瀺ãããŸãã
{ "ok" : 0, "errmsg" : "Arbiters are not allowed in replica set configurations being used for config servers", "code" : 93 }
ãã®ãããæ§æã¬ããªã«ã«ã¯2ã€ã®SECONDARYã€ã³ã¹ã¿ã³ã¹/ mongodsããããŸããrscfgã¬ããªã«ã®ã¹ãŒããŒãŠãŒã¶ãŒçšã«å¥ã®èšŒææžãäœæããŠã¿ãŸããããããã¯ãå ã»ã©è¿°ã¹ãããã«ãã¯ã©ã¹ã¿ãŒã¬ãã«ã®ã«ãŒãã«ããªããŸãã
server1.cluster.com:~/mongodb/keys# openssl req -new -nodes -newkey rsa:2048 -keyout rootuser.key -out rootuser.csr Generating a 2048 bit RSA private key ......................+++ .........................................+++ writing new private key to 'rootuser.key' ----- You are about to be asked to enter information that will be incorporated into your certificate request. What you are about to enter is what is called a Distinguished Name or a DN. There are quite a few fields but you can leave some blank For some fields there will be a default value, If you enter '.', the field will be left blank. ----- Country Name (2 letter code) [AU]: RU State or Province Name (full name) [Some-State]: MoscowRegion Locality Name (eg, city) []: Moscow Organization Name (eg, company) [Internet Widgits Pty Ltd]: SomeSystems Organizational Unit Name (eg, section) []: StatisticsClient Common Name (eg server FQDN or YOUR name) []: root Email Address []: Please enter the following 'extra' attributes to be sent with your certificate request A challenge password []: An optional company name []:
server1.cluster.com:~/mongodb/keys# openssl x509 -CA mongodb-CA-cert.crt -CAkey mongodb-private.key -CAcreateserial -req -days 36500 -in rootuser.csr -out rootuser.crt Signature ok subject=/C=RU/ST=MoscowRegion/L=Moscow/O=SomeSystems/OU=StatisticsClient/CN=root Getting CA Private Key Enter pass phrase for mongodb-private.key:
server1.cluster.com:~/mongodb/keys# cat rootuser.key rootuser.crt > rootuser.pem
server1.cluster.com:~/mongodb/keys# openssl x509 -in rootuser.pem -inform PEM -subject -nameopt RFC2253 subject= CN=root,OU=StatisticsClient,O=SomeSystems,L=Moscow,ST=MoscowRegion,C=RU -----BEGIN CERTIFICATE-----
1.æåã®ãµãŒããŒã§èšå®ãªãã§mongodãèµ·åããŸãã
server1.cluster.com:~/mongodb/keys# mongod --port 27888 --dbpath /root/mongodb/data/config
2. Mongodã«æ¥ç¶ããã¬ããªã«ã¹ãŒããŒãŠãŒã¶ãŒïŒrscfgïŒãè¿œå ããŸãã
server1.cluster.com:~/mongodb/keys# mongo --port 27888
> db.getSiblingDB("$external").runCommand({createUser: "CN=root,OU=StatisticsClient,O=SomeSystems,L=Moscow,ST=MoscowRegion,C=RU", roles: [{role: "root", db: "admin"}] })
3.èšå®ãã¡ã€ã«ã䜿çšããŠãæåã®ãµãŒããŒã§mongodãåèµ·åããŸãã2çªç®ãš3çªç®ã®ãµãŒããŒã§ã察å¿ããæ§æãã¡ã€ã«ã䜿çšããŠMongodãäžããŸãã
root@server1.cluster.com# mongod --config /root/mongodb/cfg/mongod-rscfg.conf root@server2.cluster.com# mongod --config /root/mongodb/cfg/mongod-rscfg.conf root@server3.cluster.com# mongod --config /root/mongodb/cfg/mongod-rscfg.conf
4.蚌ææžã䜿çšããŠMongodã«æ¥ç¶ããèªèšŒãè¡ããæ§æã¬ããªã«ïŒrscfgïŒãåæåããŸãã
root@server1.cluster.com# mongo admin --ssl --sslCAFile /root/mongodb/keys/mongodb-CA-cert.crt --sslPEMKeyFile /root/mongodb/keys/rootuser.pem --host server1.cluster.com --port 27888
> db.getSiblingDB("$external").auth({ mechanism:"MONGODB-X509", user: "CN=root,OU=StatisticsClient,O=SomeSystems,L=Moscow,ST=MoscowRegion,C=RU" }) > rs.initiate( { _id: "rscfg", members: [ { _id: 0, host : "server1.cluster.com:27888" }, { _id: 1, host : "server2.cluster.com:27888" }, { _id: 2, host : "server3.cluster.com:27888" } ] } )
æ§æãµãŒããŒããŒã¹ã®ã¬ããªã«ã»ããã®æºåãã§ããŸãããããã§ãmongosãèµ·åããŠã¯ã©ã¹ã¿ãŒã«æ¥ç¶ã§ããŸãã
mongosã®æ§æãšéå§
Mongosã®ç®æšã¯ãã¯ã©ã¹ã¿ãŒããŒã¿ãžã®ã¢ã¯ã»ã¹ãã€ã³ããæäŸããããšã§ãïŒããã«ãã¯ã©ã€ã¢ã³ãã¯mongosãä»ããŠã®ã¿ã¯ã©ã¹ã¿ãŒããŒã¿ã«ã¢ã¯ã»ã¹ã§ããŸãïŒã MongoDBããã¥ã¡ã³ãã®å³ã§ã¯ãã¢ããªã±ãŒã·ã§ã³ãµãŒããŒäžã§å®è¡ãããŠããMongosãæãããŠããŸããç§ãè¡šãã¯ã©ã¹ã¿ãŒæ§é ã§ã¯ãserver1.cluster.comãšserver2.cluster.comã§çŽæ¥å®è¡ãããŠãã2ã€ã®mongosã€ã³ã¹ã¿ã³ã¹ããããŸãã
ãŸããmongodã«ã€ããŠã¯ãèµ·åæã«Mongosã«è»¢éããæ§æãã¡ã€ã«ãäœæããŸãã
mongosèšå®ãšmongodã®äž»ãªéãã¯ãMongosã«ã¯ããŒã¿ãã£ã¬ã¯ããªããªãããããã·ããŒã¿ã®ã¿ãä¿åãããããã§ããMongosã¯ãæ§æãµãŒããŒã®æ§æã³ã¬ã¯ã·ã§ã³ããã¯ã©ã¹ã¿ãŒã®æ§æãšç¶æ ã«é¢ããå¿ èŠãªæ å ±ããã¹ãŠååŸããŸããMongosã¯ãsharding.configDBãã©ã¡ãŒã¿ãŒã䜿çšããŠæ§æãµãŒããŒã«æ¥ç¶ããæ¹æ³ã«ã€ããŠåŠç¿ããŸããæ§æãµãŒããŒã¯ã»ããã®ã¬ããªã«ã«åºã¥ããŠãããããã¬ããªã«åœ¢åŒã§æå®ããŸãïŒã¬ããªã«èªäœã®ååãã¹ã©ãã·ã¥ãããŒããå«ããã¹ãã®ãªã¹ããã³ã³ãã§åºåã£ãŠæå®ããŸããMongoãããã©ã«ãããŒãã®Mongi-27017ã§èµ·åããŸãã
# # /root/mongodb/cfg/mongos.conf # sharding: configDB: "rscfg/server1.cluster.com:27888,server2.cluster.com:27888,server3.cluster.com:27888" net: port: 27017 ssl: mode: requireSSL PEMKeyFile: /root/mongodb/keys/server1.pem clusterFile: /root/mongodb/keys/server1.pem CAFile: /root/mongodb/keys/mongodb-CA-cert.crt weakCertificateValidation: false allowInvalidCertificates: false security: clusterAuthMode: x509 systemLog: destination: file path: /root/mongodb/logs/mongos.log logAppend: true
æ§æãã¡ã€ã«ãäž¡æ¹ã®ãµãŒããŒã«ã³ããŒãïŒå¯Ÿå¿ããPEM蚌ææžãæå®ïŒãã³ãã³ãã§å®è¡ããŸãã
mongos --config /root/mongodb/cfg/mongos.conf
ã¢ã¯ã·ã§ã³ã®æ£ããã確èªããŸã-mongosã«æ¥ç¶ããæ§æã¬ããªã«ã«è¿œå ããã«ãŒããŠãŒã¶ãŒèšŒææžã§èªèšŒããŸãïŒã¬ããªã«ã¬ããªã«ã®ãŠãŒã¶ãŒã¯ã¯ã©ã¹ã¿ãŒãŠãŒã¶ãŒã§ããããšãæãåºããŠãã ããïŒã
mongo admin --ssl --sslCAFile /root/mongodb/keys/mongodb-CA-cert.crt --sslPEMKeyFile /root/mongodb/keys/rootuser.pem --host server1.cluster.com --port 27017
ãmongos>ããšããç¢æã«ãã£ãŠã誰ã«æ¥ç¶ããŠããããããããŸãã
mongos> db.getSiblingDB("$external").auth({ mechanism:"MONGODB-X509", user: "CN=root,OU=StatisticsClient,O=SomeSystems,L=Moscow,ST=MoscowRegion,C=RU" })
ïŒåºåã«è¯å®çãªã1ãã衚瀺ãããããšãäºæ³ãããŸãïŒ
äžè¬ã«ãmongaã¯ã«ãŒãããæ¥ç¶ãããšãæ°ã«å ¥ããªãããããã»ãã¥ãªãã£äžã®çç±ã§ãããè¡ãã¹ãã§ã¯ãªãããšãéç¥ããŸãããããã£ãŠãå®éã®ã¯ã©ã¹ã¿ãŒã§äœæ¥ããå Žåã¯ãçµã¿èŸŒã¿ããŒã«userAdminAnyDatabaseãä»äžããããŠãŒã¶ãŒïŒåœç¶ãåå¥ã®èšŒææžãæã€ãŠãŒã¶ãŒïŒãè¿œå ããããšããå§ãããŸãããã®åœ¹å²ã«ã¯ã管çã¿ã¹ã¯ãå®è¡ããããã«å¿ èŠãªã»ãŒãã¹ãŠã®æš©éããããŸãã
ããã§ã¯ãå¥ã®ãŠãŒã¶ãŒã®èšŒææžãäœæããäŸãæãã䟡å€ããããšæããŸãããã®ãŠãŒã¶ãŒã¯åæããŒã¿ããŒã¹ã«ã®ã¿ã¢ã¯ã»ã¹ã§ãããŠãŒã¶ãŒã«ä»£ãã£ãŠãµãŒãã¹ã®ãã¹ãŠã®ã¢ããªã±ãŒã·ã§ã³ãã¯ã©ã¹ã¿ãŒã«æ¥ç¶ãããŸãã
ãã®ããã蚌ææ©é¢ã®ãããã£ã¬ã¯ããªã«ç§»åããæ°ãããŠãŒã¶ãŒã®ããŒãšèšŒææžçœ²åèŠæ±ãäœæããŸãããããanalyticsuserãšåŒã³ãŸãã
server1.cluster.com:~/mongodb/keys# openssl req -new -nodes -newkey rsa:2048 -keyout analyticsuser.key -out analyticsuser.csr Generating a 2048 bit RSA private key ......................+++ .........................................+++ writing new private key to 'analyticsuser.key' ----- You are about to be asked to enter information that will be incorporated into your certificate request. What you are about to enter is what is called a Distinguished Name or a DN. There are quite a few fields but you can leave some blank For some fields there will be a default value, If you enter '.', the field will be left blank. ----- Country Name (2 letter code) [AU]: RU State or Province Name (full name) [Some-State]: MoscowRegion Locality Name (eg, city) []: Moscow Organization Name (eg, company) [Internet Widgits Pty Ltd]: SomeSystems Organizational Unit Name (eg, section) []: StatisticsClient Common Name (eg server FQDN or YOUR name) []: analyticsuser Email Address []: Please enter the following 'extra' attributes to be sent with your certificate request A challenge password []: An optional company name []:
蚌ææžã«çœ²åããŸãã
server1.cluster.com:~/mongodb/keys# openssl x509 -CA mongodb-CA-cert.crt -CAkey mongodb-private.key -CAcreateserial -req -days 36500 -in analyticsuser.csr -out analyticsuser.crt Signature ok subject=/C=RU/ST=MoscowRegion/L=Moscow/O=SomeSystems/OU=StatisticsClient/CN=analyticsuser Getting CA Private Key Enter pass phrase for mongodb-private.key:
PEMãã¡ã€ã«ãäœæããŸãã
server1.cluster.com:~/mongodb/keys# cat analyticsuser.key analyticsuser.crt > analyticsuser.pem
ã©ã®ãµããžã§ã¯ãã蚌ææžãæã£ãŠãããèŠãŠã¿ãŸãããïŒ
server1.cluster.com:~/mongodb/keys# openssl x509 -in rootuser.pem -inform PEM -subject -nameopt RFC2253 subject= CN=analyticsuser,OU=StatisticsClient,O=SomeSystems,L=Moscow,ST=MoscowRegion,C=RU -----BEGIN CERTIFICATE-----
管çè æš©éãæã€ãŠãŒã¶ãŒã®ä»£ããã«ã¯ã©ã¹ã¿ãŒïŒMongosïŒã«æ¥ç¶ããæ°ãããŠãŒã¶ãŒãè¿œå ããŸãã
mongos> db.getSiblingDB("$external").runCommand({createUser: "CN=analyticsuser,OU=StatisticsClient,O=SomeSystems,L=Moscow,ST=MoscowRegion,C=RU", roles: [{role: "readWrite", db: "analytics"}] })
ãŠãŒã¶ãŒanalyticsuserã«ã¯ã1ã€ã®åæããŒã¿ããŒã¹ã«å¯ŸããŠã®ã¿èªã¿åãããã³æžã蟌ã¿æš©éãä»äžããããšã«æ³šæããŠãã ãããããã«ãããå€éšã¢ããªã±ãŒã·ã§ã³ããåæããŒã¿ããŒã¹èªäœããã³ã¯ã©ã¹ã¿ãŒå šäœã®èšå®ã«å¯ŸããŠçºçããå¯èœæ§ã®ããïŒç¡è¬ãŸãã¯æªæã®ããïŒã¢ã¯ã·ã§ã³ããã¯ã©ã¹ã¿ãŒãä¿è·ãããŸãã
ã·ã£ãŒãã£ã³ã°
ãã®å Žåã®ã·ã£ãŒãã£ã³ã°ã¯ãæå®ãããã€ã³ããã¯ã¹ã§é«è² è·ã®çµ±èšã³ã¬ã¯ã·ã§ã³ãå ±æããŸããããã¯ãããã«è¿œå ããè€æ°ã®ã·ã£ãŒãéã®ã·ã£ãŒãããŒã§ããã³ã¬ã¯ã·ã§ã³ã«å¯ŸããŠã·ã£ãŒãã£ã³ã°ãã¢ã¯ãã£ãåããããšããã®ããã¥ã¡ã³ãã®ã»ããå šäœãChunksãšåŒã°ããnåã®éšåã«åå²ãããŸããã·ã£ãŒãã£ã³ã°ãæå¹ã«ãªã£ãŠãããšãã«ã³ã¬ã¯ã·ã§ã³ãåå²ããããã£ã³ã¯ã®æ°ãããã³æ°ãããã£ã³ã¯ã圢æãããé »åºŠã¯ãã³ã¬ã¯ã·ã§ã³å ã®ããŒã¿éãšããã£ã³ã¯ã®ãµã€ãºã«åœ±é¿ãããã£ã³ã¯ãµã€ãºãã©ã¡ãŒã¿ãŒã«äŸåããããã©ã«ãã§ã¯64 Mbã§ããã¯ã©ã¹ã¿ãŒã§å¥ã®ãã£ã³ã¯ãµã€ãºãæå®ããå Žåã¯ããããã®ã³ã¬ã¯ã·ã§ã³ã§ã·ã£ãŒãã£ã³ã°ãã¢ã¯ãã£ãã«ããåã«ãããè¡ãå¿ èŠããããŸããæ°ãããã£ã³ã¯ãµã€ãºã¯ãæ°ãã圢æããããã£ã³ã¯ã«ã®ã¿é©çšãããŸãã
ãã£ã³ã¯ã®ãµã€ãºãå€æŽããã«ã¯ãã¹ãŒããŒãŠãŒã¶ãŒèšŒææžã§Mongosã«æ¥ç¶ããèªèšŒãæž¡ããŸããäžè¬ã«ãèªèšŒã¯ããã®ã¡ã«ããºã ïŒauthenticationMechanismåŒæ°ïŒã蚌ææžã®èªèšŒãæ åœããããŒã¿ããŒã¹ïŒauthenticationDatabaseïŒãããã³èšŒææžãææãããŠãŒã¶ãŒïŒuïŒã«çŽæ¥æå®ããããšã«ãããå ¥åãšçµã¿åãããããšãã§ããŸããã¹ãŒããŒãŠãŒã¶ãŒïŒrootïŒã®å Žåããconnect + authenticationãã³ãã³ãã¯æ¬¡ã®åœ¢åŒãåããŸãã
mongo --ssl --sslCAFile /root/mongodb1/keys/mongodb-CA-cert.crt --sslPEMKeyFile /root/mongodb1/keys/rootuser.pem --host server1.cluster.com --port 27017 --authenticationMechanism "MONGODB-X509" --authenticationDatabase "$external" -u âCN=root,OU=StatisticsClient,O=SomeSystems,L=Moscow,ST=MoscowRegion,C=RUâ
ãã°ã€ã³ã«æåããããæ§æã³ã¬ã¯ã·ã§ã³ãéžæããç®çã®ãã©ã¡ãŒã¿ãŒãå€æŽããŸãã
mongos> use config mongos> db.settings.save({_id: "chunksize", value: NumberLong(32)}) WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
ãã£ã³ã¯ãµã€ãºã32 Mbã«èšå®ããŸããã次ã®ã³ãã³ãã䜿çšããŠããã®èšå®ã®çŸåšã®å€ã確èªã§ããŸãã
mongos> db.settings.find({'_id':"chunksize" }) { "_id" : "chunksize", "value" : NumberLong(32) }
ã·ã£ãŒãã管çããã«ã¯ïŒæåã«ã·ã£ãŒããè¿œå ããå¿ èŠããããŸãïŒãçµã¿èŸŒã¿ããŒã«clusterAdminãæã€ãŠãŒã¶ãŒãšããŠæ¥ç¶ããå¿ èŠããããŸããã¯ã©ã¹ã¿ãŒç®¡çè ã®èšŒææžãäœæããŸãã
server1.cluster.com:~/mongodb/keys# openssl req -new -nodes -newkey rsa:2048 -keyout clusterAdmin.key -out aclusterAdmin.csr Generating a 2048 bit RSA private key ................+++ .......................................+++ writing new private key to 'clusterAdmin.key' ----- You are about to be asked to enter information that will be incorporated into your certificate request. What you are about to enter is what is called a Distinguished Name or a DN. There are quite a few fields but you can leave some blank For some fields there will be a default value, If you enter '.', the field will be left blank. ----- Country Name (2 letter code) [AU]: RU State or Province Name (full name) [Some-State]: MoscowRegion Locality Name (eg, city) []: Moscow Organization Name (eg, company) [Internet Widgits Pty Ltd]: SomeSystems Organizational Unit Name (eg, section) []: Statistics Common Name (eg server FQDN or YOUR name) []: clusteradmin Email Address []: Please enter the following 'extra' attributes to be sent with your certificate request A challenge password []: An optional company name []:
server1.cluster.com:~/mongodb/keys# openssl x509 -CA mongodb-CA-cert.crt -CAkey mongodb-private.key -CAcreateserial -req -days 36500 -in clusterAdmin.csr -out clusterAdmin.crt Signature ok subject=/C=RU/ST=MoscowRegion/L=Moscow/O=SomeSystems/OU=Statistics/CN=clusteradmin Getting CA Private Key Enter pass phrase for mongodb-private.key:
server1.cluster.com:~/mongodb/keys# cat clusterAdmin.key clusterAdmin.crt > clusterAdmin.pem
server1.cluster.com:~/mongodb/keys# openssl x509 -in clusterAdmin.pem -inform PEM -subject -nameopt RFC2253 subject= CN=clusteradmin,OU=StatisticsClient,O=SomeSystems,L=Moscow,ST=MoscowRegion,C=RU -----BEGIN CERTIFICATE-----
ç§ãã¡ã«ãšã£ãŠçããããšã¯äœããããŸãããã¯ã©ã¹ã¿ãŒã¡ã³ããŒã«ç€ºãããOUãšã¯ç°ãªãOUãæå®ããããšãå¿ããªãã§ãã ããã次ã«ãMongosã«å床æ¥ç¶ããrootãšããŠèªèšŒããæ°ãããŠãŒã¶ãŒïŒã¯ã©ã¹ã¿ãŒç®¡çè ïŒãè¿œå ããŸãã
mongos> db.getSiblingDB("$external").runCommand({ createUser: "CN=clusteradmin,OU=StatisticsClient,O=SomeSystems,L=Moscow,ST=MoscowRegion,C=RU", roles: [{role: "clusterAdmin", db: "admin"}] })
ã¯ã©ã¹ã¿ãŒç®¡çè ã®äžã§mongosã«åæ¥ç¶ããŸãïŒèªèšŒã¯æ¥ç¶ã³ãã³ãã«å«ãŸããŠããŸãïŒïŒ
mongo --ssl --sslCAFile /root/mongodb1/keys/mongodb-CA-cert.crt --sslPEMKeyFile /root/mongodb1/keys/clusterAdmin.pem --host server1.cluster.com --port 27017 --authenticationMechanism "MONGODB-X509" --authenticationDatabase "$external" -u âCN=clusteradmin,OU=StatisticsClient,O=SomeSystems,L=Moscow,ST=MoscowRegion,C=RUâ
ã¢ãŒãã¿ãŒã€ã³ã¹ã¿ã³ã¹ãé€ããã»ããã®ã¬ããªã«ã§æå®ãããŠããã·ã£ãŒããè¿œå ããŸãã
mongos> sh.addShard("rs0/server1.cluster.com:27000,server2.cluster.com:27000") mongos> sh.addShard("rs1/server1.cluster.com:27001,server2.cluster.com:27001") mongos> sh.addShard("rs2/server1.cluster.com:27002,server2.cluster.com:27002")
ã·ã£ãŒãã®è¿œå ã§ãã¹ãŠãããŸããã£ãå Žåãã·ã£ãŒãã£ã³ã°ã³ãã³ãã§çŸåšã®ã¹ããŒã¿ã¹ã確èªã§ããŸãã
mongos> sh.status() --- Sharding Status --- sharding version: { "_id" : 1, "minCompatibleVersion" : 5, "currentVersion" : 6, "clusterId" : ObjectId("5795284cd589624d4e36b7d4") } shards: { "_id" : "rs0", "host" : "rs0/server1.cluster.com:27100,server2.cluster.com:27200" } { "_id" : "rs1", "host" : "rs1/server1.cluster.com:27101,server2.cluster.com:27201" } { "_id" : "rs2", "host" : "rs2/server1.cluster.com:27102,server2.cluster.com:27202" } active mongoses: "3.2.8" : 1 balancer: Currently enabled: yes Currently running: no Failed balancer rounds in last 5 attempts: 0 Migration Results for the last 24 hours: No recent migrations databases:
ã·ã£ãŒãã衚瀺ããããã©ã³ãµãŒã®ã¹ããŒã¿ã¹ã衚瀺ãããŸãããã©ã³ãµãŒã¯ãªã³ã«ãªã£ãŠããŸãããå©çšå¯èœãªã·ã£ãŒãéã§åé ããããã£ã³ã¯ã®ç§»è¡ã®ããã®ããŒã¿ããŸã ãªããããçŸåšã¢ã€ãã«ç¶æ ã§ããããã¯ã空ã®ãããŒã¿ããŒã¹ããªã¹ãã瀺ããã®ã§ãããããã£ãŠãã·ã£ãŒãã¯ã©ã¹ã¿ãŒãæ§ç¯ããŸããããããã©ã«ãã§ã¯ããã¹ãŠã®ããŒã¿ããŒã¹ã®ãã¹ãŠã®ã·ã£ãŒãã§ã·ã£ãŒãã£ã³ã°ãç¡å¹ã«ãªã£ãŠããŸãã次ã®2ã€ã®æ®µéã«å«ãŸããŠããŸãã
æé 1.ç®çã®ããŒã¹ã®ã·ã£ãŒãã£ã³ã°ããªã³ã«ããŸããç§ãã¡ã®å Žåããããã¯åæã§ãïŒ
mongos> sh.enableSharding("statistics")
çµæã確èªããŸãã
mongos> sh.status() --- Sharding Status --- sharding version: { "_id" : 1, "minCompatibleVersion" : 5, "currentVersion" : 6, "clusterId" : ObjectId("5795284cd589624d4e36b7d4") } shards: { "_id" : "rs0", "host" : "rs0/server1.cluster.com:27000,server2.cluster.com:27000" } { "_id" : "rs1", "host" : "rs1/server1.cluster.com:27001,server2.cluster.com:27001" } { "_id" : "rs2", "host" : "rs2/server1.cluster.com:27002,server2.cluster.com:27002" } active mongoses: "3.2.8" : 1 balancer: Currently enabled: yes Currently running: no Failed balancer rounds in last 5 attempts: 0 Migration Results for the last 24 hours: No recent migrations databases: { "_id" : "analytics", "primary" : "rs2", "partitioned" : true }
åæããŒã¿ããŒã¹ãããŒã¿ããŒã¹ã®ãªã¹ãã«è¡šç€ºãããã¯ãã§ãããŸãããrs2ãã·ã£ãŒãããã©ã€ããªã·ã£ãŒããšããŠãã®ããŒã¿ããŒã¹ã«å²ãåœãŠãããŠããããšãããããŸãïŒPRIMARY Setã¬ããªã«ãšæ··åããªãã§ãã ããïŒãã€ãŸããã·ã£ãŒãã£ã³ã°ãç¡å¹ã«ãªã£ãŠãããã¹ãŠã®ã³ã¬ã¯ã·ã§ã³ããã¥ã¡ã³ãã¯ããã®ãã©ã€ããªã·ã£ãŒãïŒrs2ïŒã«å®å šã«ä¿åãããŸãã
ã¹ããã2.ã³ã¬ã¯ã·ã§ã³ã®ã·ã£ãŒãã£ã³ã°ããªã³ã«ããŸãã
åè¿°ã®ããã«ãã·ã£ãŒãå¯èœãªã³ã¬ã¯ã·ã§ã³ã®ããã¥ã¡ã³ãã®ã³ã¬ã¯ã·ã§ã³å šäœããã£ã³ã¯ã«åå²ããã«ã¯ãã¢ã³ã°ã«ããŒã€ã³ããã¯ã¹ïŒã·ã£ãŒãã£ã³ã°ããŒïŒãå¿ èŠã§ãã圌ã®éžæã¯éåžžã«è²¬ä»»ã®ããã¿ã¹ã¯ã§ãããå®è£ ã®èŠä»¶ãšåžžèã«åºã¥ããŠè³¢æã«ã¢ãããŒãããå¿ èŠããããŸããã³ã¬ã¯ã·ã§ã³ããã£ã³ã¯ã«åå²ãããã€ã³ããã¯ã¹ã¯ãæ¢åã®ã€ã³ããã¯ã¹ããéžæãããããã³ã¬ã¯ã·ã§ã³ã«æå³çã«è¿œå ãããŸããäœããã®æ¹æ³ã§ãã·ã£ãŒãã£ã³ã°ãæå¹ã«ãªã£ãŠããæç¹ã§ãããŒã«å¯Ÿå¿ããã€ã³ããã¯ã¹ãã³ã¬ã¯ã·ã§ã³ã«ååšããå¿ èŠããããŸããã·ã£ãŒãã£ã³ã°ããŒã¯ã察å¿ããã€ã³ããã¯ã¹ã«ç¹å¥ãªå¶éã課ããŸãããå¿ èŠã«å¿ããŠãäŸãã°{â sâïŒ1ãâ tsâïŒ-1}ã®ããã«è€åããããšãã§ããŸãã
å¿ èŠãªã€ã³ããã¯ã¹ã決å®ãããããããäœæãããããåæããŒã¿ããŒã¹ã®çµ±èšã³ã¬ã¯ã·ã§ã³ã®ã·ã£ãŒãã£ã³ã°ããŒãšããŠæå®ããŸããåè¿°ããããã«ãçµ±èšã³ã¬ã¯ã·ã§ã³ã®æã代衚çãªãã£ãŒã«ãã¯ãã»ã³ãµãŒã®èå¥å-ãã£ãŒã«ãsã§ããã³ã¬ã¯ã·ã§ã³å ã«å¯Ÿå¿ããã€ã³ããã¯ã¹ããŸã äœæããŠããªãå Žåã¯ãäœæããŸãã
mongos> use analytics mongos> db.statistics.ensureIndex({"s":1})
ããŒã·ã£ãŒãã£ã³ã°ã€ã³ããã¯ã¹ã䜿çšããŠã³ã¬ã¯ã·ã§ã³ã·ã£ãŒãã£ã³ã°ããªã³ã«ããŸãã
mongos> sh.shardCollection("analytics.statistics", {"s":1})
ããããã¯ãã¯ã©ã¹ã¿ãŒå ã®ã·ã£ãŒãã£ã³ã°ããŒã¿ã«ã€ããŠå®éã«è©±ãããšãã§ããŸããã³ã¬ã¯ã·ã§ã³ã®ã·ã£ãŒãã£ã³ã°ãæå¹ã«ããåŸãã³ã¬ã¯ã·ã§ã³ã¯ãã£ã³ã¯ã«åå²ãããŸãïŒéã¯ããŒã¿ã®ãµã€ãºãšãã£ã³ã¯èªäœã®ãµã€ãºã«äŸåããŸãïŒãæåã¯ãã©ã€ããªã·ã£ãŒãã«ããããã®åŸããã©ã³ã·ã³ã°ïŒç§»è¡ïŒããã»ã¹äžã«ä»ã®ã·ã£ãŒãã«åå²ãããŸããç§ã®æèŠã§ã¯ããã©ã³ã¹ããšãããã»ã¹ã¯éåžžã«ãã£ããããŠããŸããç§ãã¡ã®ã±ãŒã¹ã§ã¯ã3Mã¬ã³ãŒãã®ã³ã¬ã¯ã·ã§ã³ã1é±é以äžã«ããã£ãŠ3ã€ã®ã·ã£ãŒãéã§é åžãããŸããã
ãã°ããããŠãããsh.statusïŒïŒã³ãã³ããå床å®è¡ããŠãå€æŽç¹ã確èªããŸãããã
mongos> sh.status() --- Sharding Status --- sharding version: { "_id" : 1, "minCompatibleVersion" : 5, "currentVersion" : 6, "clusterId" : ObjectId("5773899ee3456024f8ef4895") } shards: { "_id" : "rs0", "host" : "rs0/server1.cluster.com:27000,server2.cluster.com:27000" } { "_id" : "rs1", "host" : "rs1/server1.cluster.com:27001,server2.cluster.com:27001" } { "_id" : "rs2", "host" : "rs2/server1.cluster.com:27002,server2.cluster.com:27002" } active mongoses: "3.2.8" : 1 balancer: Currently enabled: yes Currently running: yes Balancer lock taken at Sun Jul 29 2016 10:18:32 GMT+0000 (UTC) by MongoDB:27017:1468508127:-1574651753:Balancer Collections with active migrations: statistic.statistic started at Sun Jul 29 2016 10:18:32 GMT+0000 (UTC) Failed balancer rounds in last 5 attempts: 0 Migration Results for the last 24 hours: 3 : Success 2 : Failed with error 'aborted', from rs2 to rs0 databases: { "_id" : "analytics", "primary" : "rs2", "partitioned" : true } analytics.statistics shard key: { "s" : 1 } unique: false balancing: true chunks: rs0 1 rs1 2 rs2 21 too many chunks to print, use verbose if you want to force print
以åã«ã·ã£ãŒãã£ã³ã°ãæå¹ã«ããåæããŒã¿ããŒã¹ã«ãçŸåšã®ã·ã£ãŒãããŒã衚瀺ãããçµ±èšã³ã¬ã¯ã·ã§ã³ã衚瀺ãããŸããããŸããåºåã§ã¯ãã·ã£ãŒãããšã®ãã£ã³ã¯ã®ååžãèŠã€ããããšãã§ããŸããã³ã¬ã¯ã·ã§ã³ã«å°æ°ã®ãã£ã³ã¯ãããå Žåã¯ããã£ã³ã¯ã®ç°¡åãªæŠèŠã衚瀺ãããŸãããŸãããã©ã³ãµãŒã®ã»ã¯ã·ã§ã³ã§ã¯ããã£ã³ã¯ã®æ£åžžãªç§»è¡ã«é¢ããæ å ±ããŸãã¯åæ¥ã®ãšã©ãŒã«é¢ããæ å ±ã確èªã§ããŸãã
ç£ç£è
æšæºã®MongoDBã³ãã¥ããã£ããã±ãŒãžãã€ã³ã¹ããŒã«ãããšãã·ã¹ãã ã«mongodbãµãŒãã¹ã衚瀺ããããµãŒããŒã®ãããã¯ã¹åãããŒãžã§ã³ãè¡šããŸãããã®ãµãŒãã¹ã¯ãMongoDBã®ã€ã³ã¹ããŒã«åŸã«ããã©ã«ãã§éå§ãããŸãã
ãµãŒãã¹ãéå§ãããšããã¹/etc/init.d/mongodã«æ²¿ã£ãŠé 眮ãããæªéåã¹ã¯ãªãããæäŸãããŸãããæ°ã¥ããããããŸããããåããã·ã³äžã§è€æ°ã®mongodã€ã³ã¹ã¿ã³ã¹ãå®è¡ããserver1.cluster.comããã³server2.cluster.comããŒã¿ãµãŒããŒã«å¯ŸããŠ1ã€ã®mongosãå®è¡ããå¿ èŠããããŸãã
äžèŠãäŸãšããŠ/etc/init.d/mongodã¹ã¯ãªããã䜿çšããæ¢è£œã®ãœãªã¥ãŒã·ã§ã³ããããŸãããã¹ãŒããŒãã€ã¶ãŒãŠãŒãã£ãªãã£ã䜿çšãããªãã·ã§ã³ã¯ç§ã«ãšã£ãŠãã䟿å©ã§ééçã«èŠããŸããã
ã¹ãŒããŒãã€ã¶ãŒã¯ãmongo {d / s}ã®ãã¹ãŠã®ã³ãã³ããåæã«éå§ããã³åæ¢ããæ©èœã®åœ¢ã§ãå°ããªãã©ã¹ãæäŸããŸãã
supervisorctl start all supervisorctl stop all
ïŒãã ãããã®ãã·ã³ã®ããã«ãã¹ãŒããŒãã€ã¶ãŒã«ãã£ãŠèµ·åãããä»ã®ã¢ããªã±ãŒã·ã§ã³ããã·ã³ã«ãªãå ŽåïŒã
ã¹ãŒããŒãã€ã¶ããã±ãŒãžã¯ãæšæºãªããžããªããLinuxãã¡ããªã®ã»ãšãã©ã®ãªãã¬ãŒãã£ã³ã°ã·ã¹ãã ã«ã€ã³ã¹ããŒã«ãããŸããç§ã®å ŽåïŒDebian 8ïŒã§ã¯ãã³ãã³ããé¢é£ããŸãïŒ
# apt-get install supervisor
ã¹ãŒããŒãã€ã¶ãèšå®ããã«ã¯ãèµ·åããåã¢ããªã±ãŒã·ã§ã³ã®èšå®ãåå¥ã®èšå®ãã¡ã€ã«ã«äœæãããããã¹ãŠã®èšå®ã1ã€ã«ãŸãšããå¿ èŠããããŸãã
次ã«ãrs0ã¬ããªã«ã®mongodèšå®ã®äŸã瀺ããŸãã
# # /etc/supervisor/conf.d/mongod-rs0.conf # [program:mongod-rs0] command=mongod --config /root/mongodb/cfg/rs0.conf user=root stdout_logfile=/root/mongodb/logs/supervisor/mongod-rs0-stdout.log redirect_stderr=true autostart=true autorestart=true stopwaitsecs=60
è§ãã£ãã§ãéå§ãŸãã¯åæ¢ã«äœ¿çšããã¢ããªã±ãŒã·ã§ã³ã®èå¥åãå®çŸ©ããŸããã³ãã³ããã©ã¡ãŒã¿ã¯ãå®éã«ã¹ãŒããŒãã€ã¶ãå®è¡ããå¿ èŠãããã³ãã³ããèšå®ããŸããmongodã¯èšå®ãã¡ã€ã«ãåä¿¡ããŸãã次ã«ãããã»ã¹ãèµ·åãããŠãŒã¶ãŒãæå®ããŸãããã©ã¡ãŒã¿ãŒstdout_logfile-ã¹ãŒããŒãã€ã¶ãŒãæžã蟌ãåºåãã¡ã€ã«ãžã®ãã¹ãèšå®ããŸããããã¯ãäœããããŸãããããã¹ãŒããŒãã€ã¶ãŒãã¢ããªã±ãŒã·ã§ã³ãéå§ããªãçç±ãç解ããå¿ èŠãããå Žåã«åœ¹ç«ã¡ãŸãã
redirect_stderrã¯ãäžã§æå®ããã®ãšåããã°ãã¡ã€ã«ã«ãšã©ãŒã¹ããªãŒã ããªãã€ã¬ã¯ãããããã¹ãŒããŒãã€ã¶ãŒã«æ瀺ããŸãã次ã«ããªãã·ã§ã³autostartããã³autorestartãå¿ ãå«ããŠãã ããäžæ£ãªãµãŒããŒã®åèµ·åãšããã»ã¹èªäœã®ã¯ã©ãã·ã¥ã®å Žåã
ãŸããã¢ããªã±ãŒã·ã§ã³ãåæ¢ãããšãã«æå®ãããç§æ°ã ãã¹ãŒããŒãã€ã¶ãŒãåŸ æ©ããstopwaitsecsãã©ã¡ãŒã¿ãŒãå€æŽãããšäŸ¿å©ã§ããããã©ã«ãã§ã¯ãã¢ããªã±ãŒã·ã§ã³ãåæ¢ãããšãã¹ãŒããŒãã€ã¶ãŒã¯TERMã·ã°ãã«ãéä¿¡ãã10ç§éåŸ æ©ããŸããæå¹æéãéããŠãã¢ããªã±ãŒã·ã§ã³ãå®äºããŠããªãå Žåãã¢ããªã±ãŒã·ã§ã³ã¯ãã§ã«KILLã·ã°ãã«ãéä¿¡ããŠããŸãããããã¯ã¢ããªã±ãŒã·ã§ã³ã«ãã£ãŠç¡èŠã§ãããçè«çã«ã¯ããŒã¿æ倱ã«ã€ãªããå¯èœæ§ããããŸãããããã£ãŠãã¢ããªã±ãŒã·ã§ã³ã®å®äºãåŸ æ©ããããã©ã«ãã®ééãå¢ããããšããå§ãããŸãã
çæãããæ§æãã¡ã€ã«ã¯ãé©åãªã¹ãŒããŒãã€ã¶ãŒãã£ã¬ã¯ããªã«é 眮ããå¿ èŠããããŸããLinuxOSã®ã«ãŒã«ãšããŠã¯ã/etc/supervisor/conf.d/ã§ãã
ãã¹ãŠã®æºåãã§ãããã次ã®ã³ãã³ãã䜿çšããŠã¹ãŒããŒãã€ã¶ãŒæ§æãæŽæ°ããå¿ èŠããããŸãã
# supervisorctl reload
æ§ææžã¿ã¢ããªã±ãŒã·ã§ã³ã®ç¶æ ã®åæ¢ãéå§ãããã³ãã§ãã¯ã¯ã次ã®ã³ãã³ãã«ãã£ãŠå®è¡ãããŸãã
# supervisorctl stop mongod-rs0 # supervisorctl start mongod-rs0 # supervisorctl status mongod-rs0
ã¹ãŒããŒãã€ã¶ãŒã®äœ¿çšã«åãæ¿ããåŸã¯ãmongosãå®è¡ããããŒã27017ïŒãµãŒããŒã®åèµ·ååŸãªã©ïŒã䜿çšããå¯èœæ§ã®ããæšæºmongodbãµãŒãã¹ãéå§ãããªãããã«ããããšãéèŠã§ãããããè¡ãã«ã¯ã/ etc / init.d / mongodã¹ã¯ãªãããåé€ããã ãã§ãã
ã圹ç«ã¡æ å ±
倧èŠæš¡ãªã³ã¬ã¯ã·ã§ã³ã®ã·ã£ãŒãã£ã³ã°ãæå¹ã«ãã
移è¡æã«ããŒã¿ããŒã¹ã®æãããŒããããã³ã¬ã¯ã·ã§ã³ã¯åèšã§3Mã¬ã³ãŒããè¶ ãããã¹ãäžã«ããã®ãããªã³ã¬ã¯ã·ã§ã³ïŒsh.shardCollectionïŒïŒã³ãã³ãïŒã«ã·ã£ãŒãã£ã³ã°ãå«ããããšã¯åé¡ãªãå®è¡ãããŸããããã ãããã¹ãã¯1å件ã®åæ§ã®ã¬ã³ãŒããå«ã人çºçã«çæãããããŒã¿ããŒã¹ã§ãå®è¡ãããŸããããã®ãããªããªã¥ãŒã ã§ã¯ãsh.shardCollectionïŒïŒã³ãã³ãã¯ãã°ãããããšãã¿ã€ã ã¢ãŠãããšã©ãŒã§çµäºããŸãããã®ç¶æ³ããæãåºãæ¹æ³ã¯ã次ã®æé ã§ãã
ã¹ããã1.ããŒã¿ããŒã¹å šäœãã¯ã©ã¹ã¿ãŒã«ã€ã³ããŒãããŸãã
ã¹ããã2.å®åãµãŒããŒäžãŸãã¯æ¢ã«ã¯ã©ã¹ã¿ãŒäžã§ãå¥åã®ã倧èŠæš¡ãã³ã¬ã¯ã·ã§ã³ã®ãã³ããäœæããŸãã次ã«äŸã瀺ããŸãã
mongoexport --db analytics --collection statistics --out statistics.json
ã¹ããã3.ã¯ã©ã¹ã¿ãŒäžã®ã倧èŠæš¡ãã³ã¬ã¯ã·ã§ã³ãåé€ããŸãã
> use analytics > db.statistics.drop()
ã¹ããã4.空ã®ã倧ãã³ã¬ã¯ã·ã§ã³ãäœæããããã«ã€ã³ããã¯ã¹ãè¿œå ããŸããããã«ãããã·ã£ãŒããè¡ãããŸãã
> db.analytics.ensureIndex({"s":1})
ã¹ããã5.ã·ã£ãŒãã£ã³ã°ããŒã䜿çšããŠã³ã¬ã¯ã·ã§ã³ã®ã·ã£ãŒãã£ã³ã°ããªã³ã«ããŸãã
> sh.shardCollection("analytics.statistics", {"s":1})
ã¹ããã6.ãããŠãã³ã¬ã¯ã·ã§ã³ããŒã¿ãã€ã³ããŒãããŸãã
mongoimport --db analytics --collection statistics --file statistics.json
ãã®ææ³ã¯ããŸããããŸããããjson圢åŒã®å€§èŠæš¡ãªã³ã¬ã¯ã·ã§ã³ã®ãšã¯ã¹ããŒã/ã€ã³ããŒãã¯é«éãªããã»ã¹ã§ã¯ãªãããšã«æ³šæããŠãã ããã
ããŒã¿ããŒã¹ã®ããã¯ã¢ãã
ã·ã£ãŒãã¯ã©ã¹ã¿ãŒã®ãã¹ãŠã®ã³ã³ããŒãã³ãã®ããã¯ã¢ããã³ããŒã®äœæã¯éåžžã«è€éãªæé ã§ããããã©ã³ãµãŒããªãç¶æ ã«ããŠïŒç§»è¡äžã«åŒ·å¶çã«åæ¢ããããšã¯ã§ããŸããïŒãåŸç¶ã®ããã¯ã¢ããã®ããã«åã·ã£ãŒãã®SECONDARYããŒãããããã¯ããå¿ èŠããããŸããå®å šãªããã¯ã¢ããã®å®è¡ã®è©³çŽ°ã«ã€ããŠã¯ãå ¬åŒããã¥ã¡ã³ããã芧ãã ããã
ç§ãã¡ã¯ãå¿ èŠãªããŒã¿ããŒã¹ã®éåžžã®ããŒã¿ãã³ããå®æçã«äœæããããšã§ãããã¯ã¢ããã®åé¡ã解決ããŸãããããã§ããã®æé ã®å®è£ ã«ã€ããŠèª¬æããŸããMongoDBã³ãã¥ããã£ããã±ãŒãžã®äžéšã§ããmongodumpãŠãŒãã£ãªãã£ã䜿çš
ããŠãåæããŒã¿ããŒã¹ãããã¯ã¢ããããŸãã
MongoDBã«ã¯ãããŒã¿ããã¯ã¢ãããå®è¡ããããã®æå°éã®æš©éã»ãããæã€ç¹å¥ãªçµã¿èŸŒã¿ããã¯ã¢ããããŒã«ããããŸãããã®æé ãå®è¡ããã«ã¯ãåã ã®ãŠãŒã¶ãŒãäœæããåŸæ¥ãæåã«x.509蚌ææžãçæããŸãã蚌ææžãçæããããã®æé å šäœã説æããã®ã§ã¯ãªããèšäºã«ç¹°ãè¿ã瀺ãããŠããŸãã次ã®äž»é¡ãååŸããå¿ èŠããããšã®ã¿èšããŸãã
CN=backuper,OU=StatisticsClient,O=SomeSystems,L=Moscow,ST=MoscowRegion,C=RU
次ã«ãã¯ã©ã¹ã¿ãŒã«æ¥ç¶ããçµã¿èŸŒã¿ã®ããã¯ã¢ããããŒã«ãæã€ããã¯ã¢ãããŠãŒã¶ãŒãäœæããŸãã
mongos> db.getSiblingDB("$external").runCommand({ createUser: "CN=backuper,OU=StatisticsClient,O=SomeSystems,L=Moscow,ST=MoscowRegion,C=RU", roles: [{role: "backup", db: "admin"}] })
ãŠãŒã¶ãŒãäœæããããåæããŒã¿ããŒã¹ã®ããã¯ã¢ãããè©Šè¡ã§ããŸããmongodumpãŠãŒãã£ãªãã£ã®ã³ãã³ãåŒæ°ã¯ãèªèšŒã䜿çšããæ¥ç¶ã«äŒŒãŠããŸããããŒã¿ããŒã¹åïŒ--dbïŒã®ã¿ãè¿œå ã§ç€ºããããã³ãïŒ-oïŒãä¿åããããã£ã¬ã¯ããªãããã³ãã¹ãŠã®ãã³ããã¡ã€ã«ãå§çž®ããããšã瀺ã--gzipåŒæ°ãè¿œå ãããŸãã
mongodump --ssl --sslCAFile â/root/mongodb/keys/mongodb-CA-cert.crtâ --sslPEMKeyFile â/root/mongodb/keys/backuper.pemâ -u "CN=backuper,OU=StatisticsClient,O=SomeSystems,L=Moscow,ST=MoscowRegion,C=RU" --host server1.cluster.com --port 27017 --authenticationMechanism "MONGODB-X509" --authenticationDatabase "$external" --db analytics --gzip -o "/path/to/backup/"
ã³ãŒãã®ããã...
èšäºã®æåŸã§ãäœæããã¯ã©ã¹ã¿ãŒãžã®æ¥ç¶ã瀺ãããã°ã©ã ã³ãŒãã®äŸãå ±æããŸããã¯ã©ã¹ã¿ãŒã§åäœãããµãŒãã¹ã¯C ++ããã³Pythonã§èšè¿°ãããå€ãã®éšåã§æ§æãããŠãããããäŸã¯ãããã®çŽ æŽãããããã°ã©ãã³ã°èšèªã§äœæãããŸãã
ããã§ã¯ãC ++ã®äŸããå§ããŸãããã以äžã®æ¥ç¶äŸã¯ãå ¬åŒã®MongoDBãã©ã€ããŒmongodb-cxx-driver-legacy-1.1.1ã«é¢é£ããŠããŸãã
#include <mongo/client/dbclient.h> #include <mongo/client/options.h> ... mongo::DBClientConnection client(true); // try { // SSL mongo::client::Options options; options.setSSLMode(mongo::client::Options::SSLModes::kSSLRequired); options.setSSLCAFile("/path_to_certs/mongodb-CA-cert.crt"); options.setSSLPEMKeyFile("/path_to_certs/analyticsuser.PEM"); mongo::Status status = mongo::client::initialize(options); mongo::massertStatusOK(status); // , client.connect("www.server1.cluster.com:27017"); // mongos // : , , mongo::BSONObjBuilder auth_params; auth_params.append("db", "$external"); auth_params.append("user", "CN=username,OU=StatisticsClient,O=SomeSystems,L=Moscow,ST=MoscowRegion,C=RU"); auth_params.append("mechanism", "MONGODB-X509"); client.auth(auth_params.obj()); // } catch (const mongo::DBException &e) { std::cout << "DBException : " << e.toString() << std::endl; } ...
ããŒã¿ããŒã¹ãã¹ãã«æ¥ç¶ããåã«ãmongo :: client :: Optionsæ§é ã䜿çšããŠãSSLèŠæ±ã¬ãã«ïŒkSSLRequiredïŒããããªãã¯CA蚌ææžïŒmongodb-CA-cert.crtïŒãããã³æ·»ä»PEMãã¡ã€ã«ãæå®ããŠã¯ã©ã€ã¢ã³ããåæåããå¿ èŠããããŸãã¯ã©ã¹ã¿ãŒãŠãŒã¶ãŒïŒãã®å Žåãããã¯åã«äœæããanalyticsuserã§ãïŒã
次ã«ãããŒã¿ããŒã¹ã«æ¥ç¶ãããã¹ãŠãæåããã°èªèšŒã«æåããŸããèªèšŒãééããããŒã¿ããŒã¹ã®ååã«æ³šæããŠãã ãã-ã$ externalãããŠãŒã¶ãŒèšŒææžãããµããžã§ã¯ããæž¡ãååãšããŠãèªèšŒã¡ã«ããºã ãæå®ããããšãå¿ããªãã§ãã ããããŸãããã¹ã¯ãŒããéä¿¡ããªãããšãããããŸããç§ãã¡ã®èªèšŒã¯å€éšã§ã-蚌ææžèªèšŒãéããŠã
Pythonã§äœæããããããžã§ã¯ãã®Webéšåã«ã¯ãçŽç²ãªpymongoãã©ã€ããŒãå«ãŸãããªããžã§ã¯ãã¢ãã«ã¯mongoengineãã¬ãŒã ã¯ãŒã¯ã䜿çšããŠåœ¢æãããŸãã
å§ããã«ã¯ãpymongoã®äŸïŒ
import ssl db_hosts="server1.cluster.com:27017,server2.cluster.com:27017" db_port=None client = MongoClient(db_hosts, db_port, read_preference=ReadPreference.NEAREST, ssl=True, ssl_certfile="/path_to_certs/analyticsuser.PEM", ssl_cert_reqs=ssl.CERT_REQUIRED, ssl_ca_certs="/path_to_certs/mongodb-CA-cert.crt") db = client[db_name] db.authenticate(name=db_user, source="$external", mechanism="MONGODB-X509")
ç¹å¥ãªããšã¯ãããŸãã-ãããªãã¯CA蚌ææžãšã¯ã©ã€ã¢ã³ãPEMãã¡ã€ã«ã転éããŸããããã§db_hostså€æ°ã«æ³šæããå¿ èŠããããŸã-ããã¯å®éã«ã¯ãMongosã䜿çšå¯èœãªã¢ãã¬ã¹ãšããŒããã³ã³ãã§åºåãããæ¥ç¶æååã§ããããŒããã©ã¡ãŒã¿ãŒïŒdb_portïŒããã®å Žåã¯æå®ã§ããŸãããããããããããããã«æå®ããŸãããæåã®ã¢ãã¬ã¹ãå©çšã§ããªãå Žåããã®æ¹æ³ã§æ¥ç¶ãããpymongoãã©ã€ããŒã¯ãèªåçã«2çªç®ã®ã¢ãã¬ã¹ãžã®åæ¥ç¶ãè©Šã¿ãŸããç·Žç¿ã§ã¯ãæåã®æ¥ç¶ã§äž¡æ¹ã®ãµãŒããŒã䜿çšå¯èœãªå Žåãã¢ãã¬ã¹ãé çªã«éžæãããããšã瀺ããŠããŸããæåã¯server1.cluster.com:27017ãžã®æ¥ç¶ã§ãã
ãã ãããã®pymogoã®åäœããã¹ããããšãpytmogo.errors.AutoReconnectäŸå€ã®çæãèªååæ¥ç¶ã®åã«ããããšãããããŸããããã®ç¶æ³ãåŠçããããã«ãããšãã°çµ±èšããŒãžã衚瀺ããæ©èœãããŒã¿ãèªã¿åãAPIãªã¯ãšã¹ããã©ããã§ããå°ããªãã³ã¬ãŒã¿ãŒãäœæãããŸããïŒ
functools import wraps
from pymongo.errors import AutoReconnect
import time
def pymongo_reconnect(attempts=5): def decorator(f): @wraps(f) def decorated_function(*args, **kwargs): tries_reconnect = attempts if tries_reconnect <= 0: tries_reconnect = 1 while tries_reconnect: try: return f(*args, **kwargs) except AutoReconnect as ar: tries_reconnect -= 1 print("Caught AutoReconnect exception.") if tries_reconnect <= 0: raise ar time.sleep(0.1) print("Attempt to reconnect (%d more)...\n" % tries_reconnect) continue return decorated_function return decorator
ãã³ã¬ãŒã¿ã¯ãé¢æ°ã®å®è¡ãäœåºŠãè©Šè¡ãïŒãã®å Žåã¯5åïŒããã¹ãŠã®è©Šè¡ãè²»ãããŠäŸå€ãçµäºããŸãã
ãŸããæ¥ç¶äŸã®read_preferenceãã©ã¡ãŒã¿ãŒã«ã€ããŠãããã€ã説æããŠãã ããã read_preferenceã¯ããã®æ¥ç¶ã§äœ¿çšããããŒã¿èªã¿åãã«ãŒã«ããã©ã€ããŒã«æ瀺ããŸãïŒæžã蟌ã¿ã¯åžžã«PRIMARYïŒè«ççïŒã§è¡ãããŸãïŒã次ã®å€ã䜿çšã§ããŸã
ãPRIMARY-åžžã«ã·ã£ãŒãã¬ããªã«ã®ãã©ã€ããªã¡ã³ããŒããããŒã¿ãèªã¿åããŸãã PRIMARY_PREFERRED-ã·ã£ãŒãã¬ããªã«ã®ãã©ã€ããªã¡ã³ããŒããèªã¿åããŸãããã»ã«ã³ããªã¬ããªã«ããèªã¿åãããšãã§ããªãå Žåã
SECONDARY-ã·ã£ãŒãã®ã»ã«ã³ããªã¡ã³ããŒããã®ã¿èªã¿åãã
SECONDARY_PREFERRED-ã»ã«ã³ããªã·ã£ãŒãããå¯èœãªéãèªã¿åããŸããããã©ã€ããªã·ã£ãŒãããäžå¯èœãªå Žå
ïŒããã¥ã¡ã³ãpymongoã«è¿°ã¹ãããã«ïŒä»»æã®å©çšå¯èœããèªã¿åºããåã³-æå¯ãã®ããã¥ã¡ã³ãã ãã§ãªããã¬ããªã«ã®æåã®äœ¿çšå¯èœãªã¡ã³ããŒããæå°ãããã¯ãŒã¯é 延ãæãããã®ã«äœ¿çšãããMongiã詳现ã«æãèªäœã -åã«ãã³ã°ã第äžçŽãŸãã¯ç¬¬äºã®ããŒã¿ãæäŸãããããã
ãããã£ãŠããã®ãã©ã¡ãŒã¿ãŒã¯ãäžæ¹ã§ãèªã¿åãèŠæ±ã®è² è·ããPRIMARYã€ã³ã¹ã¿ã³ã¹ããªãããŒãããæ©äŒãæäŸããŸãããä»æ¹ã§ã¯ãç¡é¢ä¿ã§äžè²«æ§ã®ãªãããŒã¿ã«ã€ãªããå¯èœæ§ããããŸãã SECONDARYã€ã³ã¹ã¿ã³ã¹ã«ã¯ãPRIMARYãšã®åæã«äœããã®çç±ã§é 延ããããŸãïŒã¬ããªã«ã®æ§æãšã©ã°ã«äŸåããŸãïŒããããã£ãŠãã·ã¹ãã ã®åæãšå¶éã«åºã¥ããŠã泚æããŠãã®ãªãã·ã§ã³ãéžæããŠãã ããã
PRIMARYãŸãã¯SECONDARYã®èšå®ãæºããããšãã§ããªãå Žåãpymongoã¯OperationFailureäŸå€ãã¹ããŒããããããããã®ãªãã·ã§ã³ã䜿çšããéã«ã¯ãã®åäœãèæ ®ããå¿ èŠãããããšã«æ³šæããŠãã ããã
mongoengineããã±ãŒãžã§ã¯ããã¹ãŠãããæ²ãããªããŸããããããžã§ã¯ãã§æåã«ç®ã«ããã®ã¯ãmongoengineããã±ãŒãžãä»ããããŒã¿ããŒã¹ãžã®æ¥ç¶ãã€ã³ãã§ãã
connect('default', host, port)
OKãç§ã¯èããŸããïŒãæ®ãã®æ¥ç¶ãã©ã¡ãŒã¿ãŒãpymongoãšåãããã«mongoengine.connectã«è»¢éããããã決å®ãããŸãããããããmongoengine.connectã§å¿ èŠãªãã©ã¡ãŒã¿ãŒãèŠã€ãããªãã£ããããç§ã®é¡æã¯ç¡é§ã«ãªããŸãããããã¯ãããåºãåŒæ°ãªã¹ãmongoengine.register_connectionãæã€é¢æ°ã®åãªãäžè¬çãªã©ãããŒã§ãããã®æ©èœã®ãã©ã¡ãŒã¿ãŒã®äžã«ã¯ãMONGODB-X509èš±å¯ã¡ã«ããºã ãæ¥ç¶ã«è»¢éããããã«å¿ èŠãªãã®ããããŸããã§ããããã¬ãŒã ã¯ãŒã¯ãå¿ èŠãªãã®ããç解ãããããšãæåŸ ããŠãç§ã¯ããã€ãã®ç¡é§ãªè©Šã¿ãããŸãããããœãŒã¹ã³ãŒããæãäžããŠããµããŒããäžè¶³ããŠãããpymongoããããç解ããŠããmogoengineã«å¿ èŠãªã¡ã«ããºã ãã転éãã§ããªãããšã確信ããŸããïŒå®éã«åºã¥ããŠããŸãïŒ mongoengineïŒã
ãã®æ¬ ç¹ã®ããã«githubã§åæ§ã®ãã±ããããã§ã«æã¡åºãããŠããããããæåŸãŸã§æã¡èŸŒãŸããŠããªãã£ããããç§ã¯èªåã§ãã©ãŒã¯ãäœæããå¿ èŠãªãã®ããã¹ãŠè¿œå ããããšã«ããŸããã
ãããã£ãŠãx.509èªèšŒã«ããæ¥ç¶ã¯æ¬¡ã®åœ¢åŒãåããŸããã
import ssl from mongoengine import DEFAULT_CONNECTION_NAME, register_connection db_hosts="server1.cluster.com:27017,server2.cluster.com:27017" db_port=None ssl_config = { 'ssl': True, 'ssl_certfile': "/path_to_certs/analyticsuser.PEM", 'ssl_cert_reqs': ssl.CERT_REQUIRED, 'ssl_ca_certs': "/path_to_certs/mongodb-CA-cert.crt", } register_connection(alias=DEFAULT_CONNECTION_NAME, name="statistic", host=db_hosts, port=db_port, username="CN=username,OU=StatisticsClient,O=SomeSystems,L=Moscow,ST=MoscowRegion,C=RU", password=None, read_preference=ReadPreference.NEAREST, authentication_source="$external", authentication_mechanism="MONGODB-X509", **ssl_config)
æ®å¿µãªããããããŸã§ã®ãšãããã¡ã€ã³ã®MongoEngineãªããžããªãšããŒãžããããšã¯ã§ããŸããã§ããããã¹ãŠã®python / pymongoã®çµã¿åããã§ãã¹ãã倱æããŸããå€ãã®éçºè ã®ææ°ã®ããŒã«ãªã¯ãšã¹ãã§ãåããã¹ãã§åæ§ã®åé¡ã«æ°ã¥ããã®ã§ããã¬ãŒã ã¯ãŒã¯ã®ãå®å®ããããã©ã³ãã§èãããã誀åäœã®èããå¿ã³èŸŒãã§ããŸãã
è¿ãå°æ¥ãç¶æ³ãæ¹åãããåé¡ãææ¡ã§ããããã«ãªããx.509èªèšŒã®ãµããŒããã¡ã€ã³ã®MongoEngineãªããžããªã«è¡šç€ºãããããšãé¡ã£ãŠããŸããèªèšŒã¡ã«ããºã ãªãã·ã§ã³ã®
æŽæ°
ãµããŒããmongoengineã®å ¬åŒããŒãžã§ã³ã«è¿œå ãããŸããã