沒什麼好說的 = =!node
參照這個地址 : http://dockone.io/article/181mongodb
建立證書並 copy 給各個服務器.docker
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
主要命令: rs.initiate()
開啓副本集, rs.conf()
驗證初始化副本集, rs.add("node2.example.com")
添加備節點, rs.add("node3.example.com",{arbiterOnly: true})
添加仲裁者節點.centos
--smallfiles \ --keyFile /opt/keyfile/mongodb-keyfile \ --replSet "rs0"
mongodb啓動時須要增長的參數, keyFile 指定連接的關鍵密匙, replSet 統一集合名稱.服務器
參照這個地址 : http://docs.mongodb.org/manual/reference/method/rs.slaveOk/工具
分片就是將,數據分紅幾個碎片來進行管理,提高性能.性能
主從(集羣)能夠做爲一個片進入.學習
須要 config 服務, mongos 分配服務, 和片(mongod).code
若是全部服務都不在同一主機, 那麼須要配置 keyFile 與 主從同樣.ssl
config:
docker --icc=true run -it --privileged=true \ -v /my/db/core/:/opt/keyfile \ -v /my/db/config/:/data/configdb \ -v /etc/localtime:/etc/localtime:ro \ -d \ --name config \ -p 20000:27017 \ centos-mongodb mongod --configsvr --port 27017 --keyFile /opt/keyfile/mongodb-keyfile --smallfiles
--icc=true
是爲了讓 container 之間互通,-v /etc/localtime:/etc/localtime:ro
是爲了同步時區,這樣mongodb 添加 config 的時候就不會出現時區不一樣步沒法鏈接的狀況了.
mongos:
docker --icc=true run -it --privileged=true \ -v /my/db/core/:/opt/keyfile \ -v /etc/localtime:/etc/localtime:ro \ -p 27017:27017 -d --name mongos \ --add-host config.example.com:10.174.35.78 \ centos-mongodb mongos --configdb config.example.com:27017 --keyFile /opt/keyfile/mongodb-keyfile
mongos 須要配置好 config 後再配置, 配置好後 , 登錄可能會沒有權限, 因此先建立 root 用戶.
use admin db.createUser({user:"root",pwd:"passwd",roles:[{role:"root",db:"admin"}]}) db.auth("root","passwd")
其餘工具登錄也使用這個用戶名和密碼.
shard1:
docker --icc=true run -it --privileged=true \ -v /my/db/core/:/opt/keyfile \ -v /my/db/shard1/:/data/db \ -v /etc/localtime:/etc/localtime:ro \ -d -p 10001:27017 --name shard1 \ centos-mongodb mongod --shardsvr --port 27017 --keyFile /opt/keyfile/mongodb-keyfile --smallfiles
shard2:
docker --icc=true run -it --privileged=true \ -v /home/core/:/opt/keyfile \ -v /home/core/shard/shard2/:/data/db \ -v /etc/localtime:/etc/localtime:ro \ -d -p 10002:27017 --name shard2 \ centos-mongodb mongod --shardsvr --port 27017 --keyFile /opt/keyfile/mongodb-keyfile --smallfiles
回到 mongos
sh.addShard("shard1.example.com:10001") sh.addShard("shard2.example.com:10002")
同上選一種
db.runCommand({addShard:"shard1.example.com:10001"}) db.runCommand({addShard:"shard2.example.com:10002"})
查看分片結果
db.printShardingStatus()
刪除片
db.runCommand({removeshard:"shard1.example.com:10001"})
爲數據和表指定分片模式, 好像建議不選擇 _id 爲分片 key.
db.runCommand({enablesharding:"myTest"}) db.runCommand({shardcollection:"myTest.test",key:{_id:1}})
至此分片完成, 能夠進行實際操做.