您好,登錄后才能下訂單哦!
小編給大家分享一下linux下mongodb集群搭建過程的示例分析,相信大部分人都還不怎么了解,因此分享這篇文章給大家參考一下,希望大家閱讀完這篇文章后大有收獲,下面讓我們一起去了解一下吧!
mongodb的集群結構如上圖
網上有個mongo3.0的集群例子:
http://www.neiyidaogou.com/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/sh2_n1/data --pidfilepath $WORK_DIR/shard1/sh2_n1/db.pid --logpath $WORK_DIR/shard1/sh2_n1/db.log --config $CONFFILE $MONGOD --port 27002 --bind_ip_all --shardsvr --replSet shard1 --keyFile $KEYFILE --dbpath $WORK_DIR/shard1/sh2_n2/data --pidfilepath $WORK_DIR/shard1/sh2_n2/db.pid --logpath $WORK_DIR/shard1/sh2_n2/db.log --config $CONFFILE $MONGOD --port 27003 --bind_ip_all --shardsvr --replSet shard1 --keyFile $KEYFILE --dbpath $WORK_DIR/shard1/sh2_n3/data --pidfilepath $WORK_DIR/shard1/sh2_n3/db.pid --logpath $WORK_DIR/shard1/sh2_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());
以上是“linux下mongodb集群搭建過程的示例分析”這篇文章的所有內容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內容對大家有所幫助,如果還想學習更多知識,歡迎關注億速云行業資訊頻道!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。