mongoDB's GridFS used with replicaSet and sharding

版本: 
mongodb 1.6.5 x64 bin

环境图:


 
 
看到这个图是不是有点像RAID1/0的存储方式.

描述:
Server1 : 172.16.3.174
/app/mongodb1.6.5/bin/mongod --config /app/mongodb1.6.5/conf/mongod1953.conf --shardsvr --replSet rep/172.16.3.176:1954
/app/mongodb1.6.5/bin/mongod --config /app/mongodb1.6.5/conf/mongod1954.conf --shardsvr --replSet rep1/172.16.3.176:1953
Server2 : 172.16.3.176
/app/mongodb1.6.5/bin/mongod --config /app/mongodb1.6.5/conf/mongod1954.conf --shardsvr --replSet rep/172.16.3.174:1953
/app/mongodb1.6.5/bin/mongod --config /app/mongodb1.6.5/conf/mongod1953.conf --shardsvr --replSet rep1/172.16.3.174:1954
Server3 : 172.16.3.33
/app/mongodb1.6.5/bin/mongod --config /app/mongodb1.6.5/conf/mongod1953.conf --shardsvr --replSet rep2/172.16.3.39:1954
/app/mongodb1.6.5/bin/mongod --config /app/mongodb1.6.5/conf/mongod1954.conf --shardsvr --replSet rep3/172.16.3.39:1953
Server4 : 172.16.3.39
/app/mongodb1.6.5/bin/mongod --config /app/mongodb1.6.5/conf/mongod1954.conf --shardsvr --replSet rep2/172.16.3.33:1953
/app/mongodb1.6.5/bin/mongod --config /app/mongodb1.6.5/conf/mongod1953.conf --shardsvr --replSet rep3/172.16.3.33:1954
/app/mongodb1.6.5/bin/mongos --port 1956 --configdb 172.16.3.67:1955 --fork --logpath /var/log/mongo/mongodb_1956.log --logappend
Server5 : 172.16.3.67
/app/mongodb1.6.5/bin/mongod --config /app/mongodb1.6.5/conf/mongod1955.conf --configsvr

配置文件 : 
[root@db6 conf]# cat mongod1953.conf 
port=1953
fork=true
logpath=/var/log/mongo/mongodb_1953.log
logappend=true
dbpath=/mongodata/data1953
directoryperdb=true
noauth=true
nohttpinterface=true
nssize=128
oplogSize=512
maxConns=1000

[root@db6 conf]# cat mongod1954.conf 
port=1954
fork=true
logpath=/var/log/mongo/mongodb_1954.log
logappend=true
dbpath=/mongodata/data1954
directoryperdb=true
noauth=true
nohttpinterface=true
nssize=128
oplogSize=512
maxConns=1000

[root@kefu conf]# cat mongod1955.conf 
port=1955
fork=true
logpath=/var/log/mongo/mongodb_1955.log
logappend=true
dbpath=/mongodata/data1955
directoryperdb=true
noauth=true
nohttpinterface=true
nssize=128
oplogSize=512
maxConns=1000

# 初始化replica
> db.runCommand({"replSetInitiate" : {
... "_id" : "rep1",                     
... "members" : [
... {"_id" : 1,"host" : "172.16.3.174:1953"},
... {"_id" : 2,"host" : "172.16.3.176:1954"}]}})
{
        "info" : "Config now saved locally.  Should come online in about a minute.",
        "ok" : 1
}

> db.runCommand({"replSetInitiate" : {
... "_id" : "rep1",                     
... "members" : [
... {"_id" : 1,"host" : "172.16.3.176:1953"},
... {"_id" : 2,"host" : "172.16.3.174:1954"}]}})
{
        "info" : "Config now saved locally.  Should come online in about a minute.",
        "ok" : 1
}

> db.runCommand({"replSetInitiate" : {
... "_id" : "rep2",                     
... "members" : [
... {"_id" : 1,"host" : "172.16.3.33:1953"},
... {"_id" : 2,"host" : "172.16.3.39:1954"}]}})
{
        "info" : "Config now saved locally.  Should come online in about a minute.",
        "ok" : 1
}

> db.runCommand({"replSetInitiate" : {
... "_id" : "rep3",                     
... "members" : [
... {"_id" : 1,"host" : "172.16.3.39:1953"},
... {"_id" : 2,"host" : "172.16.3.33:1954"}]}})
{
        "info" : "Config now saved locally.  Should come online in about a minute.",
        "ok" : 1
}

# 上传文件
/app/mongodb1.6.5/bin/mongofiles -h 172.16.3.39:1956 -d test put ./SLES-11-SP1-DVD-x86_64-GM-DVD2.iso

