MongoDB技术之旅一 MongoDB初探

《MongoDB权威指南》阅读笔记

初探MongoDB

伴随着NoSQL在国内的火热,MongoDB作为一种NoSQL数据库越来越受到大家的熟知。MongoDB是一种强大、灵活、可扩展的数据存储方式。它扩展了关系型数据库的众多有用的功能,辅助索引、范围查询和排序等操作。MongoDB功能非常丰富,比如其内置了对MapReduce和地理空间索引的支持。MongoDB的数据模型对开发者来说非常的友好,配置选项对于管理员来说也很轻松。

MongoDB是面向文档的数据库,不是关系型的数据库,所以其扩展性要好于关系型数据库,其将在关系数据中的行装换为文档的(document)概念。面向文档的方式可以将文档或者数组内嵌,一个文档可以表达非常复杂的层次关系。面向对象的思想也是这样看待数据,所以对开发者非常友好。MongoDB没有传统的模式的概念禁锢,文档结构也不会固定不变。

MongoDB在最初设计的时候就考虑到了一个问题–扩展在传统关系型数据库中,当数据库达到瓶颈的情况下,DBA一般有两种选择,一种就是垂直扩展,通过买更好更牛的硬件来满足对容量和性能的需求,还有一种是水平扩展,通过将数据切分至不同的机器,水平分布式存放,业务通过制定好的切分规则,自动路由到数据存放位置获取数据。垂直扩展由于金钱和硬件瓶颈的限制只能短暂的规避掉问题,不能从根本解决问题。而水平扩展一般是业内最好也是使用最多的选择。在MongoDB,提供了复制集、自动分片等已有的水平扩展方案来解决数据库瓶颈问题。

MongoDB提供了非常丰富的功能,索引、JavaScript、MapReduce、聚合操作、固定集合大小、大文件存储,但是有些关系型的数据库所有的特性(事务)MongoDB并不具备。选用时一定要提前清楚MongoDB的一下功能特性和优缺点。

MongoDB其极限的追求性能,甚至为了追求性能舍弃了一些功能,文档动态填充,预分配数据文件、用空间换区性能,使用内存映射文件,动态的查询优化其会记住执行查询的方式,将服务端处理逻辑交给客户端,总之MongoDB在各个方面都对性能做了充分的考虑。

MongoDB会尽量让服务器自己管理自己,更少的人工参与,比如复制集的自动切换等。MongoDB的管理理念就是让服务器自动配置,用户只在需要的时候设置。基于以上,MongoDB是一种灵活、高效、易于扩展、功能完备的数据库。

MongoDB的基本概念

在传统关系型数据库中基本的数据单元可以理解为一行(row),但是在MongoDB中的数据单元是一个文档(document),多个文档组成一个集合(collection),集合类似于关系型中的表。多个集合组成一个数据库,每个数据库有自己的权限和集合。每个文档除非显示定义都默认有一个_id作为这个文档的唯一标示。文档是有多个key:value键值对组成,多个键值对有序的放在一起就组成了一个文档,许多编程语言中都有类似的数据结构,映射、散列、字典等。文档中键值对是有序的,不同顺序的键值对组成不同的文档,如{1:a,2:b}和{2:b,1:a}是不同的文档。一般情况下键值对的顺序并不重要。文档中的value值可以是字符串,也可以是其他的数据类型,key的命名是区分大小写的。

一组文档组成一个集合,集合是无模式概念存在的,也就是集合对于其中存储的文档没有任何的格式限制,可以存储任意格式的文档在同一个集合中,但是出于管理文档和使用文档考虑,一般会对同一个集合中存储同一类文档,便于管理和使用。

多个集合组成一个数据库,一个MongoDB实例可以承载多个数据库,数据库之间是完全独立存在的,有各自的独立权限控制,在磁盘上也是防止在不同的数据文件中。有一些MongoDB自带的数据库。

admin:这是管理数据库,在这个数据库添加一个用户,那么这个用户继承所有数据库的权限,用于管理数据库,一些特殊的服务器端命令也只能在这个数据库内运行,比如关闭数据库实例。

local:这个数据库永远不会被复制,可以用来存储限于本地使用的一些集合数据。

config:当用到分片设置时,config数据库在内部使用,用于保存分片的相关信息。

