MongoDB分片搭建

一、环境

$ cat /etc/redhat-release
CentOS Linux release 7.0.1406 (Core)
$ uname -a
Linux zhaopin-2-201 3.10.0-123.el7.x86_64 #1 SMP Mon Jun 30 12:09:22 UTC 2014 x86_64 x86_64 x86_64 GNU/Linux
$ mongo --version
MongoDB shell version: 3.0.6

二、配置Shard Server

1.创建目录
$ sudo mkdir -p /data/mongodb/{data/{sh0,sh1},backup/{sh0,sh1},log/{sh0,sh1},conf/{sh0,sh1}}
2.准备配置文件

第一个分片:

$ sudo vim /data/mongodb/conf/sh0/mongodb.conf
# base
port = 27010
maxConns = 800
filePermissions = 0700
fork = true
noauth = true
directoryperdb = true
dbpath = /data/mongodb/data/sh0
pidfilepath = /data/mongodb/data/sh0/mongodb.pid
oplogSize = 10
journal = true
# security
nohttpinterface = true
rest = false
# log
logpath = /data/mongodb/log/sh0/mongodb.log
logRotate = rename
logappend = true
slowms = 50
replSet = sh0
shardsvr = true

第二个分片:

$ sudo vim /data/mongodb/conf/sh1/mongodb.conf
# base
port = 27011
maxConns = 800
filePermissions = 0700
fork = true
noauth = true
directoryperdb = true
dbpath = /data/mongodb/data/sh1
pidfilepath = /data/mongodb/data/sh1/mongodb.pid
oplogSize = 10
journal = true
# security
nohttpinterface = true
rest = false
# log
logpath = /data/mongodb/log/sh1/mongodb.log
logRotate = rename
logappend = true
slowms = 50
replSet = sh1
shardsvr = true
3.启动Shard Server
$ sudo /opt/mongodb/bin/mongod --config /data/mongodb/conf/sh0/mongodb.conf
about to fork child process, waiting until server is ready for connections.
forked process: 41492
child process started successfully, parent exiting
$ sudo /opt/mongodb/bin/mongod --config /data/mongodb/conf/sh1/mongodb.conf
about to fork child process, waiting until server is ready for connections.
forked process: 41509
child process started successfully, parent exiting
$ ps aux | grep mongo | grep -v grep
root     41492  0.5  0.0 518016 54604 ?        Sl   10:09   0:00 /opt/mongodb/bin/mongod --config /data/mongodb/conf/sh0/mongodb.conf
root     41509  0.5  0.0 516988 51824 ?        Sl   10:09   0:00 /opt/mongodb/bin/mongod --config /data/mongodb/conf/sh1/mongodb.conf
$ mongo --port 27010
MongoDB shell version: 3.0.6
connecting to: 127.0.0.1:27010/test
>
bye
$ mongo --port 27011
MongoDB shell version: 3.0.6
connecting to: 127.0.0.1:27011/test
>
bye

三、配置Config Server

1.创建目录
$ sudo mkdir -p /data/mongodb/{data/{cf0,cf1,cf2},backup/{cf0,cf1,cf2},log/{cf0,cf1,cf2},conf/{cf0,cf1,cf2}}
2.准备配置文件

第一个配置服务器:

$ sudo vim /data/mongodb/conf/cf0/config.conf
# base
port = 27000
maxConns = 800
filePermissions = 0700
fork = true
noauth = true
directoryperdb = true
dbpath = /data/mongodb/data/cf0
pidfilepath = /data/mongodb/data/cf0/config.pid
oplogSize = 10
journal = true
# security
nohttpinterface = true
rest = false
# log
logpath = /data/mongodb/log/cf0/config.log
logRotate = rename
logappend = true
slowms = 50
configsvr = true

第二个配置服务器:

