2臺節點機器+1臺投票機器(由於偶數臺,湊夠奇數臺,由於選舉時,多與半數才能成功選舉,若是2臺,死一臺,只剩一臺,少於1/2,是不能正常進行選舉的,因此要加一臺投票機器,(其實不必定非奇數臺,確保死一臺仍有半數以上的機器就行,不過建議奇數臺))linux
10.13.2.51:27017mongodb
10.13.2.52:27017shell
10.13.2.151:27017(arbiters)數據庫
1臺mongos機器app
10.13.2.112:27107fetch
3臺config機器ui
10.13.2.91:27109spa
10.13.2.92:27109.net
10.13.2.102:27109日誌
先安裝一個replica set(這裏也能夠先安裝config和mongos機器,再安裝replica set,順序無所謂)
安裝config機器
安裝mongos機器
tar zxvf mongodb-linux-x86_64-3.0.3.tgz mv mongodb-linux-x86_64-3.0.3 mongodb chown -R mongodb.mongodb mongodb # 此處mongodb用戶運行,沒有的話useradd mongodb添加
mkdir log conf db db/rs0
systemLog: destination: file path: "/home/data/mongodb/log/rs0.log" quiet: true logAppend: true # 追加寫 timeStampFormat: iso8601-utc verbosity: 0 # 日誌級別 # component: # query: # verbosity: 2 storage: dbPath: "/home/data/mongodb/db/rs0" engine: "wiredTiger" # 採用wiredTiger引擎 wiredTiger: engineConfig: cacheSizeGB: 8 collectionConfig: blockCompressor: snappy # 注意snappy沒安裝的話要安裝 directoryPerDB: true # 是否每一個數據庫一個文件夾 indexBuildRetry: false # 重啓等狀況下是否重建索引 journal: enabled: true processManagement: fork: true # 後臺運行 pidFilePath: "/home/data/mongodb/db/rs0/mongodb.pid" net: # bindIp: 192.168.11.52 port: 27017 # 對外的端口 http: enabled: false RESTInterfaceEnabled: false operationProfiling: slowOpThresholdMs: 100 mode: "slowOp" # 記錄慢查詢 replication: oplogSizeMB: 100 replSetName: "rs0" # 同一個replica set名稱要同樣 secondaryIndexPrefetch: "all" sharding: clusterRole: "shardsvr" # 做爲分片的一員
# 除了一些文件名的更改外,另外把journal.enabled: false,其他都同樣 systemLog: destination: file path: "/home/data/mongodb/log/arb0.log" quiet: true logAppend: true timeStampFormat: iso8601-utc verbosity: 0 # component: # query: # verbosity: 2 storage: dbPath: "/home/data/mongodb/db/arb0" engine: "wiredTiger" wiredTiger: engineConfig: cacheSizeGB: 1 collectionConfig: blockCompressor: snappy directoryPerDB: true indexBuildRetry: false journal: enabled: false processManagement: fork: true pidFilePath: "/home/data/mongodb/db/arb0/mongodb.pid" net: # bindIp: 192.168.11.52 port: 27017 http: enabled: false RESTInterfaceEnabled: false operationProfiling: slowOpThresholdMs: 100 mode: "slowOp" replication: oplogSizeMB: 100 replSetName: "rs0" secondaryIndexPrefetch: "all" sharding: clusterRole: "shardsvr"
bin/mongod -f conf/mongodb.conf
bin/mongo # 默認鏈接本機的27017端口 config = {_id:"rs0", members: [ {_id: 0, host:"10.13.2.51:27017", priority:2}, {_id: 1, host:"10.13.2.52:27017", priority:1}, {_id: 2, host:"10.13.2.151:27017", arbiterOnly:true} ] }; # 此處注意arbiterOnly,意指投票節點 # priority,選舉時優先級,越大優先級越高 rs.initiate(config)
至此replica set配好了一個
下面安裝config機器
mkdir log conf configdb
systemLog: destination: file path: "/home/data/mongodb/log/config.log" quiet: true logAppend: true timeStampFormat: iso8601-utc verbosity: 0 # component: # query: # verbosity: 2 storage: dbPath: "/home/data/mongodb/configdb" engine: "wiredTiger" wiredTiger: engineConfig: cacheSizeGB: 8 collectionConfig: blockCompressor: snappy directoryPerDB: true indexBuildRetry: false journal: enabled: true processManagement: fork: true pidFilePath: "/home/data/mongodb/configdb/mongodb.pid" net: # bindIp: 192.168.11.52 port: 27019 http: enabled: false RESTInterfaceEnabled: false sharding: clusterRole: "configsvr"
bin/mongod -f conf/mongodb.conf
至此,config機器安裝完畢
下面安裝mongos機器
mkdir log conf
systemLog: destination: file path: "/home/data/mongodb/log/mongos.log" quiet: true logAppend: true timeStampFormat: iso8601-utc processManagement: fork: true net: port: 27017 sharding: configDB: 10.13.2.91:27019,10.13.2.92:27019,10.13.2.102:27019
bin/mongos -f conf/mongos.conf
bin/mongo 10.13.2.112:27017 sh.addShard( "rs0/10.13.2.51:27017" ) # 2.0.3版本之前要列出所有rs0裏的組員,以後的則沒必要了。添加單個mongod則直接sh.addShard( "mongodb0.example.net:27017" ),前面沒有replica set的名字 sh.status() # 查看分片狀態
至此複製集+分片集羣安裝完畢(這裏只加了一個分片)
後續加分片的話,步驟就是:
建立一個replica set
鏈接mongos,sh.addShard( "rs1/xxxxxx:27017" ), 可實現擴容,實現負載等