# 允许数据库shard和collection shard
> 连接到mongos admin库,允许gridfs的files和chunks collection sharding
> use admin
> db.runCommand({"enablesharding" : "test"})
> db.runCommand({"shardcollection" : "test.fs.files", "key" : {"_id" : 1}})
> db.runCommand({"shardcollection" : "test.fs.chunks", "key" : {"_id" : 1}})

# 查看shard状态
> db.printShardingStatus()                             
--- Sharding Status --- 
  sharding version: { "_id" : 1, "version" : 3 }
  shards:
      { "_id" : "rep", "host" : "rep/172.16.3.174:1953" }
      { "_id" : "rep1", "host" : "rep1/172.16.3.176:1953" }
      { "_id" : "rep2", "host" : "rep2/172.16.3.33:1953" }
      { "_id" : "rep3", "host" : "rep3/172.16.3.39:1953" }
  databases:
        { "_id" : "admin", "partitioned" : false, "primary" : "config" }

# 查看gridfs上传文件后的状态
> db.fs.chunks.totalSize()
8905203712
> db.fs.chunks.stats()    
{
        "sharded" : true,
        "ns" : "test.fs.chunks",
        "count" : 23949,
        "size" : 6279589664,
        "avgObjSize" : 262206.7586955614,
        "storageSize" : 9065091584,
        "nindexes" : 2,
        "nchunks" : 27,
        "shards" : {
                "rep" : {
                        "ns" : "test.fs.chunks",
                        "count" : 10158,
                        "size" : 2663364768,
                        "avgObjSize" : 262193.81453041936,
                        "storageSize" : 4957868032,
                        "numExtents" : 31,
                        "nindexes" : 2,
                        "lastExtentSize" : 829803520,
                        "paddingFactor" : 1,
                        "flags" : 1,
                        "totalIndexSize" : 933888,
                        "indexSizes" : {
                                "_id_" : 434176,
                                "files_id_1_n_1" : 499712
                        },
                        "ok" : 1
                },
                "rep1" : {
                        "ns" : "test.fs.chunks",
                        "count" : 7865,
                        "size" : 2062311264,
                        "avgObjSize" : 262213.7652892562,
                        "storageSize" : 2380098048,
                        "numExtents" : 27,
                        "nindexes" : 2,
                        "lastExtentSize" : 400175616,
                        "paddingFactor" : 1,
                        "flags" : 1,
                        "totalIndexSize" : 827392,
                        "indexSizes" : {
                                "_id_" : 385024,
                                "files_id_1_n_1" : 442368
                        },
                        "ok" : 1
                },
                "rep2" : {
                        "ns" : "test.fs.chunks",
                        "count" : 3046,
                        "size" : 798730912,
                        "avgObjSize" : 262222.88640840445,
                        "storageSize" : 943973632,
                        "numExtents" : 22,
                        "nindexes" : 2,
                        "lastExtentSize" : 160821760,
                        "paddingFactor" : 1,
                        "flags" : 1,
                        "totalIndexSize" : 294912,
                        "indexSizes" : {
                                "_id_" : 139264,
                                "files_id_1_n_1" : 155648
                        },
                        "ok" : 1
                },
                "rep3" : {
                        "ns" : "test.fs.chunks",
                        "count" : 2880,
                        "size" : 755182720,
                        "avgObjSize" : 262216.22222222225,
                        "storageSize" : 783151872,
                        "numExtents" : 21,
                        "nindexes" : 2,
                        "lastExtentSize" : 134018304,
                        "paddingFactor" : 1,
                        "flags" : 1,
                        "totalIndexSize" : 278528,
                        "indexSizes" : {
                                "_id_" : 131072,
                                "files_id_1_n_1" : 147456
                        },
                        "ok" : 1
                }
        },
        "ok" : 1
}
> exit  

注意事项 : 
1. mongos和config db不能在同一台服务器,否则在addshard的时候会报如下错误
> db.runCommand({"addshard" : "rep/db5.product.sky-mobi.com.sh:1953"})
{
        "ok" : 0,
        "errmsg" : "can't use localhost as a shard since all shards need to communicate. either use all shards and configdbs in localhost or all in actual IPs "
}

2. shard的replicaSet的名字不能一样,否则会报如下错误 : 
> db.runCommand({"addshard" : "rep/172.16.3.174:1953"})
{ "shardAdded" : "rep", "ok" : 1 }
# 再次添加同一replica名字的shard,报错
> db.runCommand({"addshard" : "rep/172.16.3.176:1953"})
{
        "ok" : 0,
        "errmsg" : "E11000 duplicate key error index: config.shards.$_id_  dup key: { : \"rep\" }"
}
# 因此这里使用了rep , rep1 , rep2 , rep3

3. sharding 支持自动探测 replicaSet的存活状态,支持识别replicaSet的failover.