$ sudo vim /data/mongodb/conf/cf1/config.conf
# base
port = 27001
maxConns = 800
filePermissions = 0700
fork = true
noauth = true
directoryperdb = true
dbpath = /data/mongodb/data/cf1
pidfilepath = /data/mongodb/data/cf1/config.pid
oplogSize = 10
journal = true
# security
nohttpinterface = true
rest = false
# log
logpath = /data/mongodb/log/cf1/config.log
logRotate = rename
logappend = true
slowms = 50
configsvr = true

第三个配置服务器:

$ sudo vim /data/mongodb/conf/cf2/config.conf
# base
port = 27002
maxConns = 800
filePermissions = 0700
fork = true
noauth = true
directoryperdb = true
dbpath = /data/mongodb/data/cf2
pidfilepath = /data/mongodb/data/cf2/config.pid
oplogSize = 10
journal = true
# security
nohttpinterface = true
rest = false
# log
logpath = /data/mongodb/log/cf2/config.log
logRotate = rename
logappend = true
slowms = 50
configsvr = true
3.启动
$ sudo /opt/mongodb/bin/mongod --config /data/mongodb/conf/cf0/config.conf
about to fork child process, waiting until server is ready for connections.
forked process: 41759
child process started successfully, parent exiting
$ sudo /opt/mongodb/bin/mongod --config /data/mongodb/conf/cf1/config.conf
about to fork child process, waiting until server is ready for connections.
forked process: 41774
child process started successfully, parent exiting
$ sudo /opt/mongodb/bin/mongod --config /data/mongodb/conf/cf2/config.conf
about to fork child process, waiting until server is ready for connections.
forked process: 42170
child process started successfully, parent exiting
$ ps aux | grep mongo | grep -v grep
root     41492  0.3  0.0 518016 54728 ?        Sl   10:09   0:06 /opt/mongodb/bin/mongod --config /data/mongodb/conf/sh0/mongodb.conf
root     41509  0.3  0.0 518016 54760 ?        Sl   10:09   0:06 /opt/mongodb/bin/mongod --config /data/mongodb/conf/sh1/mongodb.conf
root     41855  0.4  0.0 467828 51684 ?        Sl   10:25   0:03 /opt/mongodb/bin/mongod --config /data/mongodb/conf/cf0/config.conf
root     41870  0.4  0.0 467828 52312 ?        Sl   10:25   0:03 /opt/mongodb/bin/mongod --config /data/mongodb/conf/cf1/config.conf
root     42170  0.9  0.0 467824 52392 ?        Sl   10:37   0:00 /opt/mongodb/bin/mongod --config /data/mongodb/conf/cf2/config.conf

四、配置Query Routers

1.创建目录
$ sudo mkdir -p /data/mongodb/{data/{ms0,ms1},backup/{ms0,ms1},log/{ms0,ms1},conf/{ms0,ms1}}
2.准备配置文件

第一个配置服务器:

$ sudo vim /data/mongodb/conf/ms0/mongos.conf
# base
port = 30000
maxConns = 800
filePermissions = 0700
fork = true
pidfilepath = /data/mongodb/data/ms0/mongos.pid
# log
logpath = /data/mongodb/log/ms0/mongos.log
logRotate = rename
logappend = true
configdb = 172.30.2.201:27000,172.30.2.201:27001,172.30.2.201:27002

第二个配置服务器:

