mongo集群配置

来源:互联网 发布:淘宝订单评价过期 编辑:程序博客网 时间:2022/08/17 11:46
1.1> 创建第一个replset
# 创建目录
mkdir -p /home/lihan/work/mongo/data/replset_sharding/replset1/r0
mkdir -p /home/lihan/work/mongo/data/replset_sharding/replset1/r1
mkdir -p /home/lihan/work/mongo/data/replset_sharding/replset1/r2
mkdir -p /home/lihan/work/mongo/data/replset_sharding/replset1/log




# 改用下面的就行了(这是里为方便看日志,直接在命令行输出查看)
/home/lihan/work/mongo/bin/mongod --dbpath /home/lihan/work/mongo/data/replset_sharding/replset1/r0 --replSet  replset1 --port 18010 --directoryperdb --rest &
/home/lihan/work/mongo/bin/mongod --dbpath /home/lihan/work/mongo/data/replset_sharding/replset1/r1 --replSet  replset1 --port 18011 --directoryperdb --rest &
/home/lihan/work/mongo/bin/mongod --dbpath /home/lihan/work/mongo/data/replset_sharding/replset1/r2 --replSet  replset1 --port 18012 --directoryperdb --rest &




# 初始化复本集:
/home/lihan/work/mongo/bin/mongo --port 18010


config_replset1 = {
_id:"replset1",
members:
[
{_id:0,host:"127.0.0.1:18010",priority:4},
{_id:1,host:"127.0.0.1:18011",priority:2},
{_id:2,host:"127.0.0.1:18012",arbiterOnly : true}
]
}


# 注意arbiter仲裁节点只投票,不接收复制的数据!
rs.initiate(config_replset1);


1.2> 创建第二个replset
# 创建目录
mkdir -p /home/lihan/work/mongo/data/replset_sharding/replset2/r0
mkdir -p /home/lihan/work/mongo/data/replset_sharding/replset2/r1
mkdir -p /home/lihan/work/mongo/data/replset_sharding/replset2/r2
mkdir -p /home/lihan/work/mongo/data/replset_sharding/replset2/log




# 改用下面的就行了(这是里为方便看日志,直接在命令行输出查看)
/home/lihan/work/mongo/bin/mongod --dbpath /home/lihan/work/mongo/data/replset_sharding/replset2/r0 --replSet  replset2 --port 28010 --directoryperdb --rest &
/home/lihan/work/mongo/bin/mongod --dbpath /home/lihan/work/mongo/data/replset_sharding/replset2/r1 --replSet  replset2 --port 28011 --directoryperdb --rest &
/home/lihan/work/mongo/bin/mongod --dbpath /home/lihan/work/mongo/data/replset_sharding/replset2/r2 --replSet  replset2 --port 28012 --directoryperdb --rest &




# 初始化复本集:
/home/lihan/work/mongo/bin/mongo --port 28010


config_replset2 = {
_id:"replset2",
members:
[
{_id:0,host:"127.0.0.1:28010",priority:4},
{_id:1,host:"127.0.0.1:28011",priority:2},
{_id:2,host:"127.0.0.1:28012",arbiterOnly : true}
]
}


# 注意arbiter仲裁节点只投票,不接收复制的数据!
rs.initiate(config_replset2);


1.3> 创建第三个replset
#创建目录
mkdir -p /home/lihan/work/mongo/data/replset_sharding/replset3/r0
mkdir -p /home/lihan/work/mongo/data/replset_sharding/replset3/r1
mkdir -p /home/lihan/work/mongo/data/replset_sharding/replset3/r2
mkdir -p /home/lihan/work/mongo/data/replset_sharding/replset3/log


