Mongo分片集群脚本

bash大法好啊,一键玩mongo。

我的mongo版本是:MongoDB shell version v4.0.2

这里准备为大家献上Mongo创建分片和可复制集的脚本,以及在部署的时候踩的坑。

分完片插入的文档没有包含分片的键:

image

兄弟们,权限自动化的脚本来了~

安装好了mongo之后,执行脚本就可以玩了,呵呵

mon-config-cluster-start.sh

#!/bin/bash


# 杀死与mongo有关的所有进程, 先把进程都清一遍
pid=$(ps -ef | grep 'mongo' | grep -v grep | awk '{print $2}')
for temp in $pid
do
    echo "this is $temp"
    if ps -p $temp > /dev/null
    then
        echo "$temp is running"
        kill -9 $temp
    fi
done


# 创建分片实验的数据文件, 如果已经存在data的话就先删除
if [ ! -d "/root/data" ]; then
    mkdir /root/data
else
    echo "文件夹已经存在, 先清空/root/data"
    rm -rf /root/data/*
fi


# 创建shard-a的数据目录
echo "创建shard-a的数据目录"
mkdir /root/data/rs-a-1
mkdir /root/data/rs-a-2
mkdir /root/data/rs-a-3

# 启动第一个可复制集
mongod --shardsvr --replSet shard-a --dbpath /root/data/rs-a-1 --port 30000 --logpath /root/data/rs-a-1.log --fork
mongod --shardsvr --replSet shard-a --dbpath /root/data/rs-a-2 --port 30001 --logpath /root/data/rs-a-2.log --fork
mongod --shardsvr --replSet shard-a --dbpath /root/data/rs-a-3 --port 30002 --logpath /root/data/rs-a-3.log --fork

mongo localhost:30000/test initial-shard-a.js


# 创建shard-b的数据目录
echo "创建shard-b的数据目录"
mkdir /root/data/rs-b-1
mkdir /root/data/rs-b-2
mkdir /root/data/rs-b-3


# 启动第二个可复制集
mongod --shardsvr --replSet shard-b --dbpath /root/data/rs-b-1 --port 30100 --logpath /root/data/rs-b-1.log --fork
mongod --shardsvr --replSet shard-b --dbpath /root/data/rs-b-2 --port 30101 --logpath /root/data/rs-b-2.log --fork
mongod --shardsvr --replSet shard-b --dbpath /root/data/rs-b-3 --port 30102 --logpath /root/data/rs-b-3.log --fork

mongo localhost:30100/test initial-shard-b.js


# 创建配置文件的目录
mkdir /root/data/config-1
mkdir /root/data/config-2
mkdir /root/data/config-3


echo "准备启动配置服务进程"
# 启动配置服务器进程
# Failed global initialization: BadValue: nojournal is not allowed when configsvr is specified
# 这里没有指定配置项 --nojournal
mongod --configsvr --replSet conf --dbpath /root/data/config-1 --port 27019 --logpath /root/data/config-1.log --fork
mongod --configsvr --replSet conf --dbpath /root/data/config-2 --port 27020 --logpath /root/data/config-2.log --fork
mongod --configsvr --replSet conf --dbpath /root/data/config-3 --port 27021 --logpath /root/data/config-3.log --fork

mongo localhost:27019/test initial-conf.js

# 启动mongos进程
# 注意前面要有conf这个配置集群可复制集的名字
echo "准备启动路由进程......"
mongos --configdb conf/localhost:27019,localhost:27020,localhost:27021 --logpath /root/data/mongos.log --port 40000 --fork

# 添加分片, 以及对哪个库和集合进行分片
mongo localhost:40000/test add-shard.js

# 执行插入数据, 然后你就可以去shard-a和shard-b上去看数据了, 确实被分片了
mongo localhost:40000/test add-doc.js

initial-shard-a.js

// 使用这种单条的方式不行, 似乎只有第一条语句有效果, 后面的rs.add就没用了
// rs.initiate();
// print("执行了initiate()");
// rs.add("127.0.0.1:30001");
// print("added secondary...");
// rs.addArb("127.0.0.1:30002");

config={
    _id:"shard-a",
    members:[
        {_id:0,host:"localhost:30000"},
        {_id:1,host:"localhost:30001"},
        {_id:2,host:"localhost:30002",arbiterOnly:true}
    ]
};
rs.initiate(config);

initial-shard-b.js

// rs.initiate();
// rs.add("localhost:30101");
// rs.addArb("localhost:30102");

config={
    _id:"shard-b",
    members:[
        {_id:0,host:"localhost:30100"},
        {_id:1,host:"localhost:30101"},
        {_id:2,host:"localhost:30102",arbiterOnly:true}
    ]
};
rs.initiate(config);

initial-conf.js

// 日了狗了, 配置服务器不能使用arbiter
config={
    _id:"conf",
    members:[
        {_id:0,host:"localhost:27019"},
        {_id:1,host:"localhost:27020"},
        {_id:2,host:"localhost:27021"}
    ]
};
rs.initiate(config);

print("初始化配置可复制集...");

add-shard.js

print("准备添加分片......");
printjson(sh.addShard('shard-a/localhost:30000,localhost:30001'));
printjson(sh.addShard('shard-b/localhost:30100,localhost:30101'));
print("开启test可分片")
printjson(sh.enableSharding("test"));
printjson(sh.shardCollection('test.user',{name: 1}));
printjson(sh.status());
print("添加分片完毕");

add-doc.js

print("start....");

t = "I love China, I love palying DOTA, 妈蛋国庆节怎么说...."
for(i=0; i<10; i++) {
    t = t + t;
}

print("heheda");
for(a=0; a<1000; a++) {
    print("a = " + a);
    db.user.insert({name:Math.random().toString(36).substr(2), filename: "sheet-1", updated_at: new Date(),data: t});
}

猜你喜欢

转载自www.cnblogs.com/tuhooo/p/9724901.html
今日推荐