Replica Sets+Sharding方案之真枪实弹篇

Posted llguanli

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Replica Sets+Sharding方案之真枪实弹篇相关的知识,希望对你有一定的参考价值。

话说在虚拟机中会报各种错误。在真实的环境中就不会那么痛苦了。
想了一下虚拟机中报错的原因有。

机器卡,处理的时间长就会抛出错误
还有虚拟机假设给的空间太小。也会报异常。


此处讲讲我的大致思路。


第一,选择三台server,最好不是虚拟的
第二,设计三台server的IP。port。和各自的角色
第三,检查防火墙有没有关闭,时间是不是同步
假设上面都能够了。那你就能够操作了。
当然,mongodb版本号和mongodb前提搭建就建在前面吧

wget http://fastdl.mongodb.org/linux/mongodb-linux-x86_64-2.0.4.tgz

解压mongodb文件

主机版本不一样。我的是2.0.4

[[email protected] ~]# tar zxvfmongodb-linux-x86_64-2.4.7.tgz

[[email protected] ~]# mvmongodb-linux-x86_64-2.4.7 /home/mongodb

主机2

[[email protected]~]# tar zxvfmongodb-linux-x86_64-2.4.7.tgz

[[email protected]~]# mvmongodb-linux-x86_64-2.4.7 /home/mongodb

主机3

[[email protected] ~]# tar zxvfmongodb-linux-x86_64-2.4.7.tgz

[[email protected]~]# mvmongodb-linux-x86_64-2.4.7 /home/mongodb


mongo集群分片实战

server三台

 

 

                主机1172.16.0.40                主机2172.16.0.41                               主机3172.16.0.250

     第一片(11731)                                                                   副本                                                                             仲裁

 

    第二片(11732)                 仲裁                                                                                                                               副本

 

    第三片(11733)                    副本                                       仲裁                                                                                   

 

                   30000                    configServer                                configServer                                                           config Server

 

                    60000                          RouteProcess                        RouteProcess                                                        Route Process



创建数据库文件夹

主机1172.16.0.40)配置文件

[[email protected] ~]# mkdir -p/home/data/shard1_1

[[email protected] ~]# mkdir -p/home/data/shard2_1

[[email protected] ~]# mkdir -p/home/data/shard3_1

[[email protected] ~]# mkdir -p/home/data/config                            #config server文件夹

[[email protected] ~]# mkdir -p/home/config                                       #放配置文件

[[email protected] ~]# mkdir -p/home/data/logs

这个地方的全部配置文件夹都放在/home/config文件夹下,切记切记啊。由于我在这里吃过亏。不知一次。


分片1

[[email protected] config]# catshard1_1.conf

port=11731

dbpath=/home/data/shard1_1

logpath=/home/data/logs/shard1_1.log

directoryperdb=true

logappend=true

replSet=shard1

#bind_ip=172.16.0.40

shardsvr=true

fork=true

pidfilepath=/home/data/shard1_1.pid

maxConns=20000

oplogSize=5000

noprealloc=true

nohttpinterface=true

分片2

[[email protected] config]# catshard2_1.conf

port=11732

dbpath=/home/data/shard2_1

logpath=/home/data/logs/shard2_1.log

directoryperdb=true

logappend=true

replSet=shard2

#bind_ip=172.16.0.40

shardsvr=true

fork=true

pidfilepath=/home/data/shard2_1.pid

maxConns=20000

oplogSize=5000

noprealloc=true 

nohttpinterface=true 

分片3

[[email protected] config]# catshard3_1.conf

port=11733

dbpath=/home/data/shard3_1

logpath=/home/data/logs/shard3_1.log

directoryperdb=true

logappend=true

replSet=shard3

#bind_ip=172.16.0.40

shardsvr=true

fork=true

pidfilepath=/home/data/shard3_1.pid

maxConns=20000

oplogSize=5000

noprealloc=true

nohttpinterface=true

config server 配置文件

[[email protected] config]# catconfig.conf

port=30000

dbpath=/home/data/config

logpath=/home/data/logs/config.log

directoryperdb=true

configsvr=true

logappend=true

#bind_ip=172.16.0.40

fork=true

pidfilepath=/home/data/config.pid

maxConns=20000

oplogSize=5000

noprealloc=true

nohttpinterface=true

RouteProces 配置文件

[[email protected] config]# catmongos.conf

port=60000

logpath=/home/data/logs/mongos.log

logappend=true

configdb=172.16.0.40:30000,172.16.0.41:30000,172.16.0.250:30000

#bind_ip=172.16.0.40

fork=true

pidfilepath=/home/data/mongos.pid

chunkSize=5

maxConns=20000

nohttpinterface=true



主机2172.16.0.41)配置文件

[[email protected] ~]# mkdir -p/home/data/shard1_2

