MongoDB的简介与安装

时间:2021-10-05 19:39:38

MongoDB是一个可扩展、高性能的下一代数据库,它的特点是高性能、易部署、易使用、存储数据非常方便,主要特性有:

  1、面向文档存储,json格式的文档易读,高效。

  2、模式*,支持动态查询、完全索引,无模式。

  3、高效的数据存储,效率提高。

  4、支持复制和故障恢复。

  5、以支持云级别的伸缩性,支持水平的数据库集群,可动态添加额外的服务器

 

MongoDB的局限与不足

在32位系统上,不支持大于2.5G的数据。
单个文档大小限制为 16 M
锁粒度太粗,MongoDB使用的是一把全局的读写锁
不支持join操作和事务机制,这个确实是非MongoDB要做的领域
对内存要求比较大,至少要保证热数据(索引,数据及系统其它开销)都能装进内存
用户权限方面比较弱,将机器部署在安全的内网环境中,尽量不要用权限
MapReduce在单个实例上无法并行,可用Auto-Sharding实现。是由JS引擎的限制造成的。
MapReduce的结果无法写入到一个被Sharding的Collection中,2.0版本对这个问题的解决好像也不彻底
对于数组型的数据操作不够丰富

 

MongoDB的简介与安装

 

MongoDB的安装


[root@mongodb ~]# vi /etc/yum.repos.d/mongodb-org-3.0.repo

[mongodb-org-3.0]
name = mongodb
baseurl=http://repo.mongodb.org/yum/redhat/$releasever/mongodb-org/3.0/x86_64/
gpgcheck=0
enabled=1

[root@mongodb ~]# ping mongodb.org
PING mongodb.org (54.173.82.137) 56(84) bytes of data.
64 bytes from ec2-54-173-82-137.compute-1.amazonaws.com (54.173.82.137): icmp_seq=1 ttl=46 time=499 ms
64 bytes from ec2-54-173-82-137.compute-1.amazonaws.com (54.173.82.137): icmp_seq=2 ttl=46 time=500 ms
64 bytes from ec2-54-173-82-137.compute-1.amazonaws.com (54.173.82.137): icmp_seq=3 ttl=46 time=502 ms
64 bytes from ec2-54-173-82-137.compute-1.amazonaws.com (54.173.82.137): icmp_seq=4 ttl=46 time=453 ms
64 bytes from ec2-54-173-82-137.compute-1.amazonaws.com (54.173.82.137): icmp_seq=5 ttl=46 time=497 ms

[root@mongodb ~]# yum install -y mongodb-org
Loaded plugins: rhnplugin, security
mongodb-org-3.0                                                                                               |  951 B     00:00    
mongodb-org-3.0/primary                                                                                       | 7.4 kB     00:00    
mongodb-org-3.0                                                                                                                35/35
Setting up Install Process
Resolving Dependencies
--> Running transaction check
---> Package mongodb-org.x86_64 0:3.0.4-1.el5 set to be updated
--> Processing Dependency: mongodb-org-mongos = 3.0.4 for package: mongodb-org
--> Processing Dependency: mongodb-org-server = 3.0.4 for package: mongodb-org
--> Processing Dependency: mongodb-org-shell = 3.0.4 for package: mongodb-org
--> Processing Dependency: mongodb-org-tools = 3.0.4 for package: mongodb-org
--> Running transaction check
---> Package mongodb-org-mongos.x86_64 0:3.0.4-1.el5 set to be updated
---> Package mongodb-org-server.x86_64 0:3.0.4-1.el5 set to be updated
---> Package mongodb-org-shell.x86_64 0:3.0.4-1.el5 set to be updated
---> Package mongodb-org-tools.x86_64 0:3.0.4-1.el5 set to be updated
--> Finished Dependency Resolution

Dependencies Resolved

=====================================================================================================================================
 Package                              Arch                     Version                       Repository                         Size
=====================================================================================================================================
Installing:
 mongodb-org                          x86_64                   3.0.4-1.el5                   mongodb-org-3.0                   4.6 k
Installing for dependencies:
 mongodb-org-mongos                   x86_64                   3.0.4-1.el5                   mongodb-org-3.0                   3.9 M
 mongodb-org-server                   x86_64                   3.0.4-1.el5                   mongodb-org-3.0                   8.3 M
 mongodb-org-shell                    x86_64                   3.0.4-1.el5                   mongodb-org-3.0                   4.1 M
 mongodb-org-tools                    x86_64                   3.0.4-1.el5                   mongodb-org-3.0                    32 M

Transaction Summary
=====================================================================================================================================
Install       5 Package(s)
Upgrade       0 Package(s)

