重庆分公司,新征程启航

为企业提供网站建设、域名注册、服务器等服务

公司的实战ReplicaSets+Sharding方案采用配置文件2-创新互联

公司的实战Replica Sets+Sharding方案采用配置文件2

创新互联自2013年创立以来,是专业互联网技术服务公司,拥有项目网站设计制作、网站建设网站策划,项目实施与项目整合能力。我们以让每一个梦想脱颖而出为使命,1280元徐闻做网站,已为上家服务,为徐闻各地企业和个人服务,联系电话:18982081108

公司mongo集群分片实战

服务器三台

                                    主机1(172.16.0.115) 主机2(172.16.0.114) 主机3(172.16.0.103)

第一片(11731)                 主                                         副本                                              仲裁

第二片(11732)                 仲裁                                    主                                                副本

第三片(11733)                 副本                                    仲裁                                              主

        30000                         config Server         config Server                    config Server

        60000                       Route Process                   Route Process                   Route Process

创建数据库目录

主机1172.16.0.115)配置文件

[root@mongodb115 ~]# mkdir -p /home/data/shard1_1

[root@mongodb115 ~]# mkdir -p /home/data/shard2_1

[root@mongodb115 ~]# mkdir -p /home/data/shard3_1

[root@mongodb115 ~]# mkdir -p /home/data/config                      #config server目录

[root@mongodb115 ~]# mkdir -p /home/config          #放配置文件

[root@mongodb115 ~]# mkdir -p /home/data/logs

分片1

[root@mongodb115 config]# cat shard1_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.115

shardsvr=true

fork=true

pidfilepath=/home/data/shard1_1.pid

maxConns=20000

oplogSize=5000

noprealloc=true

nohttpinterface=true

分片2

[root@mongodb115 config]# cat shard2_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.115

shardsvr=true

fork=true

pidfilepath=/home/data/shard2_1.pid

maxConns=20000

oplogSize=5000

noprealloc=true

nohttpinterface=true

分片3

[root@mongodb115 config]# cat shard3_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.115

shardsvr=true

fork=true

pidfilepath=/home/data/shard3_1.pid

maxConns=20000

oplogSize=5000

noprealloc=true

nohttpinterface=true

config server配置文件

[root@mongodb115 config]# cat config.conf

port=30000

dbpath=/home/data/config

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

directoryperdb=true

configsvr=true

logappend=true

#bind_ip=172.16.0.115

fork=true

pidfilepath=/home/data/config.pid

maxConns=20000

oplogSize=5000

noprealloc=true

nohttpinterface=true

Route Proces配置文件

[root@mongodb115 config]# cat mongos.conf

port=60000

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

logappend=true

configdb=172.16.0.115:30000,172.16.0.114:30000,172.16.0.103:30000

#bind_ip=172.16.0.115

fork=true

pidfilepath=/home/data/mongos.pid

chunkSize=5

maxConns=20000

nohttpinterface=true

主机2172.16.0.114)配置文件

[root@mongodb114 ~]# mkdir -p /home/data/shard1_2

[root@mongodb114 ~]# mkdir -p /home/data/shard2_2

[root@mongodb114 ~]# mkdir -p /home/data/shard3_2

[root@mongodb114 ~]# mkdir -p /home/data/config                      #config server目录

[root@mongodb114 ~]# mkdir -p /home/config          #放配置文件

[root@mongodb114 ~]# mkdir -p /home/data/logs

分片1

[root@mongodb114 config]# cat shard1_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.114

shardsvr=true

fork=true

pidfilepath=/home/data/shard1_2.pid

maxConns=20000

oplogSize=5000

noprealloc=true

nohttpinterface=true

分片2

[root@mongodb114config]# cat shard2_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.114

shardsvr=true

fork=true

pidfilepath=/home/data/shard2_2.pid

maxConns=20000

oplogSize=5000

noprealloc=true

nohttpinterface=true

分片3

[root@mongodb114 config]# cat shard3_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.114

shardsvr=true

fork=true

pidfilepath=/home/data/shard3_2.pid

maxConns=20000

oplogSize=5000

noprealloc=true

nohttpinterface=true

config server配置文件

[root@mongodb114 config]# cat config.conf

port=30000

dbpath=/home/data/config

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

directoryperdb=true

configsvr=true

logappend=true

#bind_ip=172.16.0.114

fork=true

pidfilepath=/home/data/config.pid

maxConns=20000

oplogSize=5000

noprealloc=true

nohttpinterface=true

Route Proces配置文件

[root@mongodb114 config]# cat mongos.conf

port=60000

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

logappend=true

configdb=172.16.0.115:30000,172.16.0.114:30000,172.16.0.103:30000

#bind_ip=172.16.0.114

fork=true

pidfilepath=/home/data/mongos.pid

chunkSize=5

maxConns=20000

nohttpinterface=true

主机3172.16.0.103)配置文件

[root@mongodb103 ~]# mkdir -p /home/data/shard1_3

[root@mongodb103 ~]# mkdir -p /home/data/shard2_3

[root@mongodb103 ~]# mkdir -p /home/data/shard3_3

[root@mongodb103 ~]# mkdir -p /home/data/config                      #config server目录

[root@mongodb103 ~]# mkdir -p /home/config          #放配置文件

[root@mongodb103 ~]# mkdir -p /home/data/logs

分片1

[root@mongodb103 config]# cat shard1_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.103

shardsvr=true

fork=true

pidfilepath=/home/data/shard1_3.pid

maxConns=20000

oplogSize=5000

noprealloc=true

nohttpinterface=true

分片2

[root@mongodb103 config]# cat shard2_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.103

shardsvr=true

fork=true

pidfilepath=/home/data/shard2_3.pid

maxConns=20000

oplogSize=5000

noprealloc=true

nohttpinterface=true

分片3

[root@mongodb103 config]# cat shard3_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.103

shardsvr=true

fork=true

pidfilepath=/home/data/shard3_3.pid

maxConns=20000

oplogSize=5000

noprealloc=true

nohttpinterface=true

config server配置文件

[root@mongodb103 config]# cat config.conf

port=30000

dbpath=/home/data/config

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

directoryperdb=true

configsvr=true

logappend=true

#bind_ip=172.16.0.103

fork=true

pidfilepath=/home/data/config.pid

maxConns=20000

oplogSize=5000

noprealloc=true

nohttpinterface=true

Route Proces配置文件

[root@mongodb103 config]# cat mongos.conf

port=60000

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

logappend=true

configdb=172.16.0.115:30000,172.16.0.114:30000,172.16.0.103:30000

#bind_ip=172.16.0.103

fork=true

pidfilepath=/home/data/mongos.pid

chunkSize=5

maxConns=20000

nohttpinterface=true

解压mongodb文件

主机1

[root@mongodb115 ~]# tar zxvf mongodb-linux-x86_64-2.4.7.tgz

[root@mongodb115 ~]# mv mongodb-linux-x86_64-2.4.7 /home/mongodb

主机2

[root@mongodb114 ~]# tar zxvf mongodb-linux-x86_64-2.4.7.tgz

[root@mongodb114 ~]# mv mongodb-linux-x86_64-2.4.7 /home/mongodb

主机3

[root@mongodb103 ~]# tar zxvf mongodb-linux-x86_64-2.4.7.tgz

[root@mongodb103 ~]# mv mongodb-linux-x86_64-2.4.7 /home/mongodb

启动shard1,shard2,shard3

主机1

[root@mongodb115 ~]# /home/mongodb/bin/mongod -f /home/config/shard1_1.conf

[root@mongodb115 ~]# /home/mongodb/bin/mongod -f /home/config/shard2_1.conf

[root@mongodb115 ~]# /home/mongodb/bin/mongod -f /home/config/shard3_1.conf

主机2

[root@mongodb114 ~]# /home/mongodb/bin/mongod -f /home/config/shard1_2.conf

[root@mongodb114 ~]# /home/mongodb/bin/mongod -f /home/config/shard2_2.conf

