利用mongodb实现分布式WEB图片存储

来源:互联网 发布:手机表格软件中文版 编辑:程序博客网 时间:2024/06/05 09:37
利用mongodb实现分布式WEB图片存储
2012-12-22 14:00:33
标签:mongodb 分布式图片存储 nginx 分布式网站
 。 的确在站比较小得时候,附件不多的时候,当然这样处理很好。但是当 附件数量海里去了~~ 那这样存就蛋疼了, 备份是个问题,硬盘IO瓶颈那会也会凸显出来。问题接踵而来。那会,就需要分布式文件存储了。    利用 GridFS 存储文件, 再利用 mongodb 的分片(sharding) 就可以做到海量存储了。 当然本文只是一个知识扩展~ 生产使用此方案出问题,与本人无关~~别找我。

  1. 等吧~~。一会就完了。完了还要配置下,做FB的 这个懂的    vi   /etc/rc.conf:
    mongod_dbpath="/data/mongodb"  # DB存储目录
      复制代码

      1. 写个上传表单:demo.php  
             上传文件: <input type="file" name="pic"/>
      2. 接受表单把上传的文件存储到 mongodb : gridfs.php
                $con = new Mongo("mongodb://192.168.1.205"); // Connect to Mongo Server
      3.         $grid->storeUpload("pic", 'demo.jpg');
      4. 。运行程序,提交表单,然后去mongo 看看 存上没。
        MongoDB shell version: 1.8.3
      5. admin   (empty)
      6. xxoo    0.0625GB  #  看到 xxoo了吧~~ 再看下刚上面的代码。在 selectDB的时候,我们选择的就是xxoo 数据库,存在了证明文件存储上了。
      7. > show collections  # 显示数据库里的集合(你就当时表吧)
      8. system.indexes
      9. fs.chunks     块集合, gridfs 会把大文件分成一小块一小块的来进行存储,所以可以存储大文件。

        > db.fs.files.find()
      10. 里面存储着我们刚上传的文件的信息,比如 filename  文件名。   length 文件大小   chunkSize 分块大小。  文件就这样给存储到 mongodb了。
        好的。到这里。 分布式文件存储,就完成了一半了~~。 这半就是文件存储。 那分布式呢? 接下来我们就分布式吧~~ 淡定~这个分布式其实还是比较简单的。
        2、mongos 路由服务器   : 192.168.1.203       把一些存储节点的路由信息存储到内存
        首先配置mongod服务器。 192.168.1.202:
        OK。确定启动成功:   ps  aux  | grep mongo     确定了木问题了继续。
        /usr/local/bin/mongos --configdb  192.168.1.202:27019   --logpath=/data/configdb/mongo.log  --port 27017 --logappend --fork
      复制代码

    1. 添加存储节点到  mongos 路由服务上:
      db.runcOMMAND({addshard:"192.168.1.205:27017"})
    复制代码


db.runCommand({"enablesharding" : "xxoo"})
  • 好了 分布式环境搭建完了。 那我们回头看下代码方面,只需要修改下访问地址,所有的请求都应该通过  "mongos 路由服务器" 这样才能自动帮你分片嘛~~ 
            $con = new Mongo("mongodb://192.168.1.203"); // 连接到  mongos 路由服务器,让它帮我们分片
  •         $grid->storeUpload("pic", 'demo.jpg');
  • 文件上传、分布式存储。都实现了。  nginx 开始上场了。 我们用 nginx 来做静态资源的服务器,前面我们装得 nginx-gridfs  模块,里面自带了访问 mongodb  的驱动。那是相当方便呀。看下 nginx 配置:
            listen       8088;
  • #重点在这里
  •             mongo 192.168.1.203:27017;
  •         # redirect server error pages to the static page /50x.html
  •                  location = /50x.html {

  • <li lucida="" console',="" 'courier="" new',="" serif;="" font-size:="" 12px;="" line-height:="" 1.8em;"="" style="padding: 0px 0px 0px 10px; margin: 0px 0px 0px 2em; word-wrap: break-word; list-style-type: decimal-leading-zero;">        # deny access to .htaccess files, if Apache's document root
    <li lucida="" console',="" 'courier="" new',="" serif;="" font-size:="" 12px;="" line-height:="" 1.8em;"="" style="padding: 0px 0px 0px 10px; margin: 0px 0px 0px 2em; word-wrap: break-word; list-style-type: decimal-leading-zero;">        # concurs with nginx's one
  •             deny  all;
  • 重点理解这些:
    mongo 192.168.1.203:27017;
    [php]http://192.168.1.203:8088/demo.jpg[/php]    就可以显示我们要的图片了。
    #!/usr/bin/python
  • from sys import argv
  •     content = ''
  •         line = out.readline()
  •             break
  • if __name__ == '__main__':
    <li lucida="" console',="" 'courier="" new',="" serif;="" font-size:="" 12px;="" line-height:="" 1.8em;"="" style="padding: 0px 0px 0px 10px; margin: 0px 0px 0px 2em; word-wrap: break-word; list-style-type: decimal-leading-zero;">    '''
  •     if len(argv) > 1:
  •             
  •             
  •             config_mongos = {'host':'192.168.1.202:27019','logpath':'/data/config/configdb.log','port':27017}
  •         elif para == 'shard':
  •             print runShell("/usr/local/bin/mongod --shardsvr --dbpath=%s --logpath=%s --port %d --logappend --fork" % (mongodb['dbpath'],mongodb['logpath'],mongodb['port']))
  •             import pymongo

  •                 db.runCommand({'addshard':shardHOST[ip]})
  •             db.runCommand({'enableSharding':'test'})
  •     else:
    <li lucida="" console',="" 'courier="" new',="" serif;="" font-size:="" 12px;="" line-height:="" 1.8em;="" background-color:="" rgb(255,="" 255,="" 255);="" color:="" rgb(51,="" 102,="" 153);="" background-position:="" initial="" initial;="" background-repeat:="" initial;"="" style="padding: 0px 0px 0px 10px; margin: 0px 0px 0px 2em; word-wrap: break-word; list-style-type: decimal-leading-zero;">        print "no argv!"
    <li lucida="" console',="" 'courier="" new',="" serif;="" font-size:="" 12px;="" line-height:="" 1.8em;"="" style="padding: 0px 0px 0px 10px; margin: 0px 0px 0px 2em; word-wrap: break-word; list-style-type: decimal-leading-zero;">
  • 复制代码
    转载自:http://bbs.phpchina.com/forum.php?mod=viewthread&tid=226415
    0 0