Total download size: 49 M
Downloading Packages:
(1/5): mongodb-org-3.0.4-1.el5.x86_64.rpm                                                                     | 4.6 kB     00:00    
(2/5): mongodb-org-mongos-3.0.4-1.el5.x86_64.rpm                                                              | 3.9 MB     00:09    
(3/5): mongodb-org-shell-3.0.4-1.el5.x86_64.rpm                                                               | 4.1 MB     00:09    
(4/5): mongodb-org-server-3.0.4-1.el5.x86_64.rpm                                                              | 8.3 MB     00:32    
(5/5): mongodb-org-tools-3.0.4-1.el5.x86_64.rpm                                                               |  32 MB     03:24    
-------------------------------------------------------------------------------------------------------------------------------------
Total                                                                                                194 kB/s |  49 MB     04:16    
Running rpm_check_debug
Running Transaction Test
Finished Transaction Test
Transaction Test Succeeded
Running Transaction
  Installing     : mongodb-org-server                                                                                            1/5
  Installing     : mongodb-org-shell                                                                                             2/5
  Installing     : mongodb-org-tools                                                                                             3/5
  Installing     : mongodb-org-mongos                                                                                            4/5
  Installing     : mongodb-org                                                                                                   5/5

Installed:
  mongodb-org.x86_64 0:3.0.4-1.el5                                                                                                  

Dependency Installed:
  mongodb-org-mongos.x86_64 0:3.0.4-1.el5     mongodb-org-server.x86_64 0:3.0.4-1.el5     mongodb-org-shell.x86_64 0:3.0.4-1.el5   
  mongodb-org-tools.x86_64 0:3.0.4-1.el5    

Complete!

禁用selinux
[root@mongodb ~]# getenforce
Disabled

参数文件
[root@mongodb ~]# ll /etc/mongod.conf  
-rw-r--r-- 1 root root 1533 Jun 16 06:58 /etc/mongod.conf

启动mongodb后台进程
[root@mongodb ~]# service mongod status
mongod is stopped
[root@mongodb ~]# service mongod start
Starting mongod:                                           [  OK  ]
停止mongodb后台进程
[root@mongodb ~]# service mongod stop
Stopping mongod:                                           [  OK  ]
重启mongodb后台进程
[root@mongodb ~]# service mongod restart
Stopping mongod:                                           [  OK  ]
Starting mongod:                                           [  OK  ]
[root@mongodb ~]# ps -ef|grep mongod|grep -v grep
mongod    3626     1  9 19:47 ?        00:00:13 /usr/bin/mongod -f /etc/mongod.conf
[root@mongodb ~]# netstat -anp|grep mongod|grep tcp
tcp        0      0 127.0.0.1:27017             0.0.0.0:*                   LISTEN      4096/mongod        

