mongodb高可用叢集搭建(分片+讀寫分離+負載均衡)
阿新 • • 發佈:2019-01-02
#分別在多臺機器上使用chiansun使用者登入 sudo yum install -y mongodb-org node-1 node-2 node-3 mongos mongos mongos 路由伺服器,定址 config config config 配置伺服器,儲存配置 shard1主 shard2主 shard3主 分片:儲存資料 shard2從 shard3從 shard1從 副本集:備份資料,可以配置讀寫分離(主負責寫,從負責同步資料和讀) shard3從 shard1從 shard2從 #分別在多臺機器上建立mongo config server對應的目錄 mkdir -p /mongo/config/{log,data,run} #分別在多臺機器上修改config server的配置檔案 cat >> /mongo/config/mongod.conf << EOF systemLog: destination: file logAppend: true path: /mongo/config/log/mongod.log storage: dbPath: /mongo/config/data journal: enabled: true processManagement: fork: true pidFilePath: /mongo/config/run/mongod.pid net: port: 27100 replication: replSetName: config sharding: clusterRole: configsvr EOF #啟動所有的mongo config server服務 mongod --config /mongo/config/mongod.conf #登入任意一臺配置伺服器,初始化配置副本集 mongo --port 27100 #建立配置 config = { #id名要和replSetName名保持一致 _id : "config", members : [ {_id : 0, host : "192.168.1.11:27100" }, {_id : 1, host : "192.168.1.12:27100" }, {_id : 2, host : "192.168.1.13:27100" } ] } #初始化副本集配置 rs.initiate(config) #檢視分割槽狀態 rs.status() #注意:其中,"_id" : "config"應與配置檔案中配置的 replicaction.replSetName 一致,"members" 中的 "host" 為三個節點的ip和port ------------------------------------------------------------------------------------------------ ------------------------------------------------------------------------------------------------ #配置第一個分片和副本集 #修改mongo shard1 server的配置檔案 mkdir -p /mongo/shard1/{log,data,run} #分別在多臺機器上修改shard1 server的配置檔案 cat >> /mongo/shard1/mongod.conf << EOF systemLog: destination: file logAppend: true path: /mongo/shard1/log/mongod.log storage: dbPath: /mongo/shard1/data journal: enabled: true processManagement: fork: true pidFilePath: /mongo/shard1/run/mongod.pid net: port: 27001 replication: replSetName: shard1 sharding: clusterRole: shardsvr EOF #啟動所有的shard1 server mongod --config /mongo/shard1/mongod.conf #登陸任意一臺shard1伺服器,初始化副本集 mongo --port 27001 #使用admin資料庫 use admin #定義副本集配置 config = { _id : "shard1", members : [ {_id : 0, host : "192.168.1.11:27001" }, {_id : 1, host : "192.168.1.12:27001" }, {_id : 2, host : "192.168.1.13:27001" } ] } #初始化副本集配置 rs.initiate(config); #檢視分割槽狀態 rs.status() ------------------------------------------------------------------------------------------------ ------------------------------------------------------------------------------------------------ #配置第二個分片和副本集 #修改mongo shard2 server的配置檔案 mkdir -p /mongo/shard2/{log,data,run} #分別在多臺機器上修改shard2 server的配置檔案 cat >> /mongo/shard2/mongod.conf << EOF systemLog: destination: file logAppend: true path: /mongo/shard2/log/mongod.log storage: dbPath: /mongo/shard2/data journal: enabled: true processManagement: fork: true pidFilePath: /mongo/shard2/run/mongod.pid net: port: 27002 replication: replSetName: shard2 sharding: clusterRole: shardsvr EOF #啟動所有的shard2 server mongod --config /mongo/shard2/mongod.conf #登陸任意一臺shard2伺服器,初始化副本集 mongo --port 27002 #使用admin資料庫 use admin #定義副本集配置 config = { _id : "shard2", members : [ {_id : 0, host : "192.168.1.12:27002" }, {_id : 1, host : "192.168.1.13:27002" }, {_id : 2, host : "192.168.1.11:27002" } ] } #初始化副本集配置 rs.initiate(config) #檢視分割槽狀態 rs.status() ------------------------------------------------------------------------------------------------ ------------------------------------------------------------------------------------------------ #配置第三個分片和副本集 #修改mongo shard3 server的配置檔案 mkdir -p /mongo/shard3/{log,data,run} #分別在多臺機器上修改shard3 server的配置檔案 cat >> /mongo/shard3/mongod.conf << EOF systemLog: destination: file logAppend: true path: /mongo/shard3/log/mongod.log storage: dbPath: /mongo/shard3/data journal: enabled: true processManagement: fork: true pidFilePath: /mongo/shard3/run/mongod.pid net: port: 27003 replication: replSetName: shard3 sharding: clusterRole: shardsvr EOF #啟動所有的shard3 server mongod --config /mongo/shard3/mongod.conf #登陸192.168.1.13的shard3伺服器,初始化副本集 mongo --port 27003 #使用admin資料庫 use admin #定義副本集配置 config = { _id : "shard3", members : [ {_id : 0, host : "192.168.1.13:27003" }, {_id : 1, host : "192.168.1.11:27003" }, {_id : 2, host : "192.168.1.12:27003" } ] } #初始化副本集配置 rs.initiate(config) #檢視分割槽狀態 rs.status() ------------------------------------------------------------------------------------------------ ------------------------------------------------------------------------------------------------ ------------------------------------------------------------------------------------------------ ######注意:啟動mongos是守候程序是因為/mongo/mongos/mongod.conf缺少了fork: true這個配置####### ------------------------------------------------------------------------------------------------ mkdir -p /mongo/mongos/{log,data,run} #新增mongs的配置檔案 cat >> /mongo/mongos/mongod.conf << EOF systemLog: destination: file logAppend: true path: /mongo/mongos/log/mongod.log processManagement: fork: true pidFilePath: /mongo/mongos/run/mongod.pid net: port: 27200 sharding: configDB: config/192.168.1.111:27100,192.168.1.12:27100,192.168.1.13:27100 EOF #注意,這裡configDB後面的config要與配置伺服器的_id保持一致 #啟動路由伺服器 mongos --config /mongo/mongos/mongod.conf #登入其中的一臺路由節點,手動啟用分片 mongo --port 27200 #新增分片到mongos sh.addShard("shard1/192.168.1.11:27001,192.168.1.12:27001,192.168.1.13:27001") sh.addShard("shard2/192.168.1.12:27002,192.168.1.13:27002,192.168.1.11:27002") sh.addShard("shard3/192.168.1.13:27003,192.168.1.11:27003,192.168.1.12:27003") #設定slave可讀 rs.slaveOk() ------------------------------------------------------------------------------------------------ ####沒有分片是因為沒有開啟分片規則#################### ------------------------------------------------------------------------------------------------ #對bike這個資料庫開啟分片功能 db.runCommand({"enablesharding":"bike"}) ##對bike資料庫下的users集合按id的hash進行分片 db.runCommand({"shardcollection":"bike.users","key":{_id:'hashed'}}) #啟動所有的config server mongod --config /mongo/config/mongod.conf #啟動所有的shard1 mongod --config /mongo/shard1/mongod.conf #啟動所有的shard2 mongod --config /mongo/shard2/mongod.conf #啟動所有的shard3 mongod --config /mongo/shard3/mongod.conf #啟動所有的mongos mongos --config /mongo/mongos/mongod.conf #關閉服務 mongod --shutdown --dbpath /mongo/shard3/data mongod --shutdown --dbpath /mongo/shard2/data mongod --shutdown --dbpath /mongo/shard1/data mongod --shutdown --dbpath /mongo/config/data