$ sudo vim /data/mongodb/conf/ms1/mongos.conf
# base
port = 30000
maxConns = 800
filePermissions = 0700
fork = true
pidfilepath = /data/mongodb/data/ms1/mongos.pid
# log
logpath = /data/mongodb/log/ms1/mongos.log
logRotate = rename
logappend = true
configdb = 172.30.2.201:27000,172.30.2.201:27001,172.30.2.201:27002
3.启动
$ sudo /opt/mongodb/bin/mongos --config /data/mongodb/conf/ms0/mongos.conf
about to fork child process, waiting until server is ready for connections.
forked process: 42233
child process started successfully, parent exiting
$ sudo /opt/mongodb/bin/mongos --config /data/mongodb/conf/ms1/mongos.conf
about to fork child process, waiting until server is ready for connections.
forked process: 42286
child process started successfully, parent exiting
$ ps aux | grep mongo | grep -v grep
root     41492  0.3  0.0 518016 54728 ?        Sl   10:09   0:06 /opt/mongodb/bin/mongod --config /data/mongodb/conf/sh0/mongodb.conf
root     41509  0.3  0.0 518016 54760 ?        Sl   10:09   0:07 /opt/mongodb/bin/mongod --config /data/mongodb/conf/sh1/mongodb.conf
root     41855  0.4  0.0 546724 37812 ?        Sl   10:25   0:03 /opt/mongodb/bin/mongod --config /data/mongodb/conf/cf0/config.conf
root     41870  0.4  0.0 546724 38188 ?        Sl   10:25   0:03 /opt/mongodb/bin/mongod --config /data/mongodb/conf/cf1/config.conf
root     42170  0.6  0.0 546720 41960 ?        Sl   10:37   0:00 /opt/mongodb/bin/mongod --config /data/mongodb/conf/cf2/config.conf
root     42233  0.5  0.0 233536 10188 ?        Sl   10:38   0:00 /opt/mongodb/bin/mongos --config /data/mongodb/conf/ms0/mongos.conf
root     42286  0.4  0.0 241728  9056 ?        Sl   10:38   0:00 /opt/mongodb/bin/mongos --config /data/mongodb/conf/ms1/mongos.conf

五、初始化单节点副本集

配置副本集的好处是为了高可用,配置单节点是我自己为了节省时间,后续添加节点和副本集的操作一样,分片的配置不需要修改

分片一:

$ mongo --port 27010
MongoDB shell version: 3.0.6
connecting to: 127.0.0.1:27010/test
> use admin
switched to db admin
> cfg={_id:"sh0", members:[ {_id:0,host:"172.30.2.201:27010"}] }
{
        "_id" : "sh0",
        "members" : [
                {
                        "_id" : 0,
                        "host" : "172.30.2.201:27010"
                }
        ]
}
> rs.initiate( cfg );
{ "ok" : 1 }
sh0:OTHER> rs.status();
{
        "set" : "sh0",
        "date" : ISODate("2015-10-09T02:45:39.178Z"),
        "myState" : 1,
        "members" : [
                {
                        "_id" : 0,
                        "name" : "172.30.2.201:27010",
                        "health" : 1,
                        "state" : 1,
                        "stateStr" : "PRIMARY",
                        "uptime" : 2197,
                        "optime" : Timestamp(1444358731, 1),
                        "optimeDate" : ISODate("2015-10-09T02:45:31Z"),
                        "electionTime" : Timestamp(1444358731, 2),
                        "electionDate" : ISODate("2015-10-09T02:45:31Z"),
                        "configVersion" : 1,
                        "self" : true
                }
        ],
        "ok" : 1
}
sh0:PRIMARY>
bye

分片二:

$ mongo --port 27011
MongoDB shell version: 3.0.6
connecting to: 127.0.0.1:27011/test
> cfg={_id:"sh1", members:[ {_id:0,host:"172.30.2.201:27011"}] }
{
        "_id" : "sh1",
        "members" : [
                {
                        "_id" : 0,
                        "host" : "172.30.2.201:27011"
                }
        ]
}
> rs.initiate( cfg );
{ "ok" : 1 }
sh1:OTHER> rs.status();
{
        "set" : "sh1",
        "date" : ISODate("2015-10-09T02:46:32.961Z"),
        "myState" : 1,
        "members" : [
                {
                        "_id" : 0,
                        "name" : "172.30.2.201:27011",
                        "health" : 1,
                        "state" : 1,
                        "stateStr" : "PRIMARY",
                        "uptime" : 2244,
                        "optime" : Timestamp(1444358790, 1),
                        "optimeDate" : ISODate("2015-10-09T02:46:30Z"),
                        "electionTime" : Timestamp(1444358790, 2),
                        "electionDate" : ISODate("2015-10-09T02:46:30Z"),
                        "configVersion" : 1,
                        "self" : true
                }
        ],
        "ok" : 1
}
sh1:PRIMARY>
bye

