安装MongoDB Install MongoDB on Ubuntu

MongoDB 是一个基于分布式文件存储的数据库。由C++语言编写。旨在为WEB应用提供可扩展的高性能数据存储解决方案。
 

MongoDB是一个介于关系数据库和 非关系数据库之间的产品,是非关系数据库当中功能最丰富,最像关系数据库的。他支持的数据结构非常松散,是类似json的bson格式,因此可以存储比较 复杂的数据类型。Mongo最大的特点是他支持的查询语言非常强大,其语法有点类似于面向对象的查询语言,几乎可以实现类似关系数据库单表查询的绝大部分 功能,而且还支持对数据建立索引。

MongoDB是一个开源的,高性能,无模式(或者说是模式自由),使用C++语言编写 的面向文档的数据库。正因为MongoDB是面向文档的,所以它可以管理类似JSON的文档集合。又因为数据可以被嵌套到复杂的体系中并保持可以查询可索 引,这样一来,应用程序便可以以一种更加自然的方式来为数据建模。

   下面介绍MongoDB的特点:

   1 统一的UTF-8编码

      不是UTF-8编码集合的数据也可以通过使用一种特殊的二进制数据类型来保存,查询。

   2 跨平台支持

      二进制文件可以再Windows,Linux,OS X和Solaris平台上使用。MongoDB可以在大多数小端系统上编译通过。

   3 支持丰富的类型

      支持 dates, regular expressions, code, binary data 等类型

   4 查询结果支持Cursor操作

   5 支持Ad hoc queriesAd hoc query:即席查询,数据库应用最普遍的一种查询,利用数据仓库技术,可以让用户随时可以面对数据库,获取所希望的数据,详细介绍见http://www.learn.geekinterview.com/data-warehouse/dw-basics/what-is-an-ad-hoc-query.html。)

      在MongoDB中,可以在任何时候查询任何一个field。它支持 range queries,regular expression searches 和其他特殊的查询类型。同时查询也可以包含用户定义的javascript函数。

   6  支持嵌套域的查询

       查询可以深入到嵌套的对象和数组中,如果下面的对象被插入到users集合:

       {
               "username" : "bob",
               "address" : {
                      "street" : "123 Main Street",
                      "city" : "Springfield",
                      "state" : "NY"
                 }
        }
 

       我们可以这样查询嵌套在里层的域

