æ¥éª¤
Step1
ä½ è¦æ¿å°3å°Dockeræå¡å¨çIPå°åï¼å¹¶ä¸å°ä¸é¢ç»åºçIPå°åé
ç½®å°ææçæå¡å¨ä¸é¢ï¼æ¯ä¸å°æå¡å¨é½è¦æ§è¡å¦ä¸å½ä»¤ï¼è®°å¾è¦æ¿æ¢æIPå°åå¦ï¼ï¼
root@node *:/# export node1=10.11.32.174
root@node *:/# export node2=10.11.33.37
root@node *:/# export node3=10.11.31.176
çæ³æ
åµä¸ä½ 并ä¸éè¦è¿ä¹åï¼è¿äºIPé½æ¯å¯ä»¥éè¿DNSèªå¨é
置好çãä½æ¯è¿æ ·ä¼æ´å ç®åï¼æ¯ç«è¿åªæ¯ä¸æ¬¡å®è£
æµè¯ã
Step2
为æ¯ä¸ªèç¹å建å¯é¥æ件ãå¨å
¶ä¸æä¸å°æå¡å¨ä¸é¢æ§è¡å¦ä¸çå½ä»¤ï¼ç¶åæå¯é¥æ件å¤å¶å°å
¶ä½ä¸¤ä¸ªæå¡å¨çåæ ·çä½ç½®ã
å¨è¿ä¸ªæç¨ä¸ï¼æå°æææçä¸è¥¿é½æ¾å¨â/home/coreâæ件夹ä¸ã
root@node *:/# mkdir -p /home/core
root@node *:/# cd /home/core
root@node *:/# openssl rand -base64 741 > mongodb-keyfile
root@node *:/# chmod 600 mongodb-keyfile
root@node *:/# sudo chown 999 mongodb-keyfile
è¿ä¸ªå¯é¥æ件çææè
被设置æid为â999âçç¨æ·äºï¼å 为å¨MongoDBçDocker容å¨ä¸ï¼è¿ä¸ªç¨æ·éè¦ææä½å¯é¥æ件çæéã
Step3
å¯å¨node1ï¼å³ç¬¬ä¸å°Dockeræå¡å¨ï¼çMongoDB容å¨ãå®ä¼å¯å¨ä¸ä¸ªæ²¡æ身份éªè¯æºå¶ç容å¨ï¼æ以æ们è¦è®¾ç½®ä¸ä¸ªç¨æ·ã
root@node1:/# docker run --name mongo \
-v /home/core/mongo-files/data:/data/db \
-v /home/core/mongo-files:/opt/keyfile \
--hostname="node1.example.com" \
-p 27017:27017 \
-d mongo:2.6.5 --smallfiles
ç°å¨å建ä¸ä¸ªadminç¨æ·ãæ们å¯ä»¥è¿æ¥å°ååå¯å¨çmongoDB容å¨ï¼å¹¶è¿å
¥ä¸ä¸ªäº¤äºå¼çshellç¯å¢.
root@node1:/# docker exec -it mongo /bin/bash
è¿æ¶åï¼æ们就è¿å°MongoDBçDocker容å¨éé¢äºï¼ç¶åæ们è¦æå¼ä¸ä¸ªmongo shellç¯å¢ï¼
root@node1:/# mongo
ä¸é¢çå½ä»¤å¯ä»¥æå¼mongo shellç¯å¢ãæ§è¡åä½ ä¼çå°è¿æ ·çè¾åºï¼
MongoDB shell version: 2.6.5
connecting to: test
Welcome to the MongoDB shell.For interactive help, type "help".For more comprehensive documentation, see
http://docs.mongodb.org/Questions? Try the support group
http://groups.google.com/group/mongodb-user>
åæ¢å°adminç¨æ·ï¼
> use admin
switched to db admin
å建ä¸ä¸ªæ°çsite admin ç¨æ·
> db.createUser( {
user: "siteUserAdmin",
pwd: "password",
roles: [ { role: "userAdminAnyDatabase", db: "admin" } ]});
å建æåçè¯ä½ ä¼çå°å¦ä¸çæåä¿¡æ¯:
Successfully added user: {"user" : "siteUserAdmin","roles" : [
{
"role" : "userAdminAnyDatabase",
"db" : "admin"
}
]
}
å建ä¸ä¸ªrootç¨æ·ï¼
> db.createUser( {
user: "siteRootAdmin",
pwd: "password",
roles: [ { role: "root", db: "admin" } ]});
ä½ ä¼çå°å¦ä¸çæåä¿¡æ¯ï¼
Successfully added user: {
"user" : "siteRootAdmin",
"roles" : [
{
"role" : "root",
"db" : "admin"
}
]
}
æ们已ç»å建好äºæ们以åè¦ä½¿ç¨çå 个ç¨æ·ï¼ç°å¨æ们ä¸ä¼éåºäº¤äºå¼shellç¯å¢ï¼mongoåDocker容å¨çç¯å¢ï¼ã
> exit
bye
root@node1:/# exit
Step4
åæ¢ç¬¬ä¸ä¸ªMongoDBå®ä¾ï¼
root@node1:/# docker stop mongo
Step5
è¿æ¬¡ä½¿ç¨å¯é¥æ件å¯å¨ç¬¬ä¸å°MongoDBå®ä¾ï¼è¿æ¯å¨node1ä¸é¢æä½çï¼ã
root@node1:/# docker rm mongo
root@node1:/# docker run \
--name mongo \
-v /home/core/mongo-files/data:/data/db \
-v /home/core/mongo-files:/opt/keyfile \
--hostname="node1.example.com" \
--add-host node1.example.com:${node1} \
--add-host node2.example.com:${node2} \
--add-host node3.example.com:${node3} \
-p 27017:27017 -d mongo:2.6.5 \
--smallfiles \
--keyFile /opt/keyfile/mongodb-keyfile \
--replSet "rs0"
注æï¼
--keyFileçè·¯å¾æ¯/opt/keyfile/mongodb-keyfileï¼è¿æ¯æ£ç¡®çãè¿æ¯å¯é¥æ件å¨Dockerå
é¨çå°åï¼æ们ç¨-vé项å°å¯é¥æ件æ å°å°å®¹å¨å
é¨çé£ä¸ªè·¯å¾ä¸ï¼å³ï¼/opt/keyfile/mongodb-keyfileï¼ã
--add-hostæè¿äºä¿¡æ¯æ·»å å°Docker容å¨ç/etc/hostsæ件ä¸ï¼æ以æ们å¯ä»¥ä½¿ç¨ååèä¸æ¯IPå°åäºãå¨å®é
çç产ç¯å¢ä¸è¿äºä¿¡æ¯é½æ¯DNSï¼è¿äºåæ°é½å¯ä»¥å¿½ç¥ã
Step6
è¿æ¥å°å¯æ¬éä¸å¹¶ä¸å®è£
é
置好å®ãè¿è¿æ¯å¨node1ä¸é¢è¿è¡çãæ们è¦å¼å¯å¦å¤ä¸ä¸ªæ°ç交äºå¼shellç¯å¢è¿å
¥mongo容å¨ï¼åæ¶å¼å¯ä¸ä¸ªmongo shellç¯å¢ï¼
root@node1:/# docker exec -it mongo /bin/bash
root@node1:/# mongo
MongoDB shell version: 2.6.5>
åæ¢å°adminç¨æ·ä¸ï¼
> use admin
switched to db admin
å 为æ们已ç»è®¾ç½®äºä¸ä¸ªå¯ç ï¼æ以è¿æ¬¡æ们ä¸å¾ä¸å身份éªè¯ãæ们æå¯ç 设置为ï¼passwordã
> db.auth("siteRootAdmin", "password");1
ç°å¨æ们å¯ä»¥å¼å¯å¯æ¬éï¼
> rs.initiate(){
"info2" : "no configuration explicitly specified -- making one",
"me" : "node1.example.com:27017",
"info" : "Config now saved locally. Should come online in about a minute.",
"ok" : 1}>
Step7
éªè¯å·²ç»åå§åçå¯æ¬éçé
ç½®ï¼
>
rs0:PRIMARY> rs.conf(){
"_id" : "rs0",
"version" : 1,r
"members" : [
{
"_id" : 0,
"host" : "node1.example.com:27017"
}
]
}
Step8
å¨å
¶ä½ç两个èç¹å¯å¨MongoDBã
å¨node2ä¸é¢æ§è¡å½ä»¤ï¼
root@node2:/# docker run \
--name mongo \
-v /home/core/mongo-files/data:/data/db \
-v /home/core/mongo-files:/opt/keyfile \
--hostname="node2.example.com" \
--add-host node1.example.com:${node1} \
--add-host node2.example.com:${node2} \
--add-host node3.example.com:${node3} \
-p 27017:27017 -d mongo:2.6.5 \
--smallfiles \
--keyFile /opt/keyfile/mongodb-keyfile \
--replSet "rs0"
å¨node3ä¸é¢æ§è¡å½ä»¤ï¼
root@node3:/# docker run \
--name mongo \
-v /home/core/mongo-files/data:/data/db \
-v /home/core/mongo-files:/opt/keyfile \
--hostname="node3.example.com" \
--add-host node1.example.com:${node1} \
--add-host node2.example.com:${node2} \
--add-host node3.example.com:${node3} \
-p 27017:27017 -d mongo:2.6.5 \
--smallfiles \
--keyFile /opt/keyfile/mongodb-keyfile \
--replSet "rs0"
Step 9
å°é£ä¸¤ä¸ªèç¹å å°å¯æ¬éä¸ã
åå°node1èç¹ï¼å¦æä½ å¨è¿éæäºå 次å车é®ï¼enterï¼çè¯ï¼ä½ ä¼çå°ä¸é¢çæ示ï¼ârs0:PRIMARYâãè¿æ¯å 为è¿ä¸ªèç¹æ¯å¯æ¬éârsoâç主èç¹ã
rs0:PRIMARY> rs.add("node2.example.com")
rs0:PRIMARY> rs.add("node3.example.com")
æ们å¯ä»¥éè¿æ§è¡ä¸é¢çå½ä»¤æ¥éªè¯å
¶å®ä¸¤ä¸ªèç¹æ¯å¦æ£ç¡®çå å°è¿ä¸ªå¯æ¬éå½ä¸ï¼
rs0:PRIMARY> rs.status()
å¯è½ä¼è±å åéçæ¶é´æ¥å°node1ä¸é¢çæ°æ®åæ¥å°å
¶ä½ç两个èç¹ä¸é¢ãä½ å¯ä»¥éè¿æ¥çæ¥å¿æ¥è§å¯æ¯ä¸ä¸ªMongoDBçDocker容å¨éé¢åçäºä»ä¹ãå¨ä»»æä¸ä¸ªæå¡å¨ä¸é¢æ§è¡ä¸é¢çå½ä»¤å°±å¯ä»¥äºï¼
root@node*:/# docker logs -ft mongo