MongoDB的安装

下载MongoDB:https://www.mongodb.org/downloads
tar -zxvf mongodb-linux-x86_64-2.6.9.tgz
解压后就可以通过mongod来启动MongoDB,MongoDB默认端口号为27017

MongoDB的配置文件解析
./mongod --help
Options:

General options:
  -h [ --help ]               show this usage information
  --version                   show version information
  -f [ --config ] arg         configuration file specifying additional options
  -v [ --verbose ] [=arg(=v)] be more verbose (include multiple times for more 
                              verbosity e.g. -vvvvv)
  --quiet                     quieter output
  --port arg                  specify port number - 27017 by default
  --bind_ip arg               comma separated list of ip addresses to listen     
                              on - all local ips by default
  --maxConns arg              max number of simultaneous connections - 1000000 by default
  --logpath arg               log file to send write to instead of stdout -                 
                              has to be a file, not directory
  --syslog                    log to system's syslog facility instead of file 
                              or stdout
  --syslogFacility arg        syslog facility used for monogdb syslog message
  --logappend                 append to logpath instead of over-writing
  --timeStampFormat arg       Desired format for timestamps in log messages. 
                              One of ctime, iso8601-utc or iso8601-local
  --pidfilepath arg           full path to pidfile (if not set, no pidfile is 
                              created)
  --keyFile arg               private key for cluster authentication
  --setParameter arg          Set a configurable parameter
  --httpinterface             enable http interface
  --clusterAuthMode arg       Authentication mode used for cluster 
                              authentication. Alternatives are 
                              (keyFile|sendKeyFile|sendX509|x509)
  --nounixsocket              disable listening on unix sockets
  --unixSocketPrefix arg      alternative directory for UNIX domain sockets 
                              (defaults to /tmp)
  --fork                      fork server process
  --auth                      run with security
  --noauth                    run without security
  --ipv6                      enable IPv6 support (disabled by default)
  --jsonp                     allow JSONP access via http (has security 
                              implications)
  --rest                      turn on simple rest api
  --slowms arg (=100)         value of slow for profile and console log
  --profile arg               0=off 1=slow, 2=all
  --cpu                       periodically show cpu and iowait utilization
  --sysinfo                   print some diagnostic system information
  --dbpath arg                directory for datafiles - defaults to /data/db
  --directoryperdb            each database will be stored in a separate 
                              directory
  --noIndexBuildRetry         don't retry any index builds that were 
                              interrupted by shutdown
  --noprealloc                disable data file preallocation - will often 
                              hurt
                              performance
  --nssize arg (=16)          .ns file size (in MB) for new databases
  --quota                     limits each database to a certain number of 
                              files
                              (8 default)
  --quotaFiles arg            number of files allowed per db, implies --quota
  --smallfiles                use a smaller default file size
  --syncdelay arg (=60)       seconds between disk syncs (0=never, but not 
                              recommended)
  --upgrade                   upgrade db if needed
  --repair                    run repair on all dbs
  --repairpath arg            root directory for repair files - defaults to 
                              dbpath
  --noscripting               disable scripting engine
  --notablescan               do not allow table scans
  --journal                   enable journaling
  --nojournal                 disable journaling (journaling is on by default 
                              for 64 bit)
  --journalOptions arg        journal diagnostic options
  --journalCommitInterval arg how often to group/batch commit (ms)
  --shutdown                  kill a running server (for init scripts)

Replication options:
  --oplogSize arg       size to use (in MB) for replication op log. default 
                        is 5% of disk space (i.e. large is good)

Master/slave options (old; use replica sets instead):
  --master              master mode
  --slave               slave mode
  --source arg          when slave: specify master as <server:port>
  --only arg            when slave: specify a single database to replicate
  --slavedelay arg      specify delay (in seconds) to be used when applying 
                        master ops to slave
  --autoresync          automatically resync if slave data is stale

Replica set options:
  --replSet arg           arg is <setname>[/<optionalseedhostlist>]
  --replIndexPrefetch arg specify index prefetching behavior (if secondary) 
                          [none|_id_only|all]

Sharding options:
  --configsvr           declare this is a config db of a cluster; default port 
                        27019; default dir /data/configdb
  --shardsvr            declare this is a shard db of a cluster; default port 
                        27018
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值