[[email protected] ~]# mkdir -p/home/data/shard2_2

[[email protected] ~]# mkdir -p/home/data/shard3_2

[[email protected] ~]# mkdir -p/home/data/config                      #config server文件夹

[[email protected] ~]# mkdir -p/home/config          #放配置文件

[[email protected] ~]# mkdir -p/home/data/logs

 

分片1

[[email protected] config]# catshard1_2.conf

port=11731

dbpath=/home/data/shard1_2

logpath=/home/data/logs/shard1_2.log

directoryperdb=true

logappend=true

replSet=shard1

#bind_ip=172.16.0.41

shardsvr=true

fork=true

pidfilepath=/home/data/shard1_2.pid

maxConns=20000

oplogSize=5000

noprealloc=true

nohttpinterface=true

分片2

[[email protected] config]# catshard2_2.conf

port=11732

dbpath=/home/data/shard2_2

logpath=/home/data/logs/shard2_2.log

directoryperdb=true

logappend=true

replSet=shard2

#bind_ip=172.16.0.41

shardsvr=true

fork=true

pidfilepath=/home/data/shard2_2.pid

maxConns=20000

oplogSize=5000

noprealloc=true

nohttpinterface=true

分片3

[[email protected] config]# catshard3_2.conf

port=11733

dbpath=/home/data/shard3_2

logpath=/home/data/logs/shard3_2.log

directoryperdb=true

logappend=true

replSet=shard3

#bind_ip=172.16.0.41

shardsvr=true

fork=true

pidfilepath=/home/data/shard3_2.pid

maxConns=20000

oplogSize=5000

noprealloc=true

nohttpinterface=true

config server 配置文件

[[email protected] config]# catconfig.conf

port=30000

dbpath=/home/data/config

logpath=/home/data/logs/config.log

directoryperdb=true

configsvr=true

logappend=true

#bind_ip=172.16.0.41

fork=true

pidfilepath=/home/data/config.pid

maxConns=20000

oplogSize=5000

noprealloc=true

nohttpinterface=true

RouteProces 配置文件

[[email protected] config]# catmongos.conf

port=60000

logpath=/home/data/logs/mongos.log

logappend=true

configdb=172.16.0.40:30000,172.16.0.41:30000,172.16.0.250:30000

#bind_ip=172.16.0.41

fork=true

pidfilepath=/home/data/mongos.pid

chunkSize=5

maxConns=20000

nohttpinterface=true



主机3172.16.0.250)配置文件

[[email protected] ~]# mkdir -p/home/data/shard1_3

[[email protected] ~]# mkdir -p/home/data/shard2_3

[[email protected] ~]# mkdir -p/home/data/shard3_3

[[email protected] ~]# mkdir -p/home/data/config                      #config server文件夹

[[email protected] ~]# mkdir -p/home/config          #放配置文件

[[email protected] ~]# mkdir -p/home/data/logs

 

分片1

[[email protected] config]# catshard1_3.conf

port=11731

dbpath=/home/data/shard1_3

logpath=/home/data/logs/shard1_3.log

directoryperdb=true

logappend=true

replSet=shard1

#bind_ip=172.16.0.250

shardsvr=true

fork=true

pidfilepath=/home/data/shard1_3.pid

maxConns=20000

oplogSize=5000

noprealloc=true

nohttpinterface=true

分片2

[[email protected] config]# catshard2_3.conf

port=11732

dbpath=/home/data/shard2_3

logpath=/home/data/logs/shard2_3.log

directoryperdb=true

logappend=true

replSet=shard2

#bind_ip=172.16.0.250

shardsvr=true

fork=true

pidfilepath=/home/data/shard2_3.pid

maxConns=20000

oplogSize=5000

noprealloc=true

nohttpinterface=true

分片3

[[email protected] config]# catshard3_3.conf

port=11733

dbpath=/home/data/shard3_3

logpath=/home/data/logs/shard3_3.log

directoryperdb=true

logappend=true

replSet=shard3

#bind_ip=172.16.0.250

shardsvr=true

fork=true

pidfilepath=/home/data/shard3_3.pid

maxConns=20000

oplogSize=5000

noprealloc=true

nohttpinterface=true

config server 配置文件

[[email protected] config]# catconfig.conf

port=30000

dbpath=/home/data/config

logpath=/home/data/logs/config.log

directoryperdb=true

configsvr=true

logappend=true

#bind_ip=172.16.0.250

fork=true

pidfilepath=/home/data/config.pid

maxConns=20000

oplogSize=5000

noprealloc=true

nohttpinterface=true

RouteProces 配置文件

[[email protected] config]# catmongos.conf

port=60000

logpath=/home/data/logs/mongos.log

logappend=true

configdb=172.16.0.40:30000,172.16.0.41:30000,172.16.0.250:30000

#bind_ip=172.16.0.250

