Mongodb安装

本文涉及的产品
云数据库 MongoDB,通用型 2核4GB
简介:

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位系统为例
 
  1. [root@server11 ~]# wget http://downloads.mongodb.org/linux/mongodb-linux-x86_64-2.2.2.tgz  --64位系统 
  2. [root@server11 ~]# wget http://downloads.mongodb.org/linux/mongodb-linux-i686-2.2.2.tgz    --32位系统 
  3. [root@server11 ~]# tar -zxvpf mongodb-linux-i686-2.2.2.tgz  
  4. [root@server11 ~]# mv mongodb-linux-i686-2.2.2 /usr/local/mongodb 

二:查看帮助信息并启动和关闭mongodb

 
  1. [root@server11 ~]# /usr/local/mongodb/bin/mongod --help 
  2. Mon Jan 21 11:25:05  
  3. Mon Jan 21 11:25:05 ** NOTE: when using MongoDB 32 bit, you are limited to about 2 gigabytes of data 
  4. Mon Jan 21 11:25:05 **       see http://blog.mongodb.org/post/137788967/32-bit-limitations 
  5. Mon Jan 21 11:25:05 **       with --journal, the limit is lower 
  6. Mon Jan 21 11:25:05  
  7. Allowed options: 
  8.  
  9. General options: 
  10.   -h [ --help ]               show this usage information 
  11.   --version                   show version information 
  12.   -f [ --config ] arg         configuration file specifying additional options 
  13.   -v [ --verbose ]            be more verbose (include multiple times for more  
  14.                               verbosity e.g. -vvvvv) 
  15.   --quiet                     quieter output 
  16.   --port arg                  specify port number - 27017 by default 
  17.   --bind_ip arg               comma separated list of ip addresses to listen on 
  18.                               - all local ips by default 
  19.   --maxConns arg              max number of simultaneous connections - 20000 by 
  20.                               default 
  21.   --objcheck                  inspect client data for validity on receipt 
  22.   --logpath arg               log file to send write to instead of stdout - has 
  23.                               to be a file, not directory 
  24.   --logappend                 append to logpath instead of over-writing 
  25.   --pidfilepath arg           full path to pidfile (if not set, no pidfile is  
  26.                               created) 
  27.   --keyFile arg               private key for cluster authentication 
  28.   --nounixsocket              disable listening on unix sockets 
  29.   --unixSocketPrefix arg      alternative directory for UNIX domain sockets  
  30.                               (defaults to /tmp) 
  31.   --fork                      fork server process 
  32.   --syslog                    log to system's syslog facility instead of file  
  33.                               or stdout 
  34.   --auth                      run with security 
  35.   --cpu                       periodically show cpu and iowait utilization 
  36.   --dbpath arg                directory for datafiles - defaults to /data/db/ 
  37.   --diaglog arg               0=off 1=W 2=R 3=both 7=W+some reads 
  38.   --directoryperdb            each database will be stored in a separate  
  39.                               directory 
  40.   --ipv6                      enable IPv6 support (disabled by default) 
  41.   --journal                   enable journaling 
  42.   --journalCommitInterval arg how often to group/batch commit (ms) 
  43.   --journalOptions arg        journal diagnostic options 
  44.   --jsonp                     allow JSONP access via http (has security  
  45.                               implications) 
  46.   --noauth                    run without security 
  47.   --nohttpinterface           disable http interface 
  48.   --nojournal                 disable journaling (journaling is on by default  
  49.                               for 64 bit) 
  50.   --noprealloc                disable data file preallocation - will often hurt 
  51.                               performance 
  52.   --noscripting               disable scripting engine 
  53.   --notablescan               do not allow table scans 
  54.   --nssize arg (=16)          .ns file size (in MB) for new databases 
  55.   --profile arg               0=off 1=slow2=all 
  56.   --quota                     limits each database to a certain number of files 
  57.                               (8 default) 
  58.   --quotaFiles arg            number of files allowed per db, requires --quota 
  59.   --repair                    run repair on all dbs 
  60.   --repairpath arg            root directory for repair files - defaults to  
  61.                               dbpath 
  62.   --rest                      turn on simple rest api 
  63.   --shutdown                  kill a running server (for init scripts) 
  64.   --slowms arg (=100)         value of slow for profile and console log 
  65.   --smallfiles                use a smaller default file size 
  66.   --syncdelay arg (=60)       seconds between disk syncs (0=never, but not  
  67.                               recommended) 
  68.   --sysinfo                   print some diagnostic system information 
  69.   --upgrade                   upgrade db if needed 
  70.  
  71. Replication options: 
  72.   --oplogSize arg       size to use (in MB) for replication op log. default is  
  73.                         5% of disk space (i.e. large is good) 
  74.  
  75. Master/slave options: 
  76.   --master              master mode 
  77.   --slave               slave mode 
  78.   --source arg          when slave: specify master as <server:port> 
  79.   --only arg            when slave: specify a single database to replicate 
  80.   --slavedelay arg      specify delay (in seconds) to be used when applying  
  81.                         master ops to slave 
  82.   --autoresync          automatically resync if slave data is stale 
  83.  
  84. Replica set options: 
  85.   --replSet arg           arg is <setname>[/<optionalseedhostlist>
  86.   --replIndexPrefetch arg specify index prefetching behavior (if secondary)  
  87.                           [none|_id_only|all] 
  88.  
  89. Sharding options: 
  90.   --configsvr           declare this is a config db of a cluster; default port  
  91.                         27019; default dir /data/configdb 
  92.   --shardsvr            declare this is a shard db of a cluster; default port  
  93.                         27018 
  94.   --noMoveParanoia      turn off paranoid saving of data for moveChunk.  this  
  95.                         is on by default for now, but default will switch 
  96.  
  97. [root@server11 ~]# mkdir -p /data/mongodb/db1  
  98. [root@server11 ~]# mkdir -p /usr/local/mongodb/logs  
  99.  
  100. [root@server11 ~]# /usr/local/mongodb/bin/mongod --port 3306 --dbpath /data/mongodb/db1/ --logpath /usr/local/mongodb/logs/m1.log & 
  101. all output going to: /usr/local/mongodb/logs/m1.log 
  102.  
  103. [root@server11 ~]# netstat -ntpl |grep :3306 
  104. tcp        0      0 0.0.0.0:3306                0.0.0.0:*                   LISTEN      13973/mongod       
  105.  
  106. [root@server11 ~]# cat /data/mongodb/db1/mongod.lock  
  107. 13973 
  108.  
  109. [root@server11 ~]# /usr/local/mongodb/bin/mongod --port 3306 --dbpath /data/mongodb/db1/ --logpath /usr/local/mongodb/logs/m1.log --shutdown 
  110. killing process with pid: 13973 

三:客户端连接mongodb测试

 
  1. [root@server11 ~]# /usr/local/mongodb/bin/mongo 127.0.0.1:3306 
  2. MongoDB shell version: 2.2.2 
  3. connecting to: 127.0.0.1:3306/test 
  4. > help 
  5.         db.help()                    help on db methods 
  6.         db.mycoll.help()             help on collection methods 
  7.         sh.help()                    sharding helpers 
  8.         rs.help()                    replica set helpers 
  9.         help admin                   administrative help 
  10.         help connect                 connecting to a db help 
  11.         help keys                    key shortcuts 
  12.         help misc                    misc things to know 
  13.         help mr                      mapreduce 
  14.  
  15.         show dbs                     show database names 
  16.         show collections             show collections in current database 
  17.         show users                   show users in current database 
  18.         show profile                 show most recent system.profile entries with time >= 1ms 
  19.         show logs                    show the accessible logger names 
  20.         show log [name]              prints out the last segment of log in memory, 'global' is default 
  21.         use <db_name>                set current database 
  22.         db.foo.find()                list objects in collection foo 
  23.         db.foo.find( { a : 1 } )     list objects in foo where a == 1 
  24.         it                           result of the last line evaluated; use to further iterate 
  25.         DBQuery.shellBatchSize = x   set default number of items to display on shell 
  26.         exit                         quit the mongo shell 
  27.  
  28. > show dbs
  29. local   (empty) 
  30.  
  31. > use test 
  32. switched to db test 
  33.  
  34. > db.test.save( { a: 1 } ) 
  35. >  db.test.find() 
  36. { "_id" : ObjectId("50fcb7f05712bfb21c866dc6"), "a" : 1 } 
  37.  
  38. > show dbs 
  39. local   (empty) 
  40. test    0.0625GB 
  41.  
  42. > show collections  
  43. system.indexes 
  44. test 
  45.  
  46. [root@server11 ~]# ll -h /data/mongodb/db1/ 
  47. total 65M 
  48. -rwxr-xr-x 1 root root   6 Jan 21 11:38 mongod.lock 
  49. -rw------- 1 root root 16M Jan 21 11:37 test.0 
  50. -rw------- 1 root root 32M Jan 21 11:37 test.1 
  51. -rw------- 1 root root 16M Jan 21 11:37 test.ns 

四:配置mongodb开机自动启动

 
  1. [root@server11 ~]# tail -1 /etc/rc.local  
  2. /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

相关实践学习
MongoDB数据库入门
MongoDB数据库入门实验。
快速掌握 MongoDB 数据库
本课程主要讲解MongoDB数据库的基本知识,包括MongoDB数据库的安装、配置、服务的启动、数据的CRUD操作函数使用、MongoDB索引的使用(唯一索引、地理索引、过期索引、全文索引等)、MapReduce操作实现、用户管理、Java对MongoDB的操作支持(基于2.x驱动与3.x驱动的完全讲解)。 通过学习此课程,读者将具备MongoDB数据库的开发能力,并且能够使用MongoDB进行项目开发。 &nbsp; 相关的阿里云产品:云数据库 MongoDB版 云数据库MongoDB版支持ReplicaSet和Sharding两种部署架构,具备安全审计,时间点备份等多项企业能力。在互联网、物联网、游戏、金融等领域被广泛采用。 云数据库MongoDB版(ApsaraDB for MongoDB)完全兼容MongoDB协议,基于飞天分布式系统和高可靠存储引擎,提供多节点高可用架构、弹性扩容、容灾、备份回滚、性能优化等解决方案。 产品详情: https://www.aliyun.com/product/mongodb
相关文章
|
5月前
|
开发框架 NoSQL JavaScript
mongoDB入门教程四:安装Node+express环境支撑
mongoDB入门教程四:安装Node+express环境支撑
49 0
|
5月前
|
NoSQL 数据可视化 MongoDB
Windows MongoDB的安装及配置图文说明(非常详细)
Windows MongoDB的安装及配置图文说明(非常详细)
314 0
|
5月前
|
NoSQL MongoDB 数据库
mongoDB入门教程一:下载安装和环境配置、连接运行
mongoDB入门教程一:下载安装和环境配置、连接运行
205 0
|
5月前
|
JSON NoSQL MongoDB
MongoDB Compass的安装及使用图文说明(非常详细)
MongoDB Compass的安装及使用图文说明(非常详细)
524 2
|
5月前
|
存储 NoSQL Linux
Linux下安装MongoDB
Linux下安装MongoDB
97 0
|
4月前
|
NoSQL MongoDB 数据安全/隐私保护
|
2天前
|
存储 分布式计算 NoSQL
MongoDB的简介和安装(在服务器上)
MongoDB的简介和安装(在服务器上)
11 0
|
1月前
|
NoSQL MongoDB Docker
docker安装MongoDB
docker安装MongoDB
21 0
|
2月前
|
NoSQL 关系型数据库 MySQL
Windows、Linux、Mac安装数据库(mysql、MongoDB、Redis)#0
不同系统下进行MySQL安装、MongoDB安装、Redis安装【2月更文挑战第5天】
444 5
Windows、Linux、Mac安装数据库(mysql、MongoDB、Redis)#0
|
3月前
|
存储 JSON NoSQL
【MongoDB】<文档型数据库>Windows&Liunx安装MongoDB(无错完整)
【1月更文挑战第26天】【MongoDB】<文档型数据库>Windows&Liunx安装MongoDB(无错完整)