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 server
mongos -f /usr/local/mongodb/conf/mongos.conf

4、啟用分片

目前搭建了mongodb配置服務器、路由服務器,各個分片服務器,不過應用程序連接到mongos路由服務器並不能使用分片機制,還需要在程序里設置分片配置,讓分片生效。
登陸任意一台mongos
mongo --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.conf
mongod -f /usr/local/mongodb/conf/shard1.conf
mongod -f /usr/local/mongodb/conf/shard2.conf
mongod -f /usr/local/mongodb/conf/shard3.conf
mongod -f /usr/local/mongodb/conf/mongos.conf

關閉時,直接killall殺掉所有進程
killall mongod
killall mongos


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM