MongoDB-Sharding部署方案

一、    部署环境

  1. 五台主机:
    1. Amongoshard01:  10.212.74.43
    2. Amongoshard02:  10.212.84.4
    3. Amongoshard03:  10.212.98.23
    4. Amongoshard04:  10.212.46.5
    5. Amongoshard05:  10.212.70.21
  2. 安装;
    1. CentOS 6.5系统
    2. mongodb-linux-x86_64-rhel62-3.0.2.tgz

二、    部署方案

方案目标是搭建两个shard(shard1和shard2)实现数据分片,每个shard分别由一个副本集(一主两从一仲裁)组成。Amongoshard01主机开启27017和30000端口作为shard11和mongos实例端口;Amongoshard02开启27017、27018、27019和30000端口作为shard12、shard13、config和mongos实例端口;Amongoshard03主机开启27017和30000端口作为shard21和mongos实例端口;Amongoshard04开启27017、27018、27019和30000端口作为shard22、shard23、config和mongos实例端口;Amongoshard05开启端口27017、27018、27019作为shard14、shard24和config实例端口。

Shard11(主写操作:priority为2)、shard12(从读操作:priority为1)、shard13(仲裁)和shard14(从备份:priority为0)组成一个副本集shard1作为一个分片shard1;shard21(主写操作:priority为2)、shard22(从读操作:priority为1)、shard23(仲裁)和shard24(从备份:priority为0)组成另一个副本集shard2作为另一个分片shard2。实例部署如下:

    1)Amongoshard01上:

      Shard11:10.212.74.43:27017

        Mongos :10.212.74.43:30000

2)Amongoshard02上:

Shard12: 10.212.84.4:27017

Shard13: 10.212.84.4:27018

Config: 10.212.84.4:27019

Mongos: 10.212.84.4:30000

   3)Amongoshard03上:

      Shard21:10.212.98.23:27017

      Mongos :10.212.98.23:30000

4)Amongoshard04上:

Shard22:10.212.46.5:27017

Shard23: 10.212.46.5:27018

Config: 10.212.46.5:27019

Mongos: 10.212.46.5:30000

     5)Amongoshard05上:

       Shard14:10.212.70.21:27017

          Shard24:10.212.70.21:27018

          Config:10.212.70.21:27019

总概况如下图所示:

该部署方案可实现数据的读写分离和备份容灾。Config放在不同主机上或者独立,仲裁机和从机放在一起或独立,无论哪台主机当机均能提供服务。

三、    部署实现

  1. mongodb的安装:

Amongoshard01(10.212.74.43 )

--#cd /opt/software

--#tar -zxvf mongodb-linux-x86_64-rhel62-3.0.2.tgz

--#mv mongodb-linux-x86_64-rhel62-3.0.2 /opt/mongodb3.2

--#useradd mongodb

--#chmod -R mongodb.mongodb /data/mongodb

--#su - mongodb

--$mkdir -p /data/mongodb/data/shard11

Amongoshard02(10.212.84.4 )

--#cd /opt/software

--#tar -zxvf mongodb-linux-x86_64-rhel62-3.0.2.tgz

--#mv mongodb-linux-x86_64-rhel62-3.0.2 /opt/mongodb3.2

--#useradd mongodb

--#chmod -R mongodb.mongodb /data/mongodb

--#su - mongodb

--$mkdir -p /data/mongodb/data/shard12

--$mkdir -p /data/mongodb/data/shard13

--$mkdir -p /data/mongodb/data/config

Amongoshard03(10.212.98.23 )

--#cd /opt/software

--#tar -zxvf mongodb-linux-x86_64-rhel62-3.0.2.tgz

--#mv mongodb-linux-x86_64-rhel62-3.0.2 /opt/mongodb3.2

--#useradd mongodb

--#chmod -R mongodb.mongodb /data/mongodb

--#su - mongodb

--$mkdir -p /data/mongodb/data/shard21

Amongoshard04(10.212.46.5 )

--#cd /opt/software

--#tar -zxvf mongodb-linux-x86_64-rhel62-3.0.2.tgz

--#mv mongodb-linux-x86_64-rhel62-3.0.2 /opt/mongodb3.2

--#useradd mongodb

--#chmod -R mongodb.mongodb /data/mongodb

--#su - mongodb

--$mkdir -p /data/mongodb/data/shard22

--$mkdir -p /data/mongodb/data/shard23

--$mkdir -p /data/mongodb/data/config

Amongoshard05(10.212.70.21 )

--#cd /opt/software

--#tar -zxvf mongodb-linux-x86_64-rhel62-3.0.2.tgz

--#mv mongodb-linux-x86_64-rhel62-3.0.2 /opt/mongodb3.2

--#useradd mongodb

--#chmod -R mongodb.mongodb /data/mongodb

--#su - mongodb

--$mkdir -p /data/mongodb/data/shard14

--$mkdir -p /data/mongodb/data/shard24

--$mkdir -p /data/mongodb/data/config

2.mongodb的配置:

Amongoshard01(10.212.74.43 )

--$vi shard11.conf

shardsvr = true

replSet = shard1

port = 27017

dbpath = /data/mongodb/data/shard11

oplogSize = 100

logpath = /data/mongodb/data/shard11.log

logappend = true

maxConns = 10000

pidfilepath = /data/mongodb/data/shard11.pid

bind_ip = 10.212.74.43

rest = true

fork = true

noprealloc = true

directoryperdb = true

Amongoshard02(10.212.84.4 )

--$vi shard12.conf

shardsvr = true

replSet = shard1

port = 27017

dbpath = /data/mongodb/data/shard12

oplogSize = 100

logpath = /data/mongodb/data/shard12.log

logappend = true

maxConns = 10000

pidfilepath = /data/mongodb/data/shard12.pid

bind_ip = 10.212.84.4

rest = true

fork = true

noprealloc = true

directoryperdb = true

--$vi shard13.conf

shardsvr = true

replSet = shard1

port = 27018

dbpath = /data/mongodb/data/shard13

oplogSize = 100

logpath = /data/mongodb/data/shard13.log

logappend = true

maxConns = 10000

pidfilepath = /data/mongodb/data/shard13.pid

bind_ip = 10.212.84.4

rest = true

fork = true

noprealloc = true

directoryperdb = true

--$vi configsvr.conf

dbpath = /data/mongodb/data/config

logpath = /data/mongodb/data/config.log

logappend = true

bind_ip = 10.212.84.4

port = 27019

fork = true

Amongoshard03(10.212.98.23 )

--$vi shard21.conf

shardsvr = true

replSet = shard2

port = 27017

dbpath = /data/mongodb/data/shard21

oplogSize = 100

logpath = /data/mongodb/data/shard21.log

logappend = true

maxConns = 10000

pidfilepath = /data/mongodb/data/shard21.pid

bind_ip = 10.212.98.23

rest = true

fork = true

noprealloc = true

directoryperdb = true

Amongoshard04(10.212.46.5 )

--$vi shard22.conf

shardsvr = true

replSet = shard2

port = 27017

dbpath = /data/mongodb/data/shard22

oplogSize = 100

logpath = /data/mongodb/data/shard22.log

logappend = true

maxConns = 10000

pidfilepath = /data/mongodb/data/shard22.pid

bind_ip = 10.212.46.5

rest = true

fork = true

noprealloc = true

directoryperdb = true

--$vi shard23.conf

shardsvr = true

replSet = shard2

port = 27018

dbpath = /data/mongodb/data/shard23

oplogSize = 100

logpath = /data/mongodb/data/shard23.log

logappend = true

maxConns = 10000

pidfilepath = /data/mongodb/data/shard23.pid

bind_ip = 10.212.46.5

rest = true

fork = true

noprealloc = true

directoryperdb = true

--$vi configsvr.conf

dbpath = /data/mongodb/data/config

logpath = /data/mongodb/data/config.log

logappend = true

bind_ip = 10.212.46.5

port = 27019

fork = true

Amongoshard05(10.212.70.21 )

--$vi shard14.conf

shardsvr = true

replSet = shard1

port = 27017

dbpath = /data/mongodb/data/shard14

oplogSize = 100

logpath = /data/mongodb/data/shard14.log

logappend = true

maxConns = 10000

pidfilepath = /data/mongodb/data/shard14.pid

bind_ip = 10.212.70.21

rest = true

fork = true

noprealloc = true

directoryperdb = true

--$vi shard24.conf

shardsvr = true

replSet = shard2

port = 27018

dbpath = /data/mongodb/data/shard24

oplogSize = 100

logpath = /data/mongodb/data/shard24.log

logappend = true

maxConns = 10000

pidfilepath = /data/mongodb/data/shard24.pid

bind_ip = 10.212.70.21

rest = true

fork = true

noprealloc = true

directoryperdb = true

--$vi configsvr.conf

dbpath = /data/mongodb/data/config

logpath = /data/mongodb/data/config.log

logappend = true

bind_ip = 10.212.70.21

port = 27019

fork = true

3.mongodb的启动:

Amongoshard01(10.212.74.43 )

--$cd /data/mongodb/bin

--./mongod -shardsvr -f /data/mongodb/data/shard11.conf

--./mongos -configdb 10.212.84.4:27019,10.212.46.5:27019,10.212.70.21:27019 -port 30000 -chunkSize 5 -logpath /data/mongodb/data/mongos.log -logappend -fork

Amongoshard02(10.212.84.4 )

--$cd /data/mongodb/bin

--./mongod -shardsvr -f /data/mongodb/data/shard12.conf

--./mongod -shardsvr -f /data/mongodb/data/shard13.conf

--./mongod -configsvr -f /data/mongodb/data/config.conf

--./mongos -configdb 10.212.84.4:27019,10.212.46.5:27019,10.212.70.21:27019 -port 30000 -chunkSize 5 -logpath /data/mongodb/data/mongos.log -logappend -fork

Amongoshard03(10.212.98.23 )

--$cd /data/mongodb/bin

--./mongod -shardsvr -f /data/mongodb/data/shard21.conf

--./mongos -configdb 10.212.84.4:27019,10.212.46.5:27019,10.212.70.21:27019 -port 30000 -chunkSize 5 -logpath /data/mongodb/data/mongos.log -logappend -fork

Amongoshard04(10.212.46.5 )

--$cd /data/mongodb/bin

--./mongod -shardsvr -f /data/mongodb/data/shard22.conf

--./mongod -shardsvr -f /data/mongodb/data/shard23.conf

--./mongod -configsvr -f /data/mongodb/data/config.conf

--./mongos -configdb 10.212.84.4:27019,10.212.46.5:27019,10.212.70.21:27019 -port 30000 -chunkSize 5 -logpath /data/mongodb/data/mongos.log -logappend -fork

Amongoshard05(10.212.70.21 )

--$cd /data/mongodb/bin

--./mongod -shardsvr -f /data/mongodb/data/shard14.conf

--./mongod -shardsvr -f /data/mongodb/data/shard24.conf

--./mongod -configsvr -f /data/mongodb/data/config.conf

4.初始化副本集:

第一个副本集:

--#su - monogdb

--$cd /data/mongodb/bin

--$./mongo 10.212.74.43:27017

>use admin

>config={_id:'shard1',members:[{_id:0,host:'10.212.74.43:27017',priority:2},{_id:1,host:'10.212.84.4:27017',priority:1},{_id:2,host:'10.212.84.4:27018',arbiterOnly:true},{_id:3,host:'10.212.70.21:27017',priority:0}]};

>rs.initiate(config)

Shard1:PRIMARY>rs.status()

第二个副本集:

--#su - monogdb

--$cd /data/mongodb/bin

--$./mongo 10.212.74.43:27017

>use admin

>config={_id:'shard2',members:[{_id:0,host:'10.212.98.23:27017',priority:2},{_id:1,host:'10.212.46.5:27017',priority:1},{_id:2,host:'10.212.46.5:27018',arbiterOnly:true},{_id:3,host:'10.212.70.21:27018',priority:0}]};

>rs.initiate(config)

Shard2:PRIMARY>rs.status()

5.配置sharding:

Login到任意一台mongos机器上:

--#su - monogdb

--$cd /data/mongodb/bin

--$./mongo 10.212.74.43:30000/admin

mongos>db.runCommand( { addshard : "shard1/10.212.74.43:27017,10.212.84.4:27017,10.212.84.4:27018,10.212.70.21:27017",name:"shard1",maxsize:2048000});

mongos>db.runCommand( { addshard : "shard2/10.212.98.23:27017,10.212.46.5:27017,10.212.46.5:27018,10.212.70.21:27018",name:"shard2",maxsize:2048000});

mongos>db.runCommand({listshards:1})

6.数据库分片以及Collection分片:

--#su - monogdb

--$cd /data/mongodb/bin

--$./mongo 10.212.74.43:30000/admin

mongos>db.runCommand( { enablesharding : “<dbname>” } );

mongos> db.runCommand( { shardcollection : “<namespace>”,key : <shardkeypatternobject> });

注意:

--数据库中不同的collection将被存放在不同的shard上;

--一个collection仍旧存放在同一个shard上,要使单个collection也分片,还需单独对collection作些操作;

--分片的collection系统会自动创建一个索引(也可用户提前创建好);

--分片的collection只能有一个在分片key上的唯一索引,其它唯一索引不被允许

四、    测试与总结

使用for(i=1;i<=100000;i++){db.test.insert({_id:i})}语句对环境进行测试并查看分片情况。

部署时遇到的问题及解决方法:

问题1:如果在从机上查看数据时出现不能查看错误时,则在从机上执行命令:db.getMongo().setSlave0k()。再查询就可以了。Mongodb3.0以上版本的使用db.getMongo().setSlave0k()或rs.slaveOk()之后退出再次登录时还是会出现此错误,需要编辑文件:#vi ~/.mongorc.js添加rs.slaveOk();即可。

问题2:配置sharding后需注意maxSize值得指定,若发现数据开始可以分片后来不行则可能是数据储存超出maxSize的大小,调整之后即可。maxSize值(单位是M)表示一个shard可使用的磁盘大小,超出的数据不会分片但会存储在primary sharding上。使用一下语句指定或更改其大小:

第一种:

>db.runCommand({addshard:”shardIP:port”,maxSize:20480})

第二种:

>use config

>db.shards.update({_id:”[shardname]”},{$set:{maxSize:20480}})

[原创]MongoDB综合实例二的更多相关文章

  1. [原创]MongoDB综合实例一

    CentOS-6.5单机实现mongoDB分片 环境:1)CentOS 6.5系统      2)IP:本机3)MongoDB:MongoDB-linux-x86_64-2.6.1 实现:两个副本集s ...

  2. Android笔记三十四.Service综合实例二

    综合实例2:client訪问远程Service服务 实现:通过一个button来获取远程Service的状态,并显示在两个文本框中. 思路:如果A应用须要与B应用进行通信,调用B应用中的getName ...

  3. ReportingServies——SQLServer报表开发综合实例

    如果我们安装了sqlserver2008 R2,将会自动安装一个报表开发工具 不要以为此报表开发工具只适合于sqlserver2008,其实在sqlserver2012中也是支持的,事实上我现在项目中 ...

  4. Loadrunner 关联 web_custom_request综合实例

    Loadrunner 关联 web_custom_request综合实例 Loadrunner 关联web_custom_request,针对自带的订票系统的一个综合实例,相信看了本文大家对学习loa ...

  5. 使用VS2012 开发SharePoint 2013 声明式的action(activity) 综合实例

    本文讲述使用VS2012 开发SharePoint 2013 声明式的action 综合实例. 需求同: http://blog.csdn.net/abrahamcheng/article/detai ...

  6. mongodb入门教程二

    title: mongodb入门教程二 date: 2016-04-07 10:33:02 tags: --- 上一篇文章说了mongodb最基本的东西,这边博文就在深入一点,说一下mongo的一些高 ...

  7. C语言库函数大全及应用实例二

    原文:C语言库函数大全及应用实例二                                              [编程资料]C语言库函数大全及应用实例二 函数名: bioskey 功 能 ...

  8. 一个综合实例讲解vue的基础知识点。

    本文通过一个简单的实例来讲解一下vue的基本知识点.通过这个综合实例的讲解,vue的基础知识就会掌握的差不多了. 首先看一下项目的效果:

  9. WebGL多模型光照综合实例

      原文地址:WebGL多模型光照综合实例   WebGL是一个非常的接近硬件底层的光栅化API, 从非常类似C/C++风格的API调用方式就可以看出来, 习惯了高级语言的我们会觉得很不友好,觉得特别 ...

随机推荐

  1. 对quartz定时任务的初步认识

    已经好久没有写技术博文了,今天就谈一谈我前两天自学的quartz定时任务吧,我对quartz定时任务的理解,就是可以设定一个时间,然后呢,在这个时间到的时候,去执行业务逻辑,这是我的简单理解,接下来看 ...

  2. android通过代码获取华为手机的EMUI系统版本号

    因为app中用到华为推送,但是华为推送在不同版本上是存在不同问题的,需要单独来处理. 那么最基本的问题是要获取EMUI系统的版本号. 上网翻了很多博客帖子,基本上是在获取root权限下去读取/syst ...

  3. 【树莓派】修改树莓派盒子MAC地址

    用树莓派盒子,在某些客户方实施过程中,不同客户的网络环境对树莓派盒子的要求不同,网络管理配置要求MAC地址和IP绑定. 一种情况下,查询盒子的MAC地址,添加到网络管理的路由规则中即可: 另一种情况下 ...

  4. Android学习探索之运用MVP设计模式实现项目解耦

    前言: 一直致力于提高开发效率降低项目耦合,今天想抽空学习一下MVP架构设计模式,学习一下如何运用到项目中. MVP架构设计模式 MVP模式是一种架构设计模式,也是一种经典的界面模式.MVP中的M代表 ...

  5. JavaScript高级程序设计---学习笔记(五)

    1.2D上下文 1)填充与描边 填充和描边的两个操作取决于两个属性:fillStyle和strokeStyle.两个属性的值可以是字符串.渐变对象或模式对象,默认值都是#000000 例: html: ...

  6. storm kafkaSpout 踩坑问题记录! offset问题!

    整合kafka和storm例子网上很多,自行查找 问题描述: kafka是之前早就搭建好的,新建的storm集群要消费kafka的主题,由于kafka中已经记录了很多消息,storm消费时从最开始消费 ...

  7. HDU 5558 后缀数组+二分

    题意有一些绕,但其实就是对于不断变化的i,求以j(0=j<i)使得suffix[j]与suffix[i]的最长公共前缀最长,如果有多个j,则取最小的j. 可以在rank数组中二分,在1-rank ...

  8. 闭包(匿名函数) php

    php中的闭包,之前不理解.以前项目中虽然有用到,也是别人怎么用,自己也跟着怎么用,也没具体去看一下,时间长了就忘了,也不知道闭包是怎么回事.今天网上搜集了关于php闭包相关的文章,看了7,8篇,干货 ...

  9. linq 为什么要用linq linq写法

    LINQ,语言集成查询(Language Integrated Query)是一组用于c#和Visual Basic语言的扩展.它允许编写C#或者Visual Basic代码以查询数据库相同的方式操作 ...

  10. JDK与Apache Tomcat服务器的安装步骤

    先解释一下JDK和Tomcat是什么: Tomcat 服务器是一个免费的开放源代码的Web 应用服务器,属于轻量级应用服务器,在中小型系统和并发访问用户不是很多的场合下被普遍使用,是开发和调试JSP ...