mongodb集羣半自動化部署

 

2.3.1 初始化操做

2.3.1.一、openssl必備版本

查看全部機器的 openssl的版本,若是版本低於OpenSSL 1.0.1e-fips 11 Feb 2013,請安裝openssl-devel.x86_64linux

具體操做命令以下mongodb

# openssl version //查看openssl當前版本服務器

# yum install openssl-devel.x86_64 //從庫裏面安裝依賴包app

# rpm -e openssl-devel-1.0.0-27.el6.x86_64 //卸載相關依賴包(若是安裝的opendevel版本是1.0.0)spa

# openssl rpm -Uvh openssl-1.0.1e-48.el6.x86_64.rpm //升級依賴包日誌

2.3.1.2 服務器時間同步

全部安裝mongodb的linux系統時間不能相差太遠,相差太遠會報錯,以下:server

# date//查看時間執行腳本索引

# date -s '2016-09-18 10:48:00' //修改全部mongodb的時間ip

2.3.1.3關閉防火牆

# server iptables status //產看防火牆狀態                                                 ssl

# service iptables stop //關閉防火牆

2.3.二、安裝config

注:(1)腳本執行時,Server1,Server2,Server3分別用對應服務器的ip地址替換,本文檔涉及到的的腳本執行,都需這樣替換

(2)如下文檔中要執行的腳本涉及到的$MONGODB_PATH爲mongodb根目錄,$MONGODB_DATA_CONFIGSVR_DIR爲數據存儲目錄,須要先自定義建立好,腳本執行前,須要用對應的目錄替換$MONGODB_DATA_CONFIGSVR_DIR

(3)如下文檔中要執行的腳本涉及到$MONGODB_LOG_MONGOS_DIR爲日誌文件存儲目錄,須要先自定義建立好,腳本執行前,須要用對應的目錄替換$MONGODB_LOG_MONGOS_DIR

(4)如下文檔中要執行的腳本涉及到$MONGODB_DATA_SHARD1_DIR爲SHARD數據文件存儲目錄,須要先自定義建立好,腳本執行前,須要用對應的目錄替換$MONGODB_DATA_SHARD1_DIR

(5)如下文檔中要執行的腳本涉及到$MONGODB_LOG_SHARD1_DIR爲SHARD日誌文件存儲目錄,須要先自定義建立好,腳本執行前,須要用對應的目錄替換$MONGODB_LOG_MONGOS_DIR

(6)$MONGODB_DATA_SHARD2_DIR和$MONGODB_LOG_SHARD2_DIR;$MONGODB_DATA_SHARD3_DIR和$MONGODB_LOG_SHARD3_DIR同以上第五步的說明

在Server1,Server2,Server3分別執行如下腳本命令

$MONGODB_PATH/bin/mongod --configsvr --port 21000 --dbpath $MONGODB_DATA_CONFIGSVR_DIR –logpath

 

2.3.三、安裝路由mongos

路由mongs跟config共用三臺機器Server1,Server2,Server3,也能夠分開機器部署;在Server1,Server2,Server3分別執行以下腳本命令:

$MONGODB_PATH/bin/mongos-configdb Server1:21000,Server2:21000,Server3:21000 -port 20000 -logpath $MONGODB_LOG_MONGOS_DIR/mongos.log -logappend -fork

2.3.四、安裝Shards

Shard能夠跟mongos,config分開機器部署

在Server1,Server2,Server3分別執行下面三條腳本

一、

$MONGODB_PATH/bin/mongod --shardsvr --replSet shard1  --port 22001 --dbpath $MONGODB_DATA_SHARD1_DIR --logpath $MONGODB_LOG_SHARD1_DIR/${shardname}.log --fork --nojournal --oplogSize 2000

 

 

二、

$MONGODB_PATH/bin/mongod --shardsvr --replSet shard2  --port 22002 --dbpath $MONGODB_DATA_SHARD2_DIR --logpath $MONGODB_LOG_SHARD2_DIR/${shardname}.log --fork --nojournal --oplogSize 2000

 

三、

$MONGODB_PATH/bin/mongod --shardsvr --replSet shard3  --port 22003 --dbpath $MONGODB_DATA_SHARD3_DIR --logpath $MONGODB_LOG_SHARD3_DIR/${shardname}.log --fork --nojournal --oplogSize 2000

2.3.五、定義分片副本集

任意一臺shard片組的機器,執行如下腳本;priority爲分片權重,默認1,值越大權重越大,priority值最大的是主分片,arbiterOnly:true參數爲仲裁;若是一組shard只有一主一從,必需要有個仲裁,主從才能自動切換,實現高可用

$MONGODB_PATH/bin/mongo Server1:22001 <<EOF

use admin

config = { _id:"shard1", members:[ {_id:0,host:"Server1:22001",priority:2},{_id:1,host:" Server2:22001"},{_id:2,host:"Server3:22001"} ] };

rs.initiate(config);

EOF

 

$MONGODB_PATH/bin/mongo Server1:22002 <<EOF

use admin

config = { _id:"shard2", members:[ {_id:0,host:" Server1:22002"},{_id:1,host:"Server2:22002",priority:2},{_id:2,host:"Server3:22002"} ] };

rs.initiate(config);

EOF

$MONGODB_PATH/bin/mongo Server1:22003 <<EOF

use admin

config = { _id:"shard3", members:[ {_id:0,host:" Server1:22003"},{_id:1,host:"Server2:22003"},{_id:2,host:"Server3:22003",priority:2} ] };

rs.initiate(config);

EOF

 

 

2.3.六、分片串聯路由

目前搭建了mongodb配置服務器、路由服務器,各個分片服務器,不過應用程序鏈接到 mongos 路由服務器並不能使用分片機制,還須要在程序裏設置分片配置,讓分片生效。任意一臺路由mongos執行如下腳本,串聯路由跟副本集

$MONGODB_PATH/bin/mongo Server1:20000 <<EOF

use admin

db.runCommand( { addshard : "shard1/Server1:22001,Server2:22001,Server3:22001"});

db.runCommand( { addshard : "shard2/Server1:22002,Server2:22002,Server3:22002"});

db.runCommand( { addshard : "shard3/Server1:22003,Server2:22003,Server3:22003"});

db.runCommand( { listshards : 1 } );

EOF

 

2.3.七、分片策略生成

分片須要指定表,且必須指定按照維度分片,維度必須建索引

 

# $MONGODB_PATH/bin/mongo Server1:20000 <<EOF//鏈接路由mongos

# use admin//使用admin

# db.runCommand( { enablesharding :"testdb"});執行腳本,使分片在庫testdb生效

# testdb//使用testdb

 

# db.testTable.ensureIndex({ "row1" : 1 },{ "name" : "row1_ind" });//生成索引

# db.testTable.ensureIndex({ "row2" : "1" },{ "name" : "row2_ind" });//生成索引

# db.testTable.ensureIndex({ "row3" : "hashed" },{ "name" : "row3_ind" });//生成索引

# use admin //使用

 

 

 

 

分片策略一下選擇一種

使用有序分片策略

# db.runCommand( { shardcollection : "testdb.testTable",key : {row1: 1,row2: 1 } } )

 

上面這個片鍵經過字段row1的值進行數據分配。若是這個有大量相同的值,則會根據row2字段的值對塊進行分裂。

使用散列分片策略

# db.runCommand( { shardcollection : "testdb.testTable",key : {row3: 'hashed'} } )

 

上面這個片鍵經過字段row3的散列值進行數據分配。MongoDB計算row3字段的散列值做爲散列索引,它將提供集羣中文檔的均勻分佈。

相關文章
相關標籤/搜索