MongoDB是一个介于关系数据库和非关系数据库之间的产品,是非关系数据库当中功能最丰富,最像关系数据库的。他支持的数据结构非常松散,是类似json的bjson格式,因此可以存储比较复杂的数据类型。Mongo最大的特点是他支持的查询语言非常强大,其语法有点类似于面向对象的查询语言,几乎可以实现类似关系数据库单表查询的绝大部分功能,而且还支持对数据建立索引。
它的特点是高性能、易部署、易使用,存储数据非常方便。主要功能特性有:
使用高效的二进制数据存储,包括大型对象(如视频等)。
支持RUBY,PYTHON,JAVA,C++,PHP等多种语言。
文件存储格式为BSON(一种JSON的扩展),可通过网络访问。
所谓“面向集合”(Collenction-Orented),意思是数据被分组存储在数据集中,被称为一个集合(Collenction)。每个集合在数据库中都有一个唯一的标识名,并且可以包含无限数目的文档。集合的概念类似关系型数据库(RDBMS)里的表(table),不同的是它不需要定义任何模式(schema)。
模式自由(schema-free),意味着对于存储在mongodb数据库中的文件,我们不需要知道它的任何结构定义。如果需要的话,你完全可以把不同结构的文件存储在同一个数据库里。
存储在集合中的文档,被存储为键-值对的形式。键用于唯一标识一个文档,为字符串类型,而值则可以是各中复杂的文件类型。我们称这种存储形式为BSON(Binary Serialized document Format)。
MongoDB服务端可运行在Linux、Windows或OS X平台,支持32位和64位应用,默认端口为27017。推荐运行在64位平台,因为MongoDB在32位模式运行时支持的最大文件尺寸为2GB。MongoDB把数据存储在文件中(默认路径为:/data/db),为提高效率使用内存映射文件进行管理。
-----------以上文字摘自http://www.oschina.net/p/mongodb------------
一:下载并解压mongodb源代码,本文中以32位系统为例
- [root@server11 ~]# wget http://downloads.mongodb.org/linux/mongodb-linux-x86_64-2.2.2.tgz --64位系统
- [root@server11 ~]# wget http://downloads.mongodb.org/linux/mongodb-linux-i686-2.2.2.tgz --32位系统
- [root@server11 ~]# tar -zxvpf mongodb-linux-i686-2.2.2.tgz
- [root@server11 ~]# mv mongodb-linux-i686-2.2.2 /usr/local/mongodb
二:查看帮助信息并启动和关闭mongodb
- [root@server11 ~]# /usr/local/mongodb/bin/mongod --help
- Mon Jan 21 11:25:05
- Mon Jan 21 11:25:05 ** NOTE: when using MongoDB 32 bit, you are limited to about 2 gigabytes of data
- Mon Jan 21 11:25:05 ** see http://blog.mongodb.org/post/137788967/32-bit-limitations
- Mon Jan 21 11:25:05 ** with --journal, the limit is lower
- Mon Jan 21 11:25:05
- Allowed options:
-
- General options:
- -h [ --help ] show this usage information
- --version show version information
- -f [ --config ] arg configuration file specifying additional options
- -v [ --verbose ] 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 - 20000 by
- default
- --objcheck inspect client data for validity on receipt
- --logpath arg log file to send write to instead of stdout - has
- to be a file, not directory
- --logappend append to logpath instead of over-writing
- --pidfilepath arg full path to pidfile (if not set, no pidfile is
- created)
- --keyFile arg private key for cluster authentication
- --nounixsocket disable listening on unix sockets
- --unixSocketPrefix arg alternative directory for UNIX domain sockets
- (defaults to /tmp)
- --fork fork server process
- --syslog log to system's syslog facility instead of file
- or stdout
- --auth run with security
- --cpu periodically show cpu and iowait utilization
- --dbpath arg directory for datafiles - defaults to /data/db/
- --diaglog arg 0=off 1=W 2=R 3=both 7=W+some reads
- --directoryperdb each database will be stored in a separate
- directory
- --ipv6 enable IPv6 support (disabled by default)
- --journal enable journaling
- --journalCommitInterval arg how often to group/batch commit (ms)
- --journalOptions arg journal diagnostic options
- --jsonp allow JSONP access via http (has security
- implications)
- --noauth run without security
- --nohttpinterface disable http interface
- --nojournal disable journaling (journaling is on by default
- for 64 bit)
- --noprealloc disable data file preallocation - will often hurt
- performance
- --noscripting disable scripting engine
- --notablescan do not allow table scans
- --nssize arg (=16) .ns file size (in MB) for new databases
- --profile arg 0=off 1=slow, 2=all
- --quota limits each database to a certain number of files
- (8 default)
- --quotaFiles arg number of files allowed per db, requires --quota
- --repair run repair on all dbs
- --repairpath arg root directory for repair files - defaults to
- dbpath
- --rest turn on simple rest api
- --shutdown kill a running server (for init scripts)
- --slowms arg (=100) value of slow for profile and console log
- --smallfiles use a smaller default file size
- --syncdelay arg (=60) seconds between disk syncs (0=never, but not
- recommended)
- --sysinfo print some diagnostic system information
- --upgrade upgrade db if needed
-
- 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:
- --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
- --noMoveParanoia turn off paranoid saving of data for moveChunk. this
- is on by default for now, but default will switch
-
- [root@server11 ~]# mkdir -p /data/mongodb/db1
- [root@server11 ~]# mkdir -p /usr/local/mongodb/logs
-
- [root@server11 ~]# /usr/local/mongodb/bin/mongod --port 3306 --dbpath /data/mongodb/db1/ --logpath /usr/local/mongodb/logs/m1.log &
- all output going to: /usr/local/mongodb/logs/m1.log
-
- [root@server11 ~]# netstat -ntpl |grep :3306
- tcp 0 0 0.0.0.0:3306 0.0.0.0:* LISTEN 13973/mongod
-
- [root@server11 ~]# cat /data/mongodb/db1/mongod.lock
- 13973
-
- [root@server11 ~]# /usr/local/mongodb/bin/mongod --port 3306 --dbpath /data/mongodb/db1/ --logpath /usr/local/mongodb/logs/m1.log --shutdown
- killing process with pid: 13973
三:客户端连接mongodb测试
- [root@server11 ~]# /usr/local/mongodb/bin/mongo 127.0.0.1:3306
- MongoDB shell version: 2.2.2
- connecting to: 127.0.0.1:3306/test
- > help
- db.help() help on db methods
- db.mycoll.help() help on collection methods
- sh.help() sharding helpers
- rs.help() replica set helpers
- help admin administrative help
- help connect connecting to a db help
- help keys key shortcuts
- help misc misc things to know
- help mr mapreduce
-
- show dbs show database names
- show collections show collections in current database
- show users show users in current database
- show profile show most recent system.profile entries with time >= 1ms
- show logs show the accessible logger names
- show log [name] prints out the last segment of log in memory, 'global' is default
- use <db_name> set current database
- db.foo.find() list objects in collection foo
- db.foo.find( { a : 1 } ) list objects in foo where a == 1
- it result of the last line evaluated; use to further iterate
- DBQuery.shellBatchSize = x set default number of items to display on shell
- exit quit the mongo shell
-
- > show dbs
- local (empty)
-
- > use test
- switched to db test
-
- > db.test.save( { a: 1 } )
- > db.test.find()
- { "_id" : ObjectId("50fcb7f05712bfb21c866dc6"), "a" : 1 }
-
- > show dbs
- local (empty)
- test 0.0625GB
-
- > show collections
- system.indexes
- test
-
- [root@server11 ~]# ll -h /data/mongodb/db1/
- total 65M
- -rwxr-xr-x 1 root root 6 Jan 21 11:38 mongod.lock
- -rw------- 1 root root 16M Jan 21 11:37 test.0
- -rw------- 1 root root 32M Jan 21 11:37 test.1
- -rw------- 1 root root 16M Jan 21 11:37 test.ns
四:配置mongodb开机自动启动
- [root@server11 ~]# tail -1 /etc/rc.local
- /usr/local/mongodb/bin/mongod --port 3306 --dbpath /data/mongodb/db1/ --logpath /usr/local/mongodb/logs/m1.log &
参考:http://docs.mongodb.org/manual/tutorial/install-mongodb-on-linux/
本文转自斩月博客51CTO博客,原文链接http://blog.51cto.com/ylw6006/1123302如需转载请自行联系原作者
ylw6006