db.users.find({“address.state”:"NY”})
 

       数组元素则可以被这样查询:

       > db.food.insert({"fruit" : ["peach", "plum", "pear"]})
       > db.food.find({"fruit" : "pear"})
 

   7 支持索引

       支持二级索引包括 single-key, compound, unique, non-unique, geospatial indexes.嵌套的域同样也可以被索引。如果我们对一个数组类型进行索引,那么数组中所有元素也会自动被索引。

       当一个查询执行时,MongoDB的查询优化器会尝试多个不同的query plan,并选择执行速度最快的。开发者可以通过explain功能看到索引被使用的过程,然后可以通过hint功能来选择另一个不同的索引。

       可以在任何时候创建和删除索引

   8 aggregation

       除了ad hoc queries外,MongoDB还支持一系列工具来支持聚合,例如MapReduce和其他类似于SQL的GROUP BY的函数集合。

   9 文件存储

       该软件实现了一个称为GridFS的协议,这个协议是用来帮助从数据库中存储和获取文件的。

   10 支持服务器端javascript执行

        javaScript是MongoDB的一种通用语言,它可以被用在查询,聚集函数,直接由数据库执行。

        下面是一个使用javascript的查询例子:

 db.foo.find({$where:function(){return this.x==this.y;}})
 

        发送代码到数据库执行:

db.eval(function(name){return “Hello, ”+name;},[“Joe”])
 

        javaScript的变量可以被存储在数据库中并被其他javas作为全局变量使用。任何合法的javascript类型包括函数和对象,都可以被存储在MongoDB中,所以javascript可以被用来写<存储过程>

    11 capped collection

         MongoDB支持被称为capped collections(定量集合)的定长集合。Capped collections是唯一一种维持插入顺序的集合,其中,如果达到容量最大值,那么就会覆盖第一个元素,也就是说capped collection的行为类似于一个环形队列。

         一种特殊的cursor类型,称为tailable cursor,可以被用在capped collection上,当完成结果返回时,这种cursor不会关闭,而是会继续等待更多的结果来返回。也就是说如果有新的记录插入到capped collection的话,cursor会自动返回。

    12 目前提供多种语言的driver

    13 部署

        MongoDB使用的是memory-mapped files(内存映射文件:A memory-mapped file is a segment of virtual memory which has been assigned a direct byte-for-byte correlation with some portion of a file or file-like resource. This resource is typically a file that is physically present on-disk, but can also be a device, shared memory object, or other resource that the operating system can reference through a file descriptor. Once present, this correlation between the file and the memory space permits applications to treat the mapped portion as if it were primary memory. 详细见http://en.wikipedia.org/wiki/Memory-mapped_file),所以在32位机器上限制数据的最大大小为2GB,同时MongoDB服务器只能在小端系统上运行。

    14 Replication

       MongoDB不应被部署到少于两台的服务器上,也就是说至少有一台作为master,另一台作为slave。Master可以用来执行读写,而 slave可以从master上复制数据,但是它只能执行读操作或备份操作。开发者可以根据情况让一个operation可以被replicate到多个 servers上。

       下面的代码是启动一个master服务器和对应的slave服务器

       $ mkdir –p ~/dbs/master ~/dbs/slave
       $ ./mongod –master –port 10000 –dbpath ~/dbs/master
       $ ./mongod –slave  --port10001 –dbpath ~/dbs/slave  -- source localhost:10000
 

   好了,维基百科就翻译到这里,这样我们对MongoDB就有个大概的印象了,具体和普通的关系型数据库对比有什么优势呢,还要我们深入了解,这些后面找时间了解了解。

   然后我们就在网上再找找其他人的总结,看看有木有延伸的知识:

+1)   

所谓“面向集合”(Collenction-Orented),意思是数据被分组存储在数据集中,被称为一个集合(Collenction)。每个 集合在数据库中都有一个唯一的标识名,并且可以包含无限数目的文档。集合的概念类似关系型数据库(RDBMS)里的表(table),不同的是它不需要定 义任何模式(schema)。
模式自由(schema-free),意味着对于存储在mongodb数据库中的文件,我们不需要知道它的任何结构定义。如果需要的话,你完全可以把不同结构的文件存储在同一个数据库里。
存储在集合中的文档,被存储为键-值对的形式。键用于唯一标识一个文档,为字符串类型,而值则可以是各中复杂的文件类型。我们称这种存储形式为BSON(Binary Serialized dOcument Format)。

原文连接:http://blog.csdn.net/lolinzhang/article/details/4353699

+2)

MongoDB把数据存储在文件中(默认路径为:/data/db),为提高效率使用内存映射文件进行管理。

MongoDB的主要目标是在键/值存储方式(提供了高性能和高度伸缩性)以及传统的RDBMS系统(丰富的功能)架起一座桥梁,集两者的优势于一身。

根据官方网站的描述,Mongo适合用于以下场景:

l 网站数据:Mongo非常适合实时的插入,更新与查询,并具备网站实时数据存储所需的复制及高度伸缩性。

l 缓存:由于性能很高,Mongo也适合作为信息基础设施的缓存层。在系统重启之后,由Mongo搭建的持久化缓存层可以避免下层的数据源过载。

l 大尺寸,低价值的数据:使用传统的关系型数据库存储一些数据时可能会比较昂贵,在此之前,很多时候程序员往往会选择传统的文件进行存储。

l 高伸缩性的场景:Mongo非常适合由数十或数百台服务器组成的数据库。Mongo的路线图中已经包含对MapReduce引擎的内置支持。

l 用于对象及JSON数据的存储:Mongo的BSON数据格式非常适合文档化格式的存储及查询。

自然,MongoDB的使用也会有一些限制,例如它不适合:

l 高度事务性的系统:例如银行或会计系统。传统的关系型数据库目前还是更适用于需要大量原子性复杂事务的应用程序。

l 传统的商业智能应用:针对特定问题的BI数据库会对产生高度优化的查询方式。对于此类应用,数据仓库可能是更合适的选择。

l 需要SQL的问题

原文连接:http://lony1107.blog.163.com/blog/static/1212687032009916104328418/

   更多的介绍大家可以直接上MongoDB去仔细研读http://www.mongodb.org/display/DOCS/Introduction 原文介绍得非常详细!

   明白基本概念和特征后,我们就来尝试用用MongoDB,找篇入门教程对着做一次是最快的。。。。。。

    http://www.cnblogs.com/bestfc/archive/2011/06/28/MongoDB.html

   在上面这篇文章中我们发现好几个问题啊,为什么这样呢?别急,直接看人家mongoDB上的文档就知道咯,很小的问题嘛,不必每次都问google(官方 网站上一定会贴一大堆文档带大家入门的啊,不然怎么吸引我们去试用啊)例如上面的教程文章中说到的 安装过程,运行过程出现的错误都可以在MongoDB的Quickstart文档找到正确的解决方法 。(http://www.mongodb.org/display/DOCS/Quickstart+Windows)中找到(ps:有个问题不懂,为什么默认的数据文件要放在C盘的data/db下呢?可能跟内存映射文件的策略有关吧)(感谢 [↑起↑]阿非 的提醒,这个路径是可以自定义的,MongoDB的官方文档上是这样写的If you prefer to place datafiles elsewhere, use the --dbpath command line parameter when starting mongod.exe.下次我得先读完官方文档....)

   按照文件的介绍,我们很顺利的搭好服务器并在客户端上进行一些基本的操作,ok,我们体验完了,接下来干什么? 当然是继续体验啊,最好能用到项目中,官网上还有好多东西没挖出来了,大家一起去挖吧http://www.mongodb.org/,挖完后再总结一篇吧。


作者:Aga.J
出处:http://www.cnblogs.com/aga-j

Install MongoDB on Ubuntu

Package Options

The 10gen repository provides the mongodb-10gen package, which contains the latest stable release. Additionally you can install previous releases of MongoDB.

You cannot install this package concurrently with the mongodb, mongodb-server, or mongodb-clients packages provided by Ubuntu.

Install MongoDB

Configure Package Management System (APT)

The Ubuntu package management tool (i.e. dpkg and apt) ensure package consistency and authenticity by requiring that distributors sign packages with GPG keys. Issue the following command to import the 10gen public GPG Key:

sudo apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv 7F0CEB10

Create a /etc/apt/sources.list.d/10gen.list file using the following command.

echo 'deb http://downloads-distro.mongodb.org/repo/ubuntu-upstart dist 10gen' | sudo tee /etc/apt/sources.list.d/10gen.list

Now issue the following command to reload your repository:

sudo apt-get update

Install Packages

Issue the following command to install the latest stable version of MongoDB:

sudo apt-get install mongodb-10gen

When this command completes, you have successfully installed MongoDB! Continue for configuration and start-up suggestions.

Manage Installed Versions

You can use the mongodb-10gen package to install previous versions of MongoDB. To install a specific release, append the version number to the package name, as in the following example:

apt-get install mongodb-10gen=2.2.3

This will install the 2.2.3 release of MongoDB. You can specify any available version of MongoDB; however apt-get will upgrade the mongodb-10gen package when a newer version becomes available. Use the following pinning procedure to prevent unintended upgrades.

To pin a package, issue the following command at the system prompt to pin the version of MongoDB at the currently installed version:

echo "mongodb-10gen hold" | sudo dpkg --set-selections

Configure MongoDB

These packages configure MongoDB using the /etc/mongodb.conf file in conjunction with the control script. You will find the control script is at /etc/init.d/mongodb.

This MongoDB instance will store its data files in the /var/lib/mongodb and its log files in /var/log/mongodb, and run using the mongodb user account.

Note

 

If you change the user that runs the MongoDB process, you will need to modify the access control rights to the /var/lib/mongodb and /var/log/mongodb directories.

 

Controlling MongoDB

Starting MongoDB

You can start the mongod process by issuing the following command:

sudo service mongodb start

You can verify that mongod has started successfully by checking the contents of the log file at /var/log/mongodb/mongodb.log.

Stopping MongoDB

As needed, you may stop the mongod process by issuing the following command:

sudo service mongodb stop

Restarting MongoDB

You may restart the mongod process by issuing the following command:

sudo service mongodb restart

 

Controlling mongos

As of the current release, there are no control scripts for mongos. mongos is only used in sharding deployments and typically do not run on the same systems where mongod runs. You can use the mongodb script referenced above to derive your own mongos control script.

Using MongoDB

Among the tools included with the MongoDB package, is the mongo shell. You can connect to your MongoDB instance by issuing the following command at the system prompt:

mongo

This will connect to the database running on the localhost interface by default. At the mongo prompt, issue the following two commands to insert a record in the “test” collection of the (default) “test” database.

db.test.save( { a: 1 } )
db.test.find()

来源: http://docs.mongodb.org/manual/tutorial/install-mongodb-on-ubuntu/

猜你喜欢

转载自justcoding.iteye.com/blog/1929540