mongodblinux下集群搭建过程


当前第2页 返回上一页

并用同样的方法启动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集群搭建内容请搜索


打赏

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,您说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

分享从这里开始,精彩与您同在

评论

管理员已关闭评论功能...