MongoDB 基本NoSQL的大數據數據存儲工具,內部數據以文檔的概念存儲BSON(Binary JSON)。
全部的數據存儲概念均離不開「分片」(數據的橫向擴展,以分離存儲的方式提升數據存儲、檢索的性能)、「複製」(高可用,包含冗餘備份、故障自檢、自動恢復、讀寫分離等功能)。
本文不會詳細介紹MongoDB基礎及分佈概念,讀者能夠自行查閱學習。html
# uname -a Linux 1ddc8b08470a 4.9.49-moby #1 SMP Thu Sep 21 05:50:41 UTC 2017 x86_64 GNU/Linux
# mongo -version MongoDB shell version v3.4.10 git version: 078f28920cb24de0dd479b5ea6c66c644f6326e9 OpenSSL version: OpenSSL 1.0.1t 3 May 2016 allocator: tcmalloc modules: none build environment: distmod: debian81 distarch: x86_64 target_arch: x86_64
# mkdir /data/log # mkdir /data/db1 # nohup mongod --port 27020 --dbpath=/data/db1 --logpath=/data/log/rs0-1.log --logappend --fork --shardsvr --replSet=rs0 & # mkdir /data/db2 # nohup mongod --port 27021 --dbpath=/data/db2 --logpath=/data/log/rs0-2.log --logappend --fork --shardsvr --replSet=rs0 &
# mongo localhost:27020 > rs.initiate({_id: 'rs0', members: [{_id: 0, host: 'localhost:27020'}, {_id: 1, host: 'localhost:27021'}]}) > rs.isMaster() #查看主從關係
# mkdir /data/db3 # nohup mongod --port 27030 --dbpath=/data/db3 --logpath=/data/log/rs1-1.log --logappend --fork --shardsvr --replSet=rs1 & # mkdir /data/db4 # nohup mongod --port 27031 --dbpath=/data/db4 --logpath=/data/log/rs1-2.log --logappend --fork --shardsvr --replSet=rs1 &
# mongo localhost:27030 > rs.initiate({_id: 'rs1', members: [{_id: 0, host: 'localhost:27030'}, {_id: 1, host: 'localhost:27031'}]}) > rs.isMaster() #查看主從關係
# mkdir /data/conf1 # nohup mongod --port 27100 --dbpath=/data/conf1 --logpath=/data/log/conf-1.log --logappend --fork --configsvr --replSet=conf & # mkdir /data/conf2 # nohup mongod --port 27101 --dbpath=/data/conf2 --logpath=/data/log/conf-2.log --logappend --fork --configsvr --replSet=conf &
# mongo localhost:27100 > rs.initiate({_id: 'conf', members: [{_id: 0, host: 'localhost:27100'}, {_id: 1, host: 'localhost:27101'}]}) > rs.isMaster() #查看主從關係
# nohup mongos --port 40000 --configdb conf/localhost:27100,localhost:27101 --fork --logpath=/data/log/route.log --logappend &
# mongo localhost:40000 > use admin > db.runCommand({ addshard: 'rs0/localhost:27020,localhost:27021'}) > db.runCommand({ addshard: 'rs1/localhost:27030,localhost:27031'}) > db.runCommand({ enablesharding: 'test'}) > db.runCommand({ shardcollection: 'test.user', key: {name: 1}})
經過以上步驟操做,即可以直接鏈接 40000端口的mongdos享用分片及複製功能。git
基本大數據分佈式背景而生的產品,在分佈式集羣配置上仍是很簡單方便的。這裏僅僅是一個基本的操做演示,更多的功能還須要讀者去深刻了解。mongodb