六、配置分片

$ mongo --port 30000
MongoDB shell version: 3.0.6
connecting to: 127.0.0.1:30000/test
mongos> use admin;
switched to db admin
mongos> sh.addShard("sh0/172.30.2.201:27010");
{ "shardAdded" : "sh0", "ok" : 1 }
mongos> sh.addShard("sh1/172.30.2.201:27011");
{ "shardAdded" : "sh1", "ok" : 1 }
mongos> use mydb;
switched to db mydb
mongos> db.createCollection("test");
{
        "ok" : 1,
        "$gleStats" : {
                "lastOpTime" : Timestamp(1444358911, 1),
                "electionId" : ObjectId("56172a4bc03d9b1667f8e928")
        }
}
mongos> sh.enableSharding("mydb");
{ "ok" : 1 }
mongos> sh.shardCollection("mydb.test", {"_id":1});
{ "collectionsharded" : "mydb.test", "ok" : 1 }
mongos> sh.status();
--- Sharding Status ---
  sharding version: {
        "_id" : 1,
        "minCompatibleVersion" : 5,
        "currentVersion" : 6,
        "clusterId" : ObjectId("561728b4030ea038bcb57fa0")
}
  shards:
        {  "_id" : "sh0",  "host" : "sh0/172.30.2.201:27010" }
        {  "_id" : "sh1",  "host" : "sh1/172.30.2.201:27011" }
  balancer:
        Currently enabled:  yes
        Currently running:  no
        Failed balancer rounds in last 5 attempts:  0
        Migration Results for the last 24 hours:
                No recent migrations
  databases:
        {  "_id" : "admin",  "partitioned" : false,  "primary" : "config" }
        {  "_id" : "mydb",  "partitioned" : true,  "primary" : "sh0" }
                mydb.test
                        shard key: { "_id" : 1 }
                        chunks:
                                sh0     1
                        { "_id" : { "$minKey" : 1 } } -->> { "_id" : { "$maxKey" : 1 } } on : sh0 Timestamp(1, 0)

可见分片已经配置完成了

时间: 2024-10-07 22:37:05

MongoDB分片搭建的相关文章

MongoDB 分片搭建

MongoDB 已经到了第三篇,同时也是搭建的最后一篇文章了. MongoDB 的分片搭建. 为什么使用分片? 我们为什么要使用分片呢? 因为现在的数据量越来越大了,为了扩大DB 的性能 以及吞吐量. MongoDB 分片构架 A MongoDB sharded cluster consists of the following components: shard: Each shard contains a subset of the sharded data. Each shard can

搭建mongodb分片

搭建mongodb分片 http://gong1208.iteye.com/blog/1622078 Sharding分片概念 这是一种将海量的数据水平扩展的数据库集群系统,数据分表存储在sharding的各个节点上,使用者通过简单的配置就可以很方便地构建一个分布式MongoDB集群. MongoDB 的数据分块称为 chunk.每个 chunk 都是 Collection 中一段连续的数据记录,通常最大尺寸是 200MB,超出则生成新的数据块. 要构建一个 MongoDB Sharding C

MongoDB 分片的原理、搭建、应用

一.概念: 分片(sharding)是指将数据库拆分,将其分散在不同的机器上的过程.将数据分散到不同的机器上,不需要功能强大的服务器就可以存储更多的数据和处理更大的负载.基本思想就是将集合切成小块,这些块分散到若干片里,每个片只负责总数据的一部分.通过一个名为mongos的路由进程进行操作,mongos知道数据和片的对应关系(通过配置服务器).大部分使用场景都是解决磁盘空间的问题,对于写入有可能会变差(+++里面的说明+++),查询则尽量避免跨分片查询.使用分片的时机: 1,机器的磁盘不够用了.