#改用下面的就行了(这是里为方便看日志,直接在命令行输出查看)
/home/lihan/work/mongo/bin/mongod --dbpath /home/lihan/work/mongo/data/replset_sharding/replset3/r0 --replSet  replset3 --port 38010 --directoryperdb --rest &
/home/lihan/work/mongo/bin/mongod --dbpath /home/lihan/work/mongo/data/replset_sharding/replset3/r1 --replSet  replset3 --port 38011 --directoryperdb --rest &
/home/lihan/work/mongo/bin/mongod --dbpath /home/lihan/work/mongo/data/replset_sharding/replset3/r2 --replSet  replset3 --port 38012 --directoryperdb --rest &


#初始化复本集:
/home/lihan/work/mongo/bin/mongo --port 38010


config_replset3 = {
_id:"replset3",
members:
[
{_id:0,host:"127.0.0.1:38010",priority:4},
{_id:1,host:"127.0.0.1:38011",priority:2},
{_id:2,host:"127.0.0.1:38012",arbiterOnly : true}
]
}


#注意arbiter仲裁节点只投票,不接收复制的数据!
rs.initiate(config_replset3);


2> 创建数据库,日志文件
mkdir -p /home/lihan/work/mongo/data/replset_sharding/log


2.1> 启动Config Server, Config Server : 40000,40001,40002
mkdir -p /home/lihan/work/mongo/data/replset_sharding/config0
mkdir -p /home/lihan/work/mongo/data/replset_sharding/config1
mkdir -p /home/lihan/work/mongo/data/replset_sharding/config2


/home/lihan/work/mongo/bin/mongod --dbpath /home/lihan/work/mongo/data/replset_sharding/config0 --configsvr --port 40000 --directoryperdb --rest &
/home/lihan/work/mongo/bin/mongod --dbpath /home/lihan/work/mongo/data/replset_sharding/config1 --configsvr --port 40001 --directoryperdb --rest &
/home/lihan/work/mongo/bin/mongod --dbpath /home/lihan/work/mongo/data/replset_sharding/config2 --configsvr --port 40002 --directoryperdb --rest &




#正式运行要后台运行就用下面的:
#/home/lihan/work/mongo/bin/mongod  --dbpath /data/replset_sharding/config --configsvr --port 40000  --logpath /data/replset_sharding/log/config.log --fork --directoryperdb --rest


#以上的3个Config Server里存放集群分片的配置信息,如果Config Server出问题是无法正常启动集群的!
#配置服务器使用的是两步提交机制(而不是普通MongoDB副本集的异步复制),因为两步提交是实时的一致性,
#而异步复制是最终一致性!
#两步提交机制确保3台机的配置是一致的,如果有某台配置服务器宕机了,那集群配置信息将是只读的! 
#客户端还是能够读取集群的数据,还有能有限地更新集群的数据(只要不需要修改配置服务器的配置,
#如chunks块的修改,或片键的修改)


2.2> 启动Route Process,Route Process : 50000
/home/lihan/work/mongo/bin/mongos --port 50000 --configdb 127.0.0.1:40000,127.0.0.1:40001,127.0.0.1:40002 --chunkSize 1 


# chunkSize 1 (MB)指定分片的最小单位容量,这里设置1M,方便查看效果 


#正式运行要后台运行就用下面的:
#/home/lihan/work/mongo/bin/mongos --port 50000 --configdb 127.0.0.1:40000 --chunkSize 50 --logpath=/data/replset_sharding/log/route.log --fork


2.3> 连接到mongos配置Sharding
#用Mongo Shell 登录 Route Process
/home/lihan/work/mongo/bin/mongo --port 50000 


# use admin (记得执行这一点,切换到admin数据库)


#添加分片节点,每个分片都是一个副本集
db.runCommand({addshard:"replset1/127.0.0.1:18010,127.0.0.1:18011,127.0.0.1:18012",allowLocal:true});
db.runCommand({addshard:"replset2/127.0.0.1:28010,127.0.0.1:28011,127.0.0.1:28012",allowLocal:true});
db.runCommand({addshard:"replset3/127.0.0.1:38010,127.0.0.1:38011,127.0.0.1:38012",allowLocal:true});

0 0