日志文件
[root@mongodb mongodb]# pwd
/var/log/mongodb
[root@mongodb mongodb]# more mongod.log
2015-07-13T19:38:29.845+0800 I CONTROL  ***** SERVER RESTARTED *****
2015-07-13T19:38:29.897+0800 I JOURNAL  [initandlisten] journal dir=/var/lib/mongo/journal
2015-07-13T19:38:29.897+0800 I JOURNAL  [initandlisten] recover : no journal files present, no recovery needed
2015-07-13T19:38:33.200+0800 I JOURNAL  [initandlisten] preallocateIsFaster=true 21.28
2015-07-13T19:38:37.826+0800 I JOURNAL  [initandlisten] preallocateIsFaster check took 7.928 secs
2015-07-13T19:38:37.831+0800 I JOURNAL  [durability] Durability thread started
2015-07-13T19:38:37.831+0800 I JOURNAL  [journal writer] Journal writer thread started
2015-07-13T19:38:37.833+0800 I CONTROL  [initandlisten] MongoDB starting : pid=3517 port=27017 dbpath=/var/lib/mongo 64-bit host=mongodb
2015-07-13T19:38:37.834+0800 I CONTROL  [initandlisten] db version v3.0.4
2015-07-13T19:38:37.834+0800 I CONTROL  [initandlisten] git version: 0481c958daeb2969800511e7475dc66986fa9ed5
2015-07-13T19:38:37.834+0800 I CONTROL  [initandlisten] OpenSSL version: OpenSSL 0.9.8e-fips-rhel5 01 Jul 2008
2015-07-13T19:38:37.834+0800 I CONTROL  [initandlisten] build info: Linux build14.nj1.10gen.cc 2.6.32-431.3.1.el6.x86_64 #1 SMP Fri Jan 3 21:39:27 UTC
2014 x86_64 BOOST_LIB_VERSION=1_49
2015-07-13T19:38:37.834+0800 I CONTROL  [initandlisten] allocator: tcmalloc
2015-07-13T19:38:37.834+0800 I CONTROL  [initandlisten] options: { config: "/etc/mongod.conf", net: { bindIp: "127.0.0.1" }, processManagement: { fork:
 true, pidFilePath: "/var/run/mongodb/mongod.pid" }, storage: { dbPath: "/var/lib/mongo" }, systemLog: { destination: "file", logAppend: true, path: "/
var/log/mongodb/mongod.log" } }
2015-07-13T19:38:37.834+0800 I INDEX    [initandlisten] allocating new ns file /var/lib/mongo/local.ns, filling with zeroes...
2015-07-13T19:38:37.892+0800 I STORAGE  [FileAllocator] allocating new datafile /var/lib/mongo/local.0, filling with zeroes...
2015-07-13T19:38:37.892+0800 I STORAGE  [FileAllocator] creating directory /var/lib/mongo/_tmp
2015-07-13T19:38:38.140+0800 I STORAGE  [FileAllocator] done allocating datafile /var/lib/mongo/local.0, size: 64MB,  took 0.245 secs
2015-07-13T19:38:38.254+0800 I NETWORK  [initandlisten] waiting for connections on port 27017
2015-07-13T19:40:30.695+0800 I CONTROL  [signalProcessingThread] got signal 15 (Terminated), will terminate after current cmd ends
2015-07-13T19:40:30.695+0800 I CONTROL  [signalProcessingThread] now exiting
2015-07-13T19:40:30.695+0800 I NETWORK  [signalProcessingThread] shutdown: going to close listening sockets...
2015-07-13T19:40:30.695+0800 I NETWORK  [signalProcessingThread] closing listening socket: 6
2015-07-13T19:40:30.695+0800 I NETWORK  [signalProcessingThread] closing listening socket: 7
2015-07-13T19:40:30.695+0800 I NETWORK  [signalProcessingThread] removing socket file: /tmp/mongodb-27017.sock
2015-07-13T19:40:30.695+0800 I NETWORK  [signalProcessingThread] shutdown: going to flush diaglog...
2015-07-13T19:40:30.695+0800 I NETWORK  [signalProcessingThread] shutdown: going to close sockets...
2015-07-13T19:40:30.695+0800 I STORAGE  [signalProcessingThread] shutdown: waiting for fs preallocator...
2015-07-13T19:40:30.695+0800 I STORAGE  [signalProcessingThread] shutdown: final commit...
2015-07-13T19:40:30.695+0800 I JOURNAL  [signalProcessingThread] journalCleanup...
2015-07-13T19:40:30.695+0800 I JOURNAL  [signalProcessingThread] removeJournalFiles
2015-07-13T19:40:30.709+0800 I JOURNAL  [signalProcessingThread] Terminating durability thread ...
2015-07-13T19:40:30.802+0800 I JOURNAL  [journal writer] Journal writer thread stopped
2015-07-13T19:40:30.803+0800 I JOURNAL  [durability] Durability thread stopped
2015-07-13T19:40:30.803+0800 I STORAGE  [signalProcessingThread] shutdown: closing all files...
2015-07-13T19:40:30.804+0800 I STORAGE  [signalProcessingThread] closeAllFiles() finished
2015-07-13T19:40:30.804+0800 I STORAGE  [signalProcessingThread] shutdown: removing fs lock...
2015-07-13T19:40:30.804+0800 I CONTROL  [signalProcessingThread] dbexit:  rc: 0

设置开机启动mongodb
[root@mongodb ~]# chkconfig mongod on


启动命令mongod参数说明
[root@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
  --ipv6                      enable IPv6 support (disabled 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 mongodb syslog message
  --logappend                 append to logpath instead of over-writing
  --logRotate arg             set the log rotation behavior (rename|reopen)
  --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)
  --filePermissions arg       permissions to set on UNIX domain socket file -
                              0700 by default
  --fork                      fork server process
  --auth                      run with security
  --noauth                    run without security
  --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
  --noIndexBuildRetry         don't retry any index builds that were
                              interrupted by shutdown
  --noscripting               disable scripting engine
  --notablescan               do not allow table scans
  --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

SSL options:
  --sslOnNormalPorts                    use ssl on configured ports
  --sslMode arg                         set the SSL operation mode
                                        (disabled|allowSSL|preferSSL|requireSSL
                                        )
  --sslPEMKeyFile arg                   PEM file for ssl
  --sslPEMKeyPassword arg               PEM file password
  --sslClusterFile arg                  Key file for internal SSL
                                        authentication
  --sslClusterPassword arg              Internal authentication key file
                                        password
  --sslCAFile arg                       Certificate Authority file for SSL
  --sslCRLFile arg                      Certificate Revocation List file for
                                        SSL
  --sslWeakCertificateValidation        allow client to connect without
                                        presenting a certificate
  --sslAllowConnectionsWithoutCertificates
                                        allow client to connect without
                                        presenting a certificate
  --sslAllowInvalidHostnames            Allow server certificates to provide
                                        non-matching hostnames
  --sslAllowInvalidCertificates         allow connections to servers with
                                        invalid certificates
  --sslFIPSMode                         activate FIPS 140-2 mode at startup

Storage options:
  --storageEngine arg (=mmapv1) what storage engine to use
  --dbpath arg                  directory for datafiles - defaults to /data/db
  --directoryperdb              each database will be stored in a separate
                                directory
  --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
  --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)

WiredTiger options:
  --wiredTigerCacheSizeGB arg           maximum amount of memory to allocate
                                        for cache; defaults to 1/2 of physical
                                        RAM
  --wiredTigerStatisticsLogDelaySecs arg (=0)
                                        seconds to wait between each write to a
                                        statistics file in the dbpath; 0 means
                                        do not log statistics
  --wiredTigerJournalCompressor arg (=snappy)
                                        use a compressor for log records
                                        [none|snappy|zlib]
  --wiredTigerDirectoryForIndexes       Put indexes and data in different
                                        directories
  --wiredTigerCollectionBlockCompressor arg (=snappy)
                                        block compression algorithm for
                                        collection data [none|snappy|zlib]
  --wiredTigerIndexPrefixCompression arg (=1)
                                        use prefix compression on row-store
                                        leaf pages

--quiet # 安静输出
--port arg # 指定服务端口号,默认端口27017
--bind_ip arg # 绑定服务IP,若绑定127.0.0.1,则只能本机访问,不指定默认本地所有IP
--logpath arg # 指定MongoDB日志文件,注意是指定文件不是目录
--logappend # 使用追加的方式写日志
--pidfilepath arg # PID File 的完整路径,如果没有设置,则没有PID文件
--keyFile arg # 集群的私钥的完整路径,只对于Replica Set 架构有效
--unixSocketPrefix arg # UNIX域套接字替代目录,(默认为 /tmp)
--fork # 以守护进程的方式运行MongoDB,创建服务器进程
--auth # 启用验证
--cpu # 定期显示CPU的CPU利用率和iowait
--dbpath arg # 指定数据库路径
--diaglog arg # diaglog选项 0=off 1=W 2=R 3=both 7=W+some reads
--directoryperdb # 设置每个数据库将被保存在一个单独的目录
--journal # 启用日志选项,MongoDB的数据操作将会写入到journal文件夹的文件里
--journalOptions arg # 启用日志诊断选项
--ipv6 # 启用IPv6选项
--jsonp # 允许JSONP形式通过HTTP访问(有安全影响)
--maxConns arg # 最大同时连接数 默认2000
--noauth # 不启用验证
--nohttpinterface # 关闭http接口,默认关闭27018端口访问
--noprealloc # 禁用数据文件预分配(往往影响性能)
--noscripting # 禁用脚本引擎
--notablescan # 不允许表扫描
--nounixsocket # 禁用Unix套接字监听
--nssize arg (=16) # 设置信数据库.ns文件大小(MB)
--objcheck # 在收到客户数据,检查的有效性,
--profile arg # 档案参数 0=off 1=slow, 2=all
--quota # 限制每个数据库的文件数,设置默认为8
--quotaFiles arg # number of files allower per db, requires --quota
--rest # 开启简单的rest API
--repair # 修复所有数据库run repair on all dbs
--repairpath arg # 修复库生成的文件的目录,默认为目录名称dbpath
--slowms arg (=100) # value of slow for profile and console log
--smallfiles # 使用较小的默认文件
--syncdelay arg (=60) # 数据写入磁盘的时间秒数(0=never,不推荐)
--sysinfo # 打印一些诊断系统信息
--upgrade # 如果需要升级数据库

Replicaton 参数
--fastsync    # 从一个dbpath里启用从库复制服务,该dbpath的数据库是主库的快照,可用于快速启用同步
--autoresync  # 如果从库与主库同步数据差得多,自动重新同步
--oplogSize arg # 设置oplog的大小(MB)

主/从参数
--master      # 主库模式
--slave       # 从库模式
--source arg  # 从库端口号
--only arg    # 指定单一的数据库复制
--slavedelay arg  # 设置从库同步主库的延迟时间

Replica set(副本集)选项:
--replSet arg   # 设置副本集名称

Sharding(分片)选项
--configsvr   # 声明这是一个集群的config服务,默认端口27019,默认目录/data/configdb
--shardsvr    # 声明这是一个集群的分片,默认端口27018
--noMoveParanoia  # 关闭偏执为moveChunk数据保存

上述参数都可以写入 mongod.conf 配置文档里