linux下Mongodb叢集搭建:分片+副本集
三臺伺服器 192.168.1.40/41/42
安裝包 mongodb-linux-x86_64-amazon2-4.0.1.tgz
伺服器40 | 伺服器41 | 伺服器42 |
mongos | mongos | mongos |
config server | config server | config server |
shard server1 主節點 | shard server1副節點 | shard server1仲裁 |
shard server2 仲裁 | shard server2 主節點 | shard server2 副節點 |
shard server3副節點 | shard server3仲裁 | shard server3主節點 |
埠分配:
mongos:28000
config:28001
shard1:28011
shard2:28012
shard3:28013
主要模組以及配置檔案
1、config server 配置伺服器
vi /usr/local/mongodb/conf/config.conf
40伺服器配置檔案pidfilepath = /usr/local/mongodb/config/log/configsrv.pid dbpath = /mydata/mongodb/config/data logpath = /usr/local/mongodb/config/log/congigsrv.log logappend= true bind_ip = 192.168.29.40 port = 28001 fork = true #以守護程序的方式執行MongoDB,建立伺服器程序 #declare this is a config db of a cluster; configsvr = true #副本集名稱 replSet=configs #設定最大連線數 maxConns=20000
41伺服器配置檔案pidfilepath = /usr/local/mongodb/config/log/configsrv.pid dbpath = /mydata/mongodb/config/data logpath = /usr/local/mongodb/config/log/congigsrv.log logappend= true bind_ip = 192.168.29.41 port = 28001 fork = true #以守護程序的方式執行MongoDB,建立伺服器程序 #declare this is a config db of a cluster; configsvr = true #副本集名稱 replSet=configs #設定最大連線數 maxConns=20000
42伺服器配置檔案
pidfilepath = /usr/local/mongodb/config/log/configsrv.pid dbpath = /mydata/mongodb/config/data logpath = /usr/local/mongodb/config/log/congigsrv.log logappend = true bind_ip = 192.168.29.42 port = 28001 fork = true #以守護程序的方式執行MongoDB,建立伺服器程序 #declare this is a config db of a cluster; configsvr = true #副本集名稱 replSet=configs #設定最大連線數 maxConns=20000
啟動三臺伺服器的config server mongod -f /usr/local/mongodb/conf/config.conf 登入任意一臺配置伺服器,初始化配置副本集 #連線 mongo --port 21000 #config變數 config = { ... _id : "configs", ... members : [ ... {_id : 0, host : "192.168.1.40:28001" }, ... {_id : 1, host : "192.168.1.41:28001" }, ... {_id : 2, host : "192.168.1.42:28001" } ... ] ... } #初始化副本集 rs.initiate(config)
2 配置分片副本集(三臺機器)
配置檔案vi /usr/local/mongodb/conf/shard1.conf
#配置檔案內容 #——————————————– pidfilepath = /usr/local/mongodb/shard1/log/shard1.pid dbpath = /mydata/mongodb/shard1/data logpath = /usr/local/mongodb/shard1/log/shard1.log logappend = true bind_ip = 192.168.29.40 port = 28011 fork = true #開啟web監控 #httpinterface=true #rest=true #副本集名稱 replSet=shard1 #declare this is a shard db of a cluster; shardsvr = true #設定最大連線數 maxConns=20000
#配置檔案內容 #——————————————– pidfilepath = /usr/local/mongodb/shard1/log/shard1.pid dbpath = /mydata/mongodb/shard1/data logpath = /usr/local/mongodb/shard1/log/shard1.log logappend = true bind_ip = 192.168.29.41 port = 28011 fork = true #開啟web監控 #httpinterface=true #rest=true #副本集名稱 replSet=shard1 #declare this is a shard db of a cluster; shardsvr = true #設定最大連線數 maxConns=20000
#配置檔案內容
#——————————————–
pidfilepath = /usr/local/mongodb/shard1/log/shard1.pid
dbpath = /mydata/mongodb/shard1/data
logpath = /usr/local/mongodb/shard1/log/shard1.log
logappend = true
bind_ip = 192.168.29.42
port = 28011
fork = true
#開啟web監控
#httpinterface=true
#rest=true
#副本集名稱
replSet=shard1
#declare this is a shard db of a cluster;
shardsvr = true
#設定最大連線數
maxConns=20000
vi /usr/local/mongodb/conf/shard2.conf
vi /usr/local/mongodb/conf/shard2.conf (shard2和shard3就是上面配置檔案相應地方改為2和3就可以了)
3、配置路由伺服器 mongos
先啟動配置伺服器和分片伺服器,後啟動路由例項:(三臺機器)vi /usr/local/mongodb/conf/mongos.conf
#內容 pidfilepath = /usr/local/mongodb/mongos/log/mongos.pid logpath = /usr/local/mongodb/mongos/log/mongos.log logappend = true bind_ip = 0.0.0.0 port = 28000 fork = true #監聽的配置伺服器,只能有1個或者3個 configs為配置伺服器的副本集名字 configdb = configs/192.168.1.40:28001,192.168.1.41:28001,192.168.1.42:28001 #設定最大連線數 maxConns=20000
啟動三臺伺服器的mongos servermongos -f /usr/local/mongodb/conf/mongos.conf
4、啟用分片
目前搭建了mongodb配置伺服器、路由伺服器,各個分片伺服器,不過應用程式連線到mongos路由伺服器並不能使用分片機制,還需要在程式裡設定分片配置,讓分片生效。登陸任意一臺mongosmongo --port 28000#使用admin資料庫use admin#串聯路由伺服器與分配副本集sh.addShard("shard1/192.168.1.40:28011,192.168.1.41:28011,192.168.1.42:28011")sh.addShard("shard2/192.168.1.40:28012,192.168.1.41:28012,192.168.1.42:28012")sh.addShard("shard3/192.168.1.40:28013,192.168.1.41:28013,192.168.1.42:28013")#檢視叢集狀態sh.status()
5、測試
目前配置服務、路由服務、分片服務、副本集服務都已經串聯起來了,但我們的目的是希望插入資料,資料能夠自動分片。連線在mongos上,準備讓指定的資料庫、指定的集合分片生效。 #指定testdb分片生效 db.runCommand( { enablesharding :"testdb"}); #指定資料庫裡需要分片的集合和片鍵 db.runCommand( { shardcollection : "testdb.table1",key : {id: 1} } ) 我們設定testdb的 table1 表需要分片,根據 id 自動分片到 shard1 ,shard2,shard3 上面去。要這樣設定是因為不是所有mongodb 的資料庫和表 都需要分片! 測試分片配置結果 mongo 127.0.0.1:28000 #使用testdb use testdb; #插入測試資料 for (var i = 1; i <= 100000; i++) db.table1.save({id:i,"test1":"testval1"}); #檢視分片情況如下,部分無關資訊省掉了 db.table1.stats(); { "sharded" : true, "ns" : "testdb.table1", "count" : 100000, "numExtents" : 13, "size" : 5600000, "storageSize" : 22372352, "totalIndexSize" : 6213760, "indexSizes" : { "_id_" : 3335808, "id_1" : 2877952 }, "avgObjSize" : 56, "nindexes" : 2, "nchunks" : 3, "shards" : { "shard1" : { "ns" : "testdb.table1", "count" : 42183, "size" : 0, ... "ok" : 1 }, "shard2" : { "ns" : "testdb.table1", "count" : 38937, "size" : 2180472, ... "ok" : 1 }, "shard3" : { "ns" : "testdb.table1", "count" :18880, "size" : 3419528, ... "ok" : 1 } }, "ok" : 1 }
6、後期運維
啟動關閉mongodb的啟動順序是,先啟動配置伺服器,在啟動分片,最後啟動mongos.mongod -f /usr/local/mongodb/conf/config.confmongod -f /usr/local/mongodb/conf/shard1.confmongod -f /usr/local/mongodb/conf/shard2.confmongod -f /usr/local/mongodb/conf/shard3.confmongod -f /usr/local/mongodb/conf/mongos.conf
關閉時,直接killall殺掉所有程序killall mongodkillall mongos