美文网首页程序员技术干货
mongodb sharding搭建过程

mongodb sharding搭建过程

作者: randyjia | 来源:发表于2016-01-12 14:20 被阅读541次

搭建mongodb sharding过程如下,其实手册都有,不过还是记录一下。

概述

分为三个步骤

  • 搭建configdb,并启动
  • 搭建sharding,并启动
  • 启动mongos,开启sharding
    其实configdb和sharding都是mongod进程,使用mongos来操作sharding,这样applycation不用关心数据如何存储。
    测试目录如下:


    屏幕快照 2016-01-12 下午1.51.13.png

    正式环境中,注意如下变化

  • configdb应该搭建为replyset(3.2新增)
  • configdb的应该处于不同的机器
  • sharding最好处于不同的机器

1. 搭建configdb

以configdb1为例
<pre>
mkdir configdb1
cd configdb1
touch mongod.conf
mkdir data
mkdir log
</pre>
其中,mongod.conf的内容如下:
<pre>
systemLog:
destination: file
path: /Users/mohe/work/mongodb/shards/configdb1/log/mongo.log
logAppend: true
timeStampFormat: iso8601-local
storage:
dbPath: /Users/mohe/work/mongodb/shards/configdb1/data
directoryPerDB: true
syncPeriodSecs: 30
engine: wiredTiger
journal:
enabled: true

net:
bindIp: 127.0.0.1
port: 37001

processManagement:
fork: true

sharding:
clusterRole: configsvr
</pre>
启动configdb1即可
<pre>
mongd --config mongod.conf
</pre>
类似启动configdb2,configdb3

搭建sharding

以sharing1为例
<pre>
mkdir shards1
cd shards1
touch mongod.conf
mkdir data
mkdir log
</pre>
其中,mongod.conf的内容如下
<pre>
systemLog:
destination: file
path: /Users/mohe/work/mongodb/shards/shards1/log/mongo.log
logAppend: true
timeStampFormat: iso8601-local
storage:
dbPath: /Users/mohe/work/mongodb/shards/shards1/data
directoryPerDB: true
syncPeriodSecs: 30
engine: wiredTiger
journal:
enabled: true
net:
bindIp: 127.0.0.1
port: 37011
processManagement:
fork: true
</pre>
启动sharding1
<pre>
mongd --config mongod.conf
</pre>
类似,启动sharding2,sharding3

启动mongos,开启sharding

<pre>
mkdir mongos
cd mongos
touch mongos.conf
mkdir log
</pre>
注意,mongos没有data
mongos文件内容如下
<pre>
systemLog:
destination: file
path: /Users/mohe/work/mongodb/shards/mongos/log/mongo.log
logAppend: true
timeStampFormat: iso8601-local

net:
bindIp: 127.0.0.1
port: 37000

processManagement:
fork: true

sharding:
configDB: "127.0.0.1:37001,127.0.0.1:37002,127.0.0.1:37003"
</pre>

启动mongos
<pre>
mongos -f mongos.conf
</pre>
连上mongos
<pre>
mongo 127.0.0.1:37000
</pre>

加入sharding
在mongo终端里面执行
<pre>
sh.addShard("127.0.0.1:37011"); ## sharding1加入
sh.addShard("127.0.0.1:37012"); ## sharding2加入
sh.addShard("127.0.0.1:37013"); ## sharding3加入
</pre>

enable想要sharding的数据库和集合
<pre>
sh.enableSharding("be3");
sh.shardCollection("be3.log",{ "sid": "hashed"});
</pre>
shardCollection的这里如果选择了hashed的索引,mongos会自动在各个sharding对应的字段建立一个hash_index索引。

测试

在mongos终端里面执行
<pre>
for(var i=1; i<10000; i++){var s_value = (i % 10) + 1;
db.log.insert({uid:i,sid:s_value});}
</pre>
然后连上各个mongod进程,查看是sharding分布
各个执行
<pre>
db.log.count();
</pre>
结果如下:
sharding001: 2000
sharding002: 3000
sharding002: 4999

相关文章

  • mongodb sharding搭建过程

    搭建mongodb sharding过程如下,其实手册都有,不过还是记录一下。 概述 分为三个步骤 搭建confi...

  • MongoDB分片集群搭建

    本文主要介绍了mongoDB分片集群概念,以及分片集群搭建过程,方便下次参考。 概念 分片(sharding)是一...

  • Mongodb Sharding 搭建

    前面我们介绍了mongodb的架构。下面我们就来实际搭建一个mongodb sharded cluster。 首先...

  • Mongodb高可用集群搭建

    方案选项 Mongodb的三种集群方式的搭建:Replica Set / Sharding / Master-Sl...

  • mongodb集群Replica Set搭建

    MongoDB集群有三种搭建方案,分别为Replica Set / Sharding / Master-Slave...

  • mongodb笔记08--分片

    分片(sharding): mongodb的集群搭建方式主要有三种,主从模式,Replica set模式,shar...

  • 搭建MongoDB Sharding集群

    当MongoDB存储海量的数据时,一台机器可能不足以存储数据,也可能不足以提供可接受的读写吞吐量。这时,我们就可以...

  • mongodb分片

    mongodb 分片: Sharding组件: sharding最最最关键的是sharding key(片键) s...

  • MongoDB主从

    mongodb的集群搭建方式主要有三种,主从模式,Replica set模式,sharding模式, 三种模式各有...

  • mongdb sharding key选择

    搭建mongod sharding之后,加入sharding之后,需要选择sharding-key。为了达到均匀分...

网友评论

    本文标题:mongodb sharding搭建过程

    本文链接:https://www.haomeiwen.com/subject/xoqckttx.html