首頁 > 軟體

mongodb linux下叢集搭建過程

2022-02-21 13:06:17

mongodb的叢集結構如上圖

網上有個mongo3.0的叢集例子:

https://www.jb51.net/article/191388.htm

router提供入口,mongo使用者端通過router連入叢集(本例只設定一個route叢集)

Config Servers輔助記錄資料分片(一個叢集)

Shard為資料分片叢集(本例中設定兩個,用於驗證分片),

本例中,為每個叢集(shard config)三個mongo範例

config與shard為同一個型別的程序mongod

route則為mongos程序

下載mongo二進位制壓縮包

https://fastdl.mongodb.org/linux/mongodb-linux-x86_64-rhel70-5.0.6.tgz

解壓可以看到

用openssl生成一個key,用於mongo叢集內部資料通迅

openssl rand -base64 123 > keyfile

mongod設定為件(config與shard通用)

mongo_node.conf:

storage:
    engine: wiredTiger
    directoryPerDB: true
    journal:
        enabled: true
 
systemLog:
    destination: file
    logAppend: true
operationProfiling:
  slowOpThresholdMs: 10000
replication:
    oplogSizeMB: 10240
processManagement:
    fork: true
security:
    authorization: "disabled"

mongos的組態檔(即圖中的route)

mongos.conf:

systemLog:
    destination: file
    logAppend: true
 
processManagement:
    fork: true

啟動config叢集(3個mongod程序)

WORK_DIR=/home/???/go/mongodb/mongo_test
 
KEYFILE=$WORK_DIR/key/keyfile
cat $KEYFILE
CONFFILE=$WORK_DIR/conf/mongo_node.conf
cat $CONFFILE
MONGOD=mongod
echo $MONGOD
$MONGOD --port 26001 --bind_ip_all --configsvr --replSet configReplSet --keyFile $KEYFILE --dbpath $WORK_DIR/config_cluster/conf_n1/data --pidfilepath $WORK_DIR/config_cluster/conf_n1/db.pid --logpath $WORK_DIR/config_cluster/conf_n1/db.log --config $CONFFILE 
$MONGOD --port 26002 --bind_ip_all --configsvr --replSet configReplSet --keyFile $KEYFILE --dbpath $WORK_DIR/config_cluster/conf_n2/data --pidfilepath $WORK_DIR/config_cluster/conf_n2/db.pid --logpath $WORK_DIR/config_cluster/conf_n2/db.log --config $CONFFILE 
$MONGOD --port 26003 --bind_ip_all --configsvr --replSet configReplSet --keyFile $KEYFILE --dbpath $WORK_DIR/config_cluster/conf_n3/data --pidfilepath $WORK_DIR/config_cluster/conf_n3/db.pid --logpath $WORK_DIR/config_cluster/conf_n3/db.log --config $CONFFILE

啟動成功後

用命令mongo --port 26001 --host 127.0.0.1

如下圖,進入mongo的shell

在shell中輸入如下js程式碼 設定config叢集

cfg={
    _id:"configReplSet", 
    configsvr: true,
    members:[
        {_id:0, host:'127.0.0.1:26001'},
        {_id:1, host:'127.0.0.1:26002'}, 
        {_id:2, host:'127.0.0.1:26003'}
    ]};
rs.initiate(cfg);

三個config mongo程序會自動選出一個primary,過一會再進入shell就會發現 shell提示變成primary

順便給config新增一個admin使用者,(一個叢集只要在primary程序新增一次,會自動同步給secondary)

use admin
db.createUser({
    user:'admin',pwd:'123456',
    roles:[
        {role:'clusterAdmin',db:'admin'},
        {role:'userAdminAnyDatabase',db:'admin'},
        {role:'dbAdminAnyDatabase',db:'admin'},
        {role:'readWriteAnyDatabase',db:'admin'}
]})

同樣之後shard也做同樣的新增使用者操作,便於後繼觀察資料

啟動shard

WORK_DIR=/home/???/go/mongodb/mongo_test
 
KEYFILE=$WORK_DIR/key/keyfile
cat $KEYFILE
CONFFILE=$WORK_DIR/conf/mongo_node.conf
cat $CONFFILE
MONGOD=mongod
echo $MONGOD
echo "start shard1 replicaset"
$MONGOD --port 27001 --bind_ip_all --shardsvr --replSet shard1 --keyFile $KEYFILE --dbpath $WORK_DIR/shard1/sh1_n1/data --pidfilepath $WORK_DIR/shard1/sh1_n1/db.pid --logpath $WORK_DIR/shard1/sh1_n1/db.log --config $CONFFILE
$MONGOD --port 27002 --bind_ip_all --shardsvr --replSet shard1 --keyFile $KEYFILE --dbpath $WORK_DIR/shard1/sh1_n2/data --pidfilepath $WORK_DIR/shard1/sh1_n2/db.pid --logpath $WORK_DIR/shard1/sh1_n2/db.log --config $CONFFILE
$MONGOD --port 27003 --bind_ip_all --shardsvr --replSet shard1 --keyFile $KEYFILE --dbpath $WORK_DIR/shard1/sh1_n3/data --pidfilepath $WORK_DIR/shard1/sh1_n3/db.pid --logpath $WORK_DIR/shard1/sh1_n3/db.log --config $CONFFILE

