3臺機,每臺機5個實例,分別mongos 1 個,config server 1 個,shard server 3 個
IP:192.168.56.107(mongodb01) 實例:mongos:30000 shard1:40001(主節點) shard2:40002(仲裁節點) shard3:40003(副節點) config:27027
IP:192.168.56.105(mongodb02) 實例:mongos:30000 shard1:40001(副節點) shard2:40002(主節點) shard3:40003(仲裁節點) config:27028
IP:192.168.56.106(mongodb03) 實例:mongos:30000 shard1:40001(仲裁節點) shard2:40002(副節點) shard3:40003(主節點) config:27029linux
在192.168.56.107(mongodb01)上新建:mongodb
mkdir -p /mongodb01/{data,logs,apps,run} mkdir -p /mongodb01/data/shard{1,2,3} mkdir -p /mongodb01/data/config
在192.168.56.105(mongodb02)上新建:數據庫
mkdir -p /mongodb02/{data,logs,apps,run} mkdir -p /mongodb02/data/shard{1,2,3} mkdir -p /mongodb02/data/config
在192.168.56.106(mongodb03)上新建:bash
mkdir -p /mongodb03/{data,logs,apps,run} mkdir -p /mongodb03/data/shard{1,2,3} mkdir -p /mongodb03/data/config
使用root帳號修改配置,/mongodb01/apps/bin 用於存放mongodb的執行程序服務器
echo 'export PATH=$PATH:/mongodb01/apps/bin' >> /etc/profile source /etc/profile
使用root帳號修改配置,/mongodb01/apps/bin 用於存放mongodb的執行程序app
echo 'export PATH=$PATH:/mongodb02/apps/bin' >> /etc/profile source /etc/profile
使用root帳號修改配置,/mongodb01/apps/bin 用於存放mongodb的執行程序ide
echo 'export PATH=$PATH:/mongodb03/apps/bin' >> /etc/profile source /etc/profile
Mongodb01下:測試
groupadd -g 10001 mongodb useradd -u 10001 -g mongodb mongodb id mongodb passwd mongodb 輸入新的密碼:123 chown -R mongodb:mongodb /mongodb01 chmod -R 775 /mongodb01
Mongodb02下:3d
groupadd -g 10001 mongodb useradd -u 10001 -g mongodb mongodb id mongodb passwd mongodb 輸入新的密碼:123 chown -R mongodb:mongodb /mongodb02 chmod -R 775 /mongodb02
Mongodb03下:rest
groupadd -g 10001 mongodb useradd -u 10001 -g mongodb mongodb id mongodb passwd mongodb 輸入新的密碼:123 chown -R mongodb:mongodb /mongodb03 chmod -R 775 /mongodb03
切換用戶:
su mongodb
Mongodb01下:切換目錄:
cd /mongodb01/apps
下載文件
wget https://fastdl.mongodb.org/linux/mongodb-linux-x86_64-rhel70-4.4.1.tgz
解壓文件
tar -xf mongodb-linux-x86_64-rhel70-4.4.1.tgz mv mongodb-linux-x86_64-rhel70-4.4.1/bin ./ rm -rf mongodb-linux-x86_64-rhel70-4.4.1
Mongodb02下:切換目錄:
cd /mongodb02/apps
下載文件
wget https://fastdl.mongodb.org/linux/mongodb-linux-x86_64-rhel70-4.4.1.tgz
解壓文件
tar -xf mongodb-linux-x86_64-rhel70-4.4.1.tgz mv mongodb-linux-x86_64-rhel70-4.4.1/bin ./ rm -rf mongodb-linux-x86_64-rhel70-4.4.1
Mongodb03下:切換目錄:
cd /mongodb03/apps
下載文件
wget https://fastdl.mongodb.org/linux/mongodb-linux-x86_64-rhel70-4.4.1.tgz
解壓文件
tar -xf mongodb-linux-x86_64-rhel70-4.4.1.tgz mv mongodb-linux-x86_64-rhel70-4.4.1/bin ./ rm -rf mongodb-linux-x86_64-rhel70-4.4.1
Mongodb01下新建文件目錄:
mkdir -p /mongodb01/apps/conf/ vi /mongodb01/apps/conf/mongodb01-config.yml echo "systemLog: destination: file # 注意修改路徑 path: "/mongodb01/logs/mongodb01-config.log" logAppend: true storage: journal: enabled: true #注意修改路徑 dbPath: "/mongodb01/data/config" engine: wiredTiger wiredTiger: engineConfig: cacheSizeGB: 12 processManagement: fork: true pidFilePath: "/mongodb01/run/mongodb01-config.pid" net: bindIp: 0.0.0.0 ### 注意修改端口 port: 27027 setParameter: enableLocalhostAuthBypass: true replication: # 複製集名稱 replSetName: "mgconfig" sharding: #做爲配置服務 clusterRole: configsvr " >>/mongodb01/apps/conf/mongodb01-config.yml
Mongodb02下新建文件目錄:
>mkdir -p /mongodb02/apps/conf/ vi /mongodb02/apps/conf/mongodb02-config.yml echo "systemLog: destination: file #注意修改路徑 path: "/mongodb02/logs/mongodb02-config.log" logAppend: true storage: journal: enabled: true #注意修改路徑 dbPath: "/mongodb02/data/config" engine: wiredTiger wiredTiger: engineConfig: cacheSizeGB: 12 processManagement: fork: true pidFilePath: "/mongodb02/run/mongodb02-config.pid" net: bindIp: 0.0.0.0 #注意修改端口 port: 27028 setParameter: enableLocalhostAuthBypass: true replication: #複製集名稱 replSetName: "mgconfig" sharding: #做爲配置服務 clusterRole: configsvr " >>/mongodb02/apps/conf/mongodb02-config.yml
#Mongodb03下新建文件目錄:
mkdir -p /mongodb03/apps/conf/ vi /mongodb03/apps/conf/mongodb03-config.yml echo "systemLog: destination: file #注意修改路徑 path: "/mongodb03/logs/mongodb03-config.log" logAppend: true storage: journal: enabled: true #注意修改路徑 dbPath: "/mongodb03/data/config" engine: wiredTiger wiredTiger: engineConfig: cacheSizeGB: 12 processManagement: fork: true pidFilePath: "/mongodb03/run/mongodb03-config.pid" net: bindIp: 0.0.0.0 #注意修改端口 port: 27029 setParameter: enableLocalhostAuthBypass: true replication: #複製集名稱 replSetName: "mgconfig" sharding: #做爲配置服務 clusterRole: configsvr " >>/mongodb03/apps/conf/mongodb03-config.yml
在mongodb01下
vi /mongodb01/apps/conf/mongodb01-shard1.yml echo "systemLog: destination: file path: "/mongodb01/logs/mongodb01-shard1.log" logAppend: true storage: journal: enabled: true dbPath: "/mongodb01/data/shard1" processManagement: fork: true pidFilePath: "/mongodb01/run/mongodb01-shard1.pid" net: bindIp: 0.0.0.0 #注意修改端口 port: 40001 setParameter: enableLocalhostAuthBypass: true replication: #複製集名稱 replSetName: "shard1" sharding: #做爲分片服務 clusterRole: shardsvr" >>/mongodb01/apps/conf/mongodb01-shard1.yml ----- vi /mongodb01/apps/conf/mongodb01-shard2.yml echo "systemLog: destination: file path: "/mongodb01/logs/mongodb01-shard2.log" logAppend: true storage: journal: enabled: true dbPath: "/mongodb01/data/shard2" processManagement: fork: true pidFilePath: "/mongodb01/run/mongodb01-shard2.pid" net: bindIp: 0.0.0.0 #注意修改端口 port: 40002 setParameter: enableLocalhostAuthBypass: true replication: #複製集名稱 replSetName: "shard2" sharding: #做爲分片服務 clusterRole: shardsvr" >>/mongodb01/apps/conf/mongodb01-shard2.yml ----- vi /mongodb01/apps/conf/mongodb01-shard3.yml echo "systemLog: destination: file path: "/mongodb01/logs/mongodb01-shard3.log" logAppend: true storage: journal: enabled: true dbPath: "/mongodb01/data/shard3" processManagement: fork: true pidFilePath: "/mongodb01/run/mongodb01-shard3.pid" net: bindIp: 0.0.0.0 #注意修改端口 port: 40003 setParameter: enableLocalhostAuthBypass: true replication: #複製集名稱 replSetName: "shard3" sharding: #做爲分片服務 clusterRole: shardsvr" >>/mongodb01/apps/conf/mongodb01-shard3.yml ----- vi /mongodb01/apps/conf/mongodb01-route.yml echo "systemLog: destination: file #注意修改路徑 path: "/mongodb01/logs/mongodb01-route.log" logAppend: true processManagement: fork: true pidFilePath: "/mongodb01/run/mongodb01-route.pid" net: bindIp: 0.0.0.0 #注意修改端口 port: 30000 setParameter: enableLocalhostAuthBypass: true replication: localPingThresholdMs: 15 sharding: #關聯配置服務 configDB: mgconfig/192.168.56.107:27027,192.168.56.105:27028,192.168.56.106:27029 " >>/mongodb01/apps/conf/mongodb01-route.yml
在mongodb02下
vi /mongodb02/apps/conf/mongodb02-shard1.yml echo "systemLog: destination: file path: "/mongodb02/logs/mongodb02-shard1.log" logAppend: true storage: journal: enabled: true dbPath: "/mongodb02/data/shard1" processManagement: fork: true pidFilePath: "/mongodb02/run/mongodb02-shard1.pid" net: bindIp: 0.0.0.0 #注意修改端口 port: 40001 setParameter: enableLocalhostAuthBypass: true replication: #複製集名稱 replSetName: "shard1" sharding: #做爲分片服務 clusterRole: shardsvr" >>/mongodb02/apps/conf/mongodb02-shard1.yml ----- vi /mongodb02/apps/conf/mongodb02-shard2.yml echo "systemLog: destination: file path: "/mongodb02/logs/mongodb02-shard2.log" logAppend: true storage: journal: enabled: true dbPath: "/mongodb02/data/shard2" processManagement: fork: true pidFilePath: "/mongodb02/run/mongodb02-shard2.pid" net: bindIp: 0.0.0.0 #注意修改端口 port: 40002 setParameter: enableLocalhostAuthBypass: true replication: #複製集名稱 replSetName: "shard2" sharding: #做爲分片服務 clusterRole: shardsvr" >>/mongodb02/apps/conf/mongodb02-shard2.yml ---- vi /mongodb02/apps/conf/mongodb02-shard3.yml echo "systemLog: destination: file path: "/mongodb02/logs/mongodb02-shard3.log" logAppend: true storage: journal: enabled: true dbPath: "/mongodb02/data/shard3" processManagement: fork: true pidFilePath: "/mongodb02/run/mongodb02-shard3.pid" net: bindIp: 0.0.0.0 #注意修改端口 port: 40003 setParameter: enableLocalhostAuthBypass: true replication: #複製集名稱 replSetName: "shard3" sharding: #做爲分片服務 clusterRole: shardsvr" >>/mongodb02/apps/conf/mongodb02-shard3.yml ----- vi /mongodb02/apps/conf/mongodb02-route.yml echo "systemLog: destination: file #注意修改路徑 path: "/mongodb02/logs/mongodb02-route.log" logAppend: true processManagement: fork: true pidFilePath: "/mongodb02/run/mongodb02-route.pid" net: bindIp: 0.0.0.0 #注意修改端口 port: 30000 setParameter: enableLocalhostAuthBypass: true replication: localPingThresholdMs: 15 sharding: #關聯配置服務 configDB: mgconfig/192.168.56.107:27027,192.168.56.105:27028,192.168.56.106:27029 " >>/mongodb02/apps/conf/mongodb02-route.yml
在mongodb03下
vi /mongodb03/apps/conf/mongodb03-shard1.yml echo "systemLog: destination: file path: "/mongodb03/logs/mongodb03-shard1.log" logAppend: true storage: journal: enabled: true dbPath: "/mongodb03/data/shard1" processManagement: fork: true pidFilePath: "/mongodb03/run/mongodb03-shard1.pid" net: bindIp: 0.0.0.0 #注意修改端口 port: 40001 setParameter: enableLocalhostAuthBypass: true replication: #複製集名稱 replSetName: "shard1" sharding: #做爲分片服務 clusterRole: shardsvr" >>/mongodb03/apps/conf/mongodb03-shard1.yml ----- vi /mongodb03/apps/conf/mongodb03-shard2.yml echo "systemLog: destination: file path: "/mongodb03/logs/mongodb03-shard2.log" logAppend: true storage: journal: enabled: true dbPath: "/mongodb03/data/shard2" processManagement: fork: true pidFilePath: "/mongodb03/run/mongodb03-shard2.pid" net: bindIp: 0.0.0.0 #注意修改端口 port: 40002 setParameter: enableLocalhostAuthBypass: true replication: #複製集名稱 replSetName: "shard2" sharding: #做爲分片服務 clusterRole: shardsvr" >>/mongodb03/apps/conf/mongodb03-shard2.yml ----- vi /mongodb03/apps/conf/mongodb03-shard3.yml echo "systemLog: destination: file path: "/mongodb03/logs/mongodb03-shard3.log" logAppend: true storage: journal: enabled: true dbPath: "/mongodb03/data/shard3" processManagement: fork: true pidFilePath: "/mongodb03/run/mongodb03-shard3.pid" net: bindIp: 0.0.0.0 #注意修改端口 port: 40003 setParameter: enableLocalhostAuthBypass: true replication: #複製集名稱 replSetName: "shard3" sharding: #做爲分片服務 clusterRole: shardsvr" >>/mongodb03/apps/conf/mongodb03-shard3.yml ----- vi /mongodb03/apps/conf/mongodb03-route.yml echo "systemLog: destination: file #注意修改路徑 path: "/mongodb03/logs/mongodb03-route.log" logAppend: true processManagement: fork: true pidFilePath: "/mongodb03/run/mongodb03-route.pid" net: bindIp: 0.0.0.0 #注意修改端口 port: 30000 setParameter: enableLocalhostAuthBypass: true replication: localPingThresholdMs: 15 sharding: #關聯配置服務 configDB: mgconfig/192.168.56.107:27027,192.168.56.105:27028,192.168.56.106:27029 " >>/mongodb03/apps/conf/mongodb03-route.yml
啓動config服務(3臺機器執行相同操做,只是在不一樣的mongodb上啓動時須要修改mongodb0,此處只演示mongodb01上的配置部署)
在mongodb01下
cd /mongodb01/apps/conf/
mongod --config /mongodb01/apps/conf/mongodb01-config.yml
鏈接一個實例
mongo 192.168.56.107:27027(此處三臺機器一致)
初始化複製集
(這個 mgconfig 名字必定要和config 配置文件中 replSet 的名字一致)
```config={id:"mgconfig",members:[{id:0,host:"192.168.56.107:27027"},{id:1,host:"192.168.56.105:27028"},{id:2,host:"192.168.56.106:27029"},]}
rs.initiate(config)
檢查狀態rs.status()
複製集配完後,可能狀態不會立刻改變(可能都是secondary),過幾秒就會自動更新 
啓動3臺shard1實例
cd /mongodb01/apps/conf/ mongod --config /mongodb01/apps/conf/mongodb01-shard1.yml
鏈接一個實例
mongo 192.168.56.107:40001(3臺機器一致)
建立複製集
use admin config={_id:"shard1",members:[{_id:0,host:"192.168.56.107:40001",priority:2},{_id:1,host:"192.168.56.105:40001",priority:1},{_id:2,host:"192.168.56.106:40001",arbiterOnly:true},]}
這個 shard1名字必定要和 shard1配置文件中 replSet 的名字一致
初始化複製集
rs.initiate(config)
檢查狀態
rs.status()
複製集配完後,可能狀態不會立刻改變(可能都是secondary),過幾秒就會自動更新
啓動3臺shard2實例
cd /mongodb01/apps/conf/
mongod --config /mongodb01/apps/conf/mongodb01-shard2.yml
鏈接第二個節點建立複製集
爲何是鏈接第二個,由於規劃的shard2 的主節點是105:40002,仲裁節點不能寫數據,因此這裏不能連107,要連105
mongo 192.168.56.105:40002(三臺機器一致)
建立複製集
use admin config={_id:"shard2",members:[{_id:0,host:"192.168.56.107:40002",arbiterOnly:true},{_id:1,host:"192.168.56.105:40002",priority:2}, {_id:2,host:"192.168.56.106:40002",priority:1},]}
這個 shard2名字必定要和 shard2配置文件中 replSet 的名字一致
初始化複製集
rs.initiate(config)
檢查狀態
rs.status()
複製集配完後,可能狀態不會立刻改變(可能都是secondary),過幾秒就會自動更新
啓動3臺shard3實例
cd /mongodb01/apps/conf/
mongod --config /mongodb01/apps/conf/mongodb01-shard3.yml
鏈接第三個節點建立複製集
爲何是鏈接第三個,由於規劃的shard3 的主節點是106:40003
mongo 192.168.56.106:40003(3臺機器一致)
建立複製集
use admin config={_id:"shard3",members:[{_id:0,host:"192.168.56.107:40003",priority:1},{_id:1,host:"192.168.56.105:40003",arbiterOnly:true}, {_id:2,host:"192.168.56.106:40003",priority:2},]}
這個 shard3名字必定要和 shard3配置文件中 replSet 的名字一致
初始化複製集
rs.initiate(config)
檢查狀態
rs.status()
複製集配完後,可能狀態不會立刻改變(可能都是secondary),過幾秒就會自動更新
登錄路由節點
cd /mongodb01/apps/conf/ mongos --config /mongodb01/apps/conf/mongodb01-route.yml mongo 192.168.56.107:30000 use admin sh.addShard("shard1/192.168.56.107:40001,192.168.56.105:40001,192.168.56.106:40001") sh.addShard("shard2/192.168.56.107:40002,192.168.56.105:40002,192.168.56.106:40002") sh.addShard("shard3/192.168.56.107:40003,192.168.56.105:40003,192.168.56.106:40003")
檢查狀態
sh.status()
編寫服務腳本,便於啓動和中止集羣
#!/bin/bash #mongodb script takes care of starting ||stopping ||reload mongom #chkconfig:- 80 15 #description: Mongo database #precessname: mongom user=mongodb #source function library #source /etc/init.d/functions #/mongodb/apps/mongodb/bin #the localcation of configfile config_configfile="/mongodb01/apps/conf/mongodb01-config.yml" router_configfile="/mongodb01/apps/conf/mongodb01-route.yml" shard1_configfile="/mongodb01/apps/conf/mongodb01-shard1.yml" shard2_configfile="/mongodb01/apps/conf/mongodb01-shard2.yml" shard3_configfile="/mongodb01/apps/conf/mongodb01-shard3.yml" #the options of start a mongodb server start_config_options=" --config $config_configfile" stop_config_options=" --shutdown --dbpath /mongodb01/data/config" start_router_options=" --config $router_configfile" start_shard1_options=" --config $shard1_configfile" stop_shard1_options=" --shutdown --dbpath /mongodb01/data/shard1" start_shard2_options=" --config $shard2_configfile" stop_shard2_options=" --shutdown --dbpath /mongodb01/data/shard2" start_shard3_options=" --config $shard3_configfile" stop_shard3_options=" --shutdown --dbpath /mongodb01/data/shard3" #the localcation of mongod mongod="/mongodb01/apps/bin/mongod" #the localcation of mongos mongos="/mongodb01/apps/bin/mongos" #where to lockfile config_lockfile="/mongodb01/data/config/mongod.lock" shard1_lockfile="/mongodb01/data/shard1/mongod.lock" shard2_lockfile="/mongodb01/data/shard2/mongod.lock" shard3_lockfile="/mongodb01/data/shard3/mongod.lock" #where to pidfile config_pidfile="/mongodb01/run/mongodb01-config.pid" router_pidfile="/mongodb01/run/mongodb01-route.pid" shard1_pidfile="/mongodb01/run/mongodb01-shard1.pid" shard2_pidfile="/mongodb01/run/mongodb01-shard2.pid" shard3_pidfile="/mongodb01/run/mongodb01-shard3.pid" #function of start config server function start-config(){ #print the tips echo -n $"Starting mongod of config_server:" $mongod $start_config_options #get the result RETVAL=$? if [ $RETVAL -eq 0 ];then touch $config_lockfile fi} #function of stop config server function stop-config(){ #print the tips echo $"stopping mongod of config_server:" $mongod $stop_config_options RETVAL=$? if [ $RETVAL -eq 0 ];then rm -f $config_lockfile rm -f $config_pidfile fi} #function of start router server function start-router(){ #print the tips echo $"Starting mongod of router_server:" $mongos $start_router_options } #function of stop router server function stop-router(){ #print the tips echo $"stopping mongod of router_server:" kill `cat $router_pidfile` RETVAL=$? if [ $RETVAL -eq 0 ];then rm -f $router_pidfile fi } #function of start shard1 function start-shard1(){ #print the tips echo $"Starting mongod of shard1_server:" $mongod $start_shard1_options #get the result RETVAL=$? if [ $RETVAL -eq 0 ];then touch $shard1_lockfile fi } #function of stop shard1 function stop-shard1(){ #print the tips echo $"stopping mongod of shard1_server:" $mongod $stop_shard1_options RETVAL=$? if [ $RETVAL -eq 0 ];then rm -f $shard1_lockfile rm -f $shard1_pidfile fi } #function of start shard2 function start-shard2(){ #print the tips echo -n $"Starting mongod of shard2_server:" $mongod $start_shard2_options #get the result RETVAL=$? if [ $RETVAL -eq 0 ];then touch $shard2_lockfile fi } #function of stop shard2 function stop-shard2(){ #print the tips echo $"stopping mongod of shard2_server:" $mongod $stop_shard2_options RETVAL=$? if [ $RETVAL -eq 0 ];then rm -f $shard2_lockfile rm -f $shard2_pidfile fi } #function of start shard3 function start-shard3(){ #print the tips echo -n $"Starting mongod of shard3_server:" $mongod $start_shard3_options #get the result RETVAL=$? if [ $RETVAL -eq 0 ];then touch $shard3_lockfile fi} #function of stop shard3 function stop-shard3(){ #print the tips echo $"stopping mongod of shard3_server:" $mongod $stop_shard3_options RETVAL=$? if [ $RETVAL -eq 0 ];then rm -f $shard3_lockfile rm -f $shard3_pidfile fi } start(){ start-config echo '' start-shard1 echo '' start-shard2 echo '' start-shard3 echo '' start-router } stop(){ stop-shard1 echo 'stop-shard1 ok' stop-shard2 echo 'stop-shard2 ok' stop-shard3 echo 'stop-shard3 ok' stop-router echo 'stop-router ok' stop-config echo 'stop-config ok' } RETVAL=0 case "$1" in start) start ;; stop) stop ;; start-all) start ;; stop-all) stop ;; start-shard3) start-shard3 ;; stop-shard3) stop-shard3 ;; start-shard2) start-shard2 ;; stop-shard2) stop-shard2 ;; start-shard1) start-shard1 ;; stop-shard1) stop-shard1 ;; start-config) start-config ;; stop-config) stop-config ;; start-router) start-router ;; restart-config |reload-config |force-reload-config) stop-config start-config ;; restart-router |reload-router |force-reload-router) stop-router start-router ;; restart-shard1 |reload-shard1 |force-reload-shard1) stop-shard1 start-shard1 ;; restart-shard2 |reload-shard2 |force-reload-shard2) stop-shard2 start-shard2 ;; restart-shard3 |reload-shard3 |force-reload-shard3) stop-shard3 start-shard3 ;; restart-all |reload-all |force-reload-all) stop start ;; conderstart) [ -f $lockfile ] && restart || : [ -f $lockfile ] && restart || : ;; status) status $mongod RETVAL=$? ;; *) echo "Usage: $0 {start-*|stop-*|restart-*|status|reload-*|force-reload-*|condrestart(* in {all,config,router,shard1,shard2,shard3})}" esac exit $RETVAL
模擬寫入數據
在tydb庫的tyuser表中循環寫入6萬條數據
mongo 192.168.56.107:30000
use tydb
show collections
for(i=1;i<=60000;i++){db.tyuser.insert({"id":i,"name":"ty"+i})}
啓用數據庫分片
sh.enableSharding("tydb")
建立的索引
db.tyuser.createIndex({"id":1})
啓用表分片
sh.shardCollection(」tydb.tyuser",{"id":1})
查看分片狀況
sh.status()
開啓平衡器
use admin
sh.startBalance()
關閉平衡器
use admin
sh.stopBalancer()
查看是否關閉
返回flase表示平衡器已關閉,還須要查詢均衡器正在運行的 狀況
sh.getBalancerState()