[root@mongodb114 ~]# /home/mongodb/bin/mongod -f /home/config/shard3_2.conf

主机3

[root@mongodb103 ~]# /home/mongodb/bin/mongod -f /home/config/shard1_3.conf

[root@mongodb103 ~]# /home/mongodb/bin/mongod -f /home/config/shard2_3.conf

[root@mongodb103 ~]# /home/mongodb/bin/mongod -f /home/config/shard3_3.conf

配置Replica Sets

配置分片1172.16.0.115上配置

[root@mongodb115 ~]# /home/mongodb/bin/mongo --port 11731

MongoDB shell version: 2.4.7

connecting to: 127.0.0.1:11731/test

>config={_id:'shard1',members:[{_id:0,host:'172.16.0.115:11731' ,priority:2},{_id:1,host:'172.16.0.114:11731'},{_id:2,host:'172.16.0.103:11731',arbiterOnly:true}]}

rs.initiate(config)

配置分片2172.16.0.114上配置

[root@mongodb114 ~]# /home/mongodb/bin/mongo --port 11732

MongoDB shell version: 2.4.7

connecting to: 127.0.0.1:11732/test

>config={_id:'shard2',members:[{_id:0,host:'172.16.0.115:11732',arbiterOnly:true},{_id:1,host:'172.16.0.114:11732',priority:2},{_id:2,host:'172.16.0.103:11732' }]}

rs.initiate(config)

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

[root@mongodb103 ~]# /home/mongodb/bin/mongo --port 11733

MongoDB shell version: 2.4.7

connecting to: 127.0.0.1:11733/test

>config={_id:'shard3',members:[{_id:0,host:'172.16.0.115:11733'},{_id:1,host:'172.16.0.114:11733',arbiterOnly:true },{_id:2,host:'172.16.0.103:11733',priority:2 }]}

rs.initiate(config)

启动3config server

主机1

[root@mongodb115 ~]# /home/mongodb/bin/mongod -f /home/config/config.conf

主机2

[root@mongodb114 ~]# /home/mongodb/bin/mongod -f /home/config/config.conf

主机3

[root@mongodb103 ~]# /home/mongodb/bin/mongod -f /home/config/config.conf

启动3 Route Process

主机1

[root@mongodb115 ~]# /home/mongodb/bin/mongos -f /home/config/mongos.conf

主机2

[root@mongodb114 ~]# /home/mongodb/bin/mongos -f /home/config/mongos.conf

主机3

[root@mongodb103 ~]# /home/mongodb/bin/mongos -f /home/config/mongos.conf

配置shard Cluser

[root@mongodb115 ~]# /home/mongodb/bin/mongo --port 60000

MongoDB shell version: 2.4.7

connecting to: 127.0.0.1:60000/test

mongos> use admin

switched to db admin

mongos>db.runCommand({addshard:"shard1/172.16.0.115:11731, 172.16.0.114:11731, 172.16.0.103:11731"})

mongos>db.runCommand({addshard:"shard2/172.16.0.115:11732, 172.16.0.114:11732, 172.16.0.103:11732"})

mongos> db.runCommand({addshard:"shard3/172.16.0.115:11733, 172.16.0.114:11733, 172.16.0.103:11733"})

接下来激活分片,如下面的代码所示:   采用hash分片

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

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

强调服务时间必须要同步,否则有问题

另外有需要云服务器可以了解下创新互联cdcxhl.cn,海内外云服务器15元起步,三天无理由+7*72小时售后在线,公司持有idc许可证,提供“云服务器、裸金属服务器、高防服务器、香港服务器、美国服务器、虚拟主机、免备案服务器”等云主机租用服务以及企业上云的综合解决方案,具有“安全稳定、简单易用、服务可用性高、性价比高”等特点与优势,专为企业上云打造定制,能够满足用户丰富、多元化的应用场景需求。


网站标题:公司的实战ReplicaSets+Sharding方案采用配置文件2-创新互联
本文路径:http://cqcxhl.cn/article/cdcgdj.html

其他资讯

在线咨询
服务热线
服务热线:028-86922220
TOP