fork=true

pidfilepath=/home/data/mongos.pid

chunkSize=5

maxConns=20000

nohttpinterface=true

启动shard1,shard2,shard3

主机1

[[email protected] ~]#/home/mongodb/bin/mongod -f /home/config/shard1_1.conf

[[email protected] ~]#/home/mongodb/bin/mongod -f /home/config/shard2_1.conf

[[email protected] ~]#/home/mongodb/bin/mongod -f /home/config/shard3_1.conf

 

主机2

[[email protected] ~]#/home/mongodb/bin/mongod -f /home/config/shard1_2.conf

[[email protected] ~]#/home/mongodb/bin/mongod -f /home/config/shard2_2.conf

[[email protected] ~]#/home/mongodb/bin/mongod -f /home/config/shard3_2.conf

 

主机3

[[email protected] ~]#/home/mongodb/bin/mongod -f /home/config/shard1_3.conf

[[email protected] ~]#/home/mongodb/bin/mongod -f /home/config/shard2_3.conf

[[email protected] ~]#/home/mongodb/bin/mongod -f /home/config/shard3_3.conf



配置ReplicaSets

配置分片172.16.0.40上配置

[[email protected] ~]# /home/mongodb/bin/mongo--port 11731

MongoDB shell version: 2.0.4

connecting to:127.0.0.1:11731/test

>config={_id:‘shard1‘,members:[{_id:0,host:‘172.16.0.40:11731‘,priority:2},{_id:1,host:‘172.16.0.41:11731‘},{_id:2,host:‘172.16.0.250:11731‘,arbiterOnly:true}]}

 

rs.initiate(config)

 

 

配置分片172.16.0.41上配置

[[email protected] ~]# /home/mongodb/bin/mongo--port 11732

MongoDB shell version: 2.0.4

connecting to:127.0.0.1:11732/test

>config={_id:‘shard2‘,members:[{_id:0,host:‘172.16.0.40:11732‘,arbiterOnly:true},{_id:1,host:‘172.16.0.41:11732‘,priority:2},{_id:2,host:‘172.16.0.250:11732‘}]}

 

rs.initiate(config)

 

 

配置分片172.16.0.250上配置members里面的优先级priority值高的为主节点。对于仲裁点一定要加上arbiterOnly:true

[[email protected] ~]# /home/mongodb/bin/mongo--port 11733

MongoDB shell version: 2.0.4

connecting to:127.0.0.1:11733/test

>config={_id:‘shard3‘,members:[{_id:0,host:‘172.16.0.40:11733‘},{_id:1,host:‘172.16.0.41:11733‘,arbiterOnly:true},{_id:2,host:‘172.16.0.25:11733‘,priority:2}]}

 

rs.initiate(config)

 

启动3configserver

主机1

[[email protected] ~]#/home/mongodb/bin/mongod  -f/home/config/config.conf

 

主机2

[[email protected] ~]#/home/mongodb/bin/mongod  -f/home/config/config.conf

 

主机3

[[email protected] ~]#/home/mongodb/bin/mongod  -f/home/config/config.conf

 

启动3Route Process

主机1

[[email protected] ~]# /home/mongodb/bin/mongos-f /home/config/mongos.conf

主机2

[[email protected] ~]# /home/mongodb/bin/mongos-f /home/config/mongos.conf

 

主机3

[[email protected] ~]#/home/mongodb/bin/mongos -f /home/config/mongos.conf


配置shardCluser

[[email protected] ~]# /home/mongodb/bin/mongo--port 60000

MongoDB shell version: 2.0.4

connecting to:127.0.0.1:60000/test

mongos> use admin

switched to db admin

mongos>db.runCommand({addshard:"shard1/172.16.0.40:11731,172.16.0.41:11731, 172.16.0.250:11731"})

 

mongos>db.runCommand({addshard:"shard2/172.16.0.40:11732,172.16.0.41:11732, 172.16.0.250:11732"})

 

mongos> db.runCommand({addshard:"shard3/172.16.0.40:11733,172.16.0.41:11733, 172.16.0.250:11733"})

 

接下来激活分片,如以下的代码所看到的:   採用hash分片

db.runCommand({enablesharding:"test"})

db.runCommand({shardcollection:"test.users",key:{id:"hashed"}})

上面这个会报错,我这边版本号不支持hashed分片,要把id:1

以上是关于Replica Sets+Sharding方案之真枪实弹篇的主要内容,如果未能解决你的问题,请参考以下文章

mongodb复制集(Replica sets)+分片(Sharding)环境搭建

mongodb3.2 replica sets

学习MongoDB(Troubleshoot Replica Sets) 集群排除故障

部署Replica Sets及查看相关配置

MongoDB——MongoDB副本集(Replica Sets)

MongoDB——MongoDB副本集(Replica Sets)