用mongo --port 27001 --host 127.0.0.1進入mongo shell

cfg={
    _id:"shard1", 
    members:[
        {_id:0, host:'127.0.0.1:27001'},
        {_id:1, host:'127.0.0.1:27002'},
        {_id:2, host:'127.0.0.1:27003'}
    ]};
 
rs.initiate(cfg);

同樣用之前的新增使用者的js

並用同樣的方法啟動shard2叢集,用於實驗資料分片

對應的目錄與分片名改成shard2

啟動route

WORK_DIR=/home/???/go/mongodb/mongo_test
 
KEYFILE=$WORK_DIR/key/keyfile
cat $KEYFILE
CONFFILE=$WORK_DIR/conf/mongos.conf
cat $CONFFILE
MONGOS=mongos
echo $MONGOS
echo "start mongos route instances"
$MONGOS --port=25001 --bind_ip_all --configdb configReplSet/127.0.0.1:26001,127.0.0.1:26002,127.0.0.1:26003 --keyFile $KEYFILE --pidfilepath $WORK_DIR/route/r_n1/db.pid --logpath $WORK_DIR/route/r_n1/db.log --config $CONFFILE
$MONGOS --port 25002 --bind_ip_all --configdb configReplSet/127.0.0.1:26001,127.0.0.1:26002,127.0.0.1:26003 --keyFile $KEYFILE --pidfilepath $WORK_DIR/route/r_n2/db.pid --logpath $WORK_DIR/route/r_n2/db.log --config $CONFFILE
$MONGOS --port 25003 --bind_ip_all --configdb configReplSet/127.0.0.1:26001,127.0.0.1:26002,127.0.0.1:26003 --keyFile $KEYFILE --pidfilepath $WORK_DIR/route/r_n3/db.pid --logpath $WORK_DIR/route/r_n3/db.log --config $CONFFILE

路由新增分片

用mongo --port 25001 --host 127.0.0.1 -u admin -p 123456進入shell

或者這樣也可 mongo mongodb://admin:123456@127.0.0.1:25001

在mongo shell分別執行以下兩行js

sh.addShard("shard1/127.0.0.1:27001")
sh.addShard("shard2/127.0.0.1:27011")

建立一個mongo database與collection

並設定分片

use test
 
sh.enableSharding("test")
db.createCollection("test_shard")
sh.shardCollection("test.test_shard", {_id:"hashed"}, false, { numInitialChunks: 4} )

在mongo shell用以下js新增資料,可以修改迴圈次數避免測試時間過長

var cnt = 0;
for(var i=0; i<1000; i++){
    var dl = [];
    for(var j=0; j<100; j++){
        dl.push({
                "bookId" : "BBK-" + i + "-" + j,
                "type" : "Revision",
                "version" : "IricSoneVB0001",
                "title" : "Jackson's Life",
                "subCount" : 10,
                "location" : "China CN Shenzhen Futian District",
                "author" : {
                      "name" : 50,
                      "email" : "RichardFoo@yahoo.com",
                      "gender" : "female"
                },
                "createTime" : new Date()
            });
      }
      cnt += dl.length;
      db.test_shard.insertMany(dl);
      print("insert ", cnt);
}

在windows下安裝mongodb,利用自帶的compass使用者端觀察兩個shard叢集

會發現資料分流到兩個叢集了

也可以直接連route觀察資料

補充:

把js存到檔案裡給shell執行會比較方便

執行js命令如下:

mongo mongodb://admin:123456@127.0.0.1:25001 ./test.js

範例js程式碼:

print('=========WECOME==========');
 
conn = new Mongo("mongodb://admin:123456@192.168.2.129:25001");
db = conn.getDB("testjs")
sh.enableSharding("testjs")
db.createCollection("testjs_col")
sh.shardCollection("testjs.testjs_col", {_id:"hashed"}, false, { numInitialChunks: 4} )
    var dl = [];
    for(var j=0; j<10; j++){
        dl.push({
                "bookId" : "BBK-" + 0 + "-" + j,
                "type" : "Revision",
                "version" : "IricSoneVB0001",
                "title" : "Jackson's Life",
                "subCount" : 10,
                "location" : "China CN Shenzhen Futian District",
                "author" : {
                      "name" : 50,
                      "email" : "RichardFoo@yahoo.com",
                      "gender" : "female"
                },
                "createTime" : new Date()
            });
    }
    db.testjs_col.insertMany(dl);
cursor = db.testjs_col.find();
printjson(cursor.toArray());

到此這篇關於mongodb linux下叢集搭建的文章就介紹到這了,更多相關mongodb叢集搭建內容請搜尋it145.com以前的文章或繼續瀏覽下面的相關文章希望大家以後多多支援it145.com!


IT145.com E-mail:sddin#qq.com