时间: 2024-11-05 09:56:14

mongoDB's GridFS used with replicaSet and sharding的相关文章

使用mongodb的gridfs当做文件存储系统如何?

问题描述 使用mongodb的gridfs当做文件存储系统如何? 项目中,对于文件的存放想找一个解决方案,文件有大有小,小的可能2K,大的可能10G以内. 目的想实现文件在多台服务器之间的备份,避免单台服务器物理损毁造成文件丢失 第一种方案:直接存放在linux的文件系统中,自行分目录存放.可是要自己实现备份,有点重新造轮子的感觉 第二种方案:就是标题提出的方案,使用mongodb的gridfs做文件保存,可以实现自动文件复制 第三种方案:使用fastdfs做文件保存,也可以实现自动文件复制 我

Use mongodb 1.8.1's replicaSet with auth,journal,keyFile feature

MongoDB replicSet 1.8.1 产品部署推荐: 1. 文件系统加载时使用参数noatime 2. no VM PAGEs 3. 推荐使用逻辑卷,文件系统推荐ext4或xfs 4. 3个full nodes 或 2个full nodes+1个arbiter node (最好是奇数个物理服务器,否则仲裁会有问题,例如两台物理机,两个mongod进程,相互网络不通的话,任何一台都无法达到majority,因此都无法成为primary.那就是只读了.因此本例的物理服务器只有2台是不合理的

mongoDB single db convert to replicaSet

以下以2.0.2为例 :  1. 修改主节点配置 # 其他配置不变 # 增加配置,例如 noauth = true oplogSize = 31280 journal = true journalCommitInterval = 40 2. 重启mongodb mongo 127.0.0.1:4321/admin db.shutdownServer() # 增加启动项 --replSet=set_name mongod --replSet=set_name -f mongod.conf 3. 初

MongoDB.NET 2.2.4驱动版本对Mongodb3.3数据库中GridFS增删改查_MongoDB

本文实例为大家分享了针对Mongodb3.3数据库中GridFS增删改查,供大家参考,具体内容如下 Program.cs代码如下: internal class Program { private static void Main(string[] args) { GridFSHelper helper = new GridFSHelper("mongodb://localhost", "GridFSDemo", "Pictures"); #re

PHP实现GRIDFS存储文件到MONGODB的教程

一般情况采取第一种方法,直接传文件,第二种方法适合生成的数据不用保存到本地再上传而是直接以二进制存到MongoDB,第三种方式是直接把表单上传的文件存进MongoDB. //初始化gridfs $conn = new Mongo(); //连接MongoDB $db = $conn->photos; //选择数据库 $grid = $db->getGridFS(); //取得gridfs对象   //gridfs有三种方式存储文件 //第一种直接存储文件 $id = $grid->sto

mongodb gridfs nginx 文件服务器安装配置

  gridfs是一种将大型文件存储在MongoDB的文件规范.所有官方支持的驱动均实现了GridFS规范.简单看一下官方说明:When to Use GridFSIn MongoDB, use GridFS for storing files larger than 16 MB.In some situations, storing large files may be more efficient in a MongoDB database than on a system-level fi

PHP MongoDB GridFS 存储文件的方法详解_MongoDB

<?php //初始化gridfs $conn = new Mongo(); //连接MongoDB $db = $conn->photos; //选择数据库 $grid = $db->getGridFS(); //取得gridfs对象 gridfs有三种方式存储文件 第一种直接存储文件 $id = $grid->storeFile("./logo.png"); 第二种存储文件二进制流 $data = http://www.bkjia.com/PHPjc/get

基于MongoDb的S3实现

[原文]http://www.fuchaoqun.com/2010/05/s3-on-mongodb-with-php/ 原理是利用MongoDb的GridFS,伸展性方面交由MongoDb的auto sharding去实现,这里用PHP给MongoDb绑了个S3出来,支持选择文件存储节点,支持文件分目录存储,这样的好处是对于一些受时间影响比较明显的文件,可以按照年月的形式存储,减轻历史包袱. 首先,配置MongoDb GridFS节点信息:<?php$s3Config = array(   

MongoDB资料大全

MongoDB是一个开源的.基于分布式的.面向文档存储的非关系型数据库.是非关系型数据库当中功能最丰富.最像关系数据库的.MongoDB由C++编写, MongoDB可以运行在Windows.unix.OSX.Solaris系统上,支持32位和64位应用,提供多种编程语言的驱动程序.旨在为WEB应用提供可扩展的高性能数据存储解决方案.MongoDB高性能.易部署.易使用,存储数据非常方便.MongoDB最大的特点是支持的查询语言非常强大,其语法有点类似于面向对象的查询语言,几乎可以实现类似关系数