MongoDB 分片的原理、搭建、应用 !

一.概念: 分片(sharding)是指将数据库拆分,将其分散在不同的机器上的过程.将数据分散到不同的机器上,不需要功能强大的服务器就可以存储更多的数据和处理更大的负载.基本思想就是将集合切成小块,这些块分散到若干片里,每个片只负责总数据的一部分,最后通过一个均衡器来对各个分片进行均衡(数据迁移).通过一个名为mongos的路由进程进行操作,mongos知道数据和片的对应关系(通过配置服务器).大部分使用场景都是解决磁盘空间的问题,对于写入有可能会变差(+++里面的说明+++),查询则尽量避免跨

MongoDB分片实战

MongoDB分片实战(一):集群搭建 http://www.cnblogs.com/magialmoon/archive/2013/04/10/3013121.html MongoDB分片实战(二):Sharding http://www.cnblogs.com/zhanjindong/archive/2013/04/11/3015394.html MongoDB分片实战(三):性能和优化 http://www.cnblogs.com/zhanjindong/archive/2013/04/1

MongoDB分片存储的集群架构实现

MongoDB分片存储的集群架构实现 如果需要存储大量数据,或者系统的读写吞吐量很大的时候,单个server就很难满足需求了.这个时候我们可以使用MongoDB的分片机制来解决这些问题. 分片的基本概念 分片(sharding)是一种水平扩展(horizontal scaling)的方式,把一个大的数据集分散到多个片服务器上,所有的片服务器将组成一个逻辑上的数据库来存储这个大的数据集.分片对用户(应用层)是透明的,用户不会知道数据很被存放到哪个片服务器上. 这种方式有两个好处: 分片之后,每个片

NoSql数据库初探-mongoDB环境搭建

NoSQL数据库一改关系型数据库的缺点,更容易的集成.分布式.无模式.故障恢复等特点,正在一步步餐食关系型数据库的市场,作为一个与时俱进的码农了解一下新技术是必须的,尤其是在读了<NoSql精粹>之后,更是想体验一下NoSql数据库的威力. MongoDB是一种文档数据库,也就是说对于领域模型中的每一个聚合来讲,都会作为一个文档来存储. MongoDB有如下优点: 面向文档存储.全索引支持.同步机制和高访问性.自动分片.查询.灵活的聚集和数据处理.映射化简.文件存储.   第一步:安装 从版本

MongoDB 分片集群实战

背景 在如今的互联网环境下,海量数据已随处可见并且还在不断增长,对于如何存储处理海量数据,比较常见的方法有两种: 垂直扩展:通过增加单台服务器的配置,例如使用更强悍的 CPU.更大的内存.更大容量的磁盘,此种方法虽然成本很高,但是实现比较简单,维护起来也比较方便. 水平扩展:通过使用更多配置一般的服务器来共同承担工作负载,此种方法很灵活,可以根据工作负载的大小动态增减服务器的数量,但是实现比较复杂,得有专门的人员来运维. Databases for MongoDB 试用 IBM Cloud 上提

mongoDB(三) mongoDB分片集群

mongoDB分片集群 介绍 解决数据分片,防止数据丢失生产环境需要擦用分片+副本集的部署方式 组成部分 route: 提供入口,不存储数据 configserver: 存储元数据信息,建议使用副本集 shardserver: 数据存储服务,存储真正数据, 也许要使用副本集 依赖关系 当数据插入时,需要从configsrv知道数据要插入哪个shardsrv分片 当用户获取数据时,需要从configsrv知道数据存储在哪个shardsrv分片 集群搭建 使用同一份mongodb二进制文件 修改对应