MongoDB 安装配置

来源:互联网 发布:linux的svn上传文件 编辑:程序博客网 时间:2024/05/17 06:16

MongoDB 是一个高性能,开源,无模式的文档型数据库,采用C++开发,是当前NoSQL数据库中比较热门的一种,在许多场景下可用于替代传统的关系型数据库或键/值存储方式

MongoDB使用BSON作为数据存储和传输的格式,BSON是一种类似JSON的二进制序列化文档,支持嵌套对象和数组。MongoDB很像MySQL,document对应MySQL的row,collection对应MySQL的table

MongoDB服务端可运行在Linux、Windows或OS X平台,支持32位和64位应用,默认端口为27017。推荐运行在64位平台,因为MongoDB在32位模式运行时支持的最大文件尺寸为2GB


1,特点和功能

1) 特点

  • 高性能、易部署、易使用,存储数据非常方便。
  • 面向集合存储,易存储对象类型的数据,模式自由。
  • 支持动态查询,支持完全索引,包含内部对象。
  • 支持复制和故障恢复。
  • 使用高效的二进制数据存储,包括大型对象(如视频等)。
  • 自动处理碎片,以支持云计算层次的扩展性
  • 支持Python,PHP,Ruby,Java,C,C#,Javascript,Perl及C++语言的驱动程序,社区中也提供了对Erlang及.NET等平台的驱动程序。
  • 文件存储格式为BSON(一种JSON的扩展),可通过网络访问。

2) 功能

  • 面向集合的存储:适合存储对象及JSON形式的数据。
  • 动态查询:Mongo支持丰富的查询表达式。查询指令使用JSON形式的标记,可轻易查询文档中内嵌的对象及数组。
  • 完整的索引支持:包括文档内嵌对象及数组。Mongo的查询优化器会分析查询表达式,并生成一个高效的查询计划。
  • 查询监视:Mongo包含一个监视工具用于分析数据库操作的性能。
  • 复制及自动故障转移:Mongo数据库支持服务器之间的数据复制,支持主-从模式及服务器之间的相互复制。复制的主要目标是提供冗余及自动故障转移。
  • 高效的传统存储方式:支持二进制数据及大型对象(如照片或图片)
  • 自动分片以支持云级别的伸缩性:自动分片功能支持水平的数据库集群,可动态添加额外的机器。


2,  适用场合

  1. 网站数据:Mongo非常适合实时的插入,更新与查询,并具备网站实时数据存储所需的复制及高度伸缩性。
  2. 缓存:由于性能很高,Mongo也适合作为信息基础设施的缓存层。在系统重启之后,由Mongo搭建的持久化缓存层可以避免下层的数据源 过载。
  3. 大尺寸,低价值的数据:使用传统的关系型数据库存储一些数据时可能会比较昂贵,在此之前,很多时候程序员往往会选择传统的文件进行存储。
  4. 高伸缩性的场景:Mongo非常适合由数十或数百台服务器组成的数据库。Mongo的路线图中已经包含对MapReduce引擎的内置支持。
  5. 用于对象及JSON数据的存储:Mongo的BSON数据格式非常适合文档化格式的存储及查询。

3, 下载安装

mongoDB 下载, 最新版本 mongodb-linux-x86_64-2.2.3.tgz

mongoDB 安装:

tar zxvf mongodb-linux-x86_64-2.2.3.tgz

cd mongodb-linux-x86_64-2.2.3

sudo mkdir /opt/mongodb-2.2.3

sudo ./bin/mongod --dbpath=/opt/mongodb-2.2.3/


安装完毕后,在浏览器URL栏输入: http://localhost:27017/,出现下面内容则表示安装成功!



上图,提示端口号加上1000,然后通过HTTP访问,于是在浏览器地址栏输入:http://localhost:28017,访问界面如下:




4, 启动关闭

1) 默认启动

sudo ./bin/mongod    (默认保存文件目录为/data/db/, 没有则新建 sudo mkdir -p /data/db/, 默认端口为27017)


2)指定目录启动

sudo ./bin/mongod --dbpath=/opt/mongodb-2.2.3/       (保存文件目录为/opt/mongodb-2.2.3/ , 没有则新建)


3) 指定端口启动

sudo ./bin/mongod --dbpath=/opt/mongodb-2.2.3/ -p 27000        (端口修改为指定27000),启动输出如下:

MongoDB starting : pid=4222 port=27000 dbpath=/opt/mongodb-2.2.3/ 32-bit host=ubuntu

mongodb.conf配置文件启动:

homer@ubuntu:~$ cat /opt/mongodb/mongodb.conf    
port=27017
dbpath=/opt/mongodb/data/
logpath=/opt/mongodb/log/mongodb.log
logappend=true

启动命令:

/opt/mongodb/bin/mongod -f /opt/mongodb/mongodb.conf &


4) 后台启动命令

sudo ./bin/mongod -shardsvr -replSet shard1 -port=27017 -dbpath=/opt/mongodb-2.2.3/ -logpath=/opt/mongodb-2.2.3/log -logappend -rest -fork


5) 后台关闭命令

$ .bin/mongod                       // 登录客户端
> use admin                          // 切换到管理员
> db.shutdownServer()         // 关闭mongodb

或者

> db.adminCommand({shutdown : 1, force : true})
> //or
> db.shutdownServer({force : true})

> db.adminCommand(shutdown : 1, force : true, timeoutsec : 5)      
  // 超时关闭
> //or
> db.shutdownServer({force : true, timeoutsec : 5})


6) 客户端启动

默认启动: ./bin/mongo

指定端口启动: ./bin/mongo --port 27000


7) 备份与恢复

[php] view plaincopyprint?在CODE上查看代码片派生到我的代码片
  1. $ sudo ./bin/mongodump -d foo -o /opt/mongodb-2.2.3/bk/                  // 备份到bk目录  
  2. connected to: 127.0.0.1  
  3. Tue Feb  5 20:58:13 DATABASE: foo    to     /opt/mongodb-2.2.3/bk/foo  
  4. Tue Feb  5 20:58:13     foo.foo_test to /opt/mongodb-2.2.3/bk/foo/foo_test.bson  
  5. Tue Feb  5 20:58:13          1 objects  
  6. Tue Feb  5 20:58:13     Metadata for foo.foo_test to /opt/mongodb-2.2.3/bk/foo/foo_test.metadata.json  
  7. $ ./bin/mongorestore -d foo -c foo_test /opt/mongodb-2.2.3/bk/foo/foo_test.bson          // 恢复foo_test集合  
  8. connected to: 127.0.0.1  
  9. Tue Feb  5 20:58:20 /opt/mongodb-2.2.3/bk/foo/foo_test.bson  
  10. Tue Feb  5 20:58:20     going into namespace [foo.foo_test]  
  11. Tue Feb  5 20:58:20 warning: Restoring to foo.foo_test without dropping. Restored data will be inserted without raising errors; check your server log  
  12. Tue Feb  5 20:58:20 WARNING: collection foo.foo_test exists with different options than are in the metadata.json file and not using --drop. Options in the metadata file will be ignored.  
  13. 1 objects found  
  14. Tue Feb  5 20:58:20     Creating index: { key: { _id: 1 }, ns: "foo.foo_test", name: "_id_" }  


5, 命令示例

1) 启动服务端

sudo ./bin/mongod --dbpath=/opt/mongodb-2.2.3/


2) 启动客户端

./bin/mongo


3) 客户端简单命令

帮助: help

显示数据库: show dbs;

选择数据库: use test;


插入数据: db.foo.save({homer:2}); 

查询: db.foo.find();

结果: { "_id" : ObjectId("511078ad857b699e9eaaf516"), "homer" : 2 }

更新: db.foo.update({"homer":2},{"homer":3});         // ( {}, {} ) 第一个{}是查询条件,第二个{}是重新赋值

结果: { "_id" : ObjectId("511078ad857b699e9eaaf516"), "homer" : 3 }


删除: db.foo.remove({"homer":3});


复合插入:

t={"homer":2, "name" : {"name1" : "yang", "name2" : "gang"} }
{ "homer" : 2, "name" : { "name1" : "yang", "name2" : "gang" } }
db.foo.insert(t);
db.foo.find();
{ "_id" : ObjectId("5110d3e0af69ad089b234001"), "homer" : 2, "name" : { "name1" : "yang", "name2" : "gang" } }


复合查询:

> db.foo.find();              // 先查看集合中所有记录
{ "_id" : ObjectId("5110ca1aaf69ad089b233fff"), "homer" : 222 }
{ "_id" : ObjectId("5110caffaf69ad089b234000"), "homer" : 222 }
{ "_id" : ObjectId("5110d3e0af69ad089b234001"), "homer" : 2, "name" : { "name1" : "yang", "name2" : "gang" } }


db.foo.find({"homer":2});           // 查询包含特定内容的记录
{ "_id" : ObjectId("5110d3e0af69ad089b234001"), "homer" : 2, "name" : { "name1" : "yang", "name2" : "gang" } }


MongoDB 创建示例:

[php] view plaincopyprint?在CODE上查看代码片派生到我的代码片
  1. use test2                // test2数据库不存在也可以执行,但不会立刻创建,需要执行insert操作时才创建  
  2. switched to db test2  
  3. > show dbs                 // 没有test2,表明不是立刻创建  
  4. local   (empty)  
  5. test    0.0625GB  
  6. >  
  7. > db                       // 显示当前正在使用的数据库,test2不存在但正在使用中  
  8. test2  
  9. > db.createCollection("t2_test")     // 创建集合  
  10. "ok" : 1 }  
  11. > t={"name""yanggang""sex" : "male""lover" : { "lover1" : "little dog""lover2" : "fish" }}           // 初始化集合数据  
  12. {  
  13.     "name" : "yanggang",  
  14.     "sex" : "male",  
  15.     "lover" : {  
  16.         "lover1" : "little dog",  
  17.         "lover2" : "fish"  
  18.     }  
  19. }  
  20. > db.t2_test.insert(t)            // 插入数据,此刻才会创建test2数据库  
  21. > db.t2_test.find()               // 查询  
  22. "_id" : ObjectId("5110d685af69ad089b234002"), "name" : "yanggang""sex" : "male""lover" : { "lover1" : "little dog""lover2" : "fish" } }  
  23. > show dbs                        // 查看数据库,多了test2  
  24. local   (empty)  
  25. test    0.0625GB  
  26. test2   0.0625GB  

MongoDB 删除示例:

[php] view plaincopyprint?在CODE上查看代码片派生到我的代码片
  1. > show collections        // 显示集合  
  2. system.indexes  
  3. t2_test  
  4. > db.t2_test.drop()       // 删除集合t2_test  
  5. true  
  6. > show collections        // 验证删除成功  
  7. system.indexes  
  8. > db.test2.remove()  
  9. > show dbs                // 显示数据库  
  10. local   (empty)  
  11. test    0.0625GB  
  12. test2   0.0625GB  
  13. > db                      // 显示当前数据库  
  14. test2  
  15. > db.dropDatabase()       // 删除数据库  
  16. "dropped" : "test2""ok" : 1 }  
  17. > show dbs                // 验证删除成功  
  18. local   (empty)  
  19. test    0.0625GB  


MongoDB 索引

[php] view plaincopyprint?在CODE上查看代码片派生到我的代码片
  1. > db.foo.find()  
  2. "_id" : ObjectId("5110ca1aaf69ad089b233fff"), "homer" : 222 }  
  3. "_id" : ObjectId("5110caffaf69ad089b234000"), "homer" : 222 }  
  4. "_id" : ObjectId("5110d3e0af69ad089b234001"), "homer" : 2, "name" : { "name1" : "yang""name2" : "gang" } }  
  5. > db.foo.getIndexes()                        // 查看索引(_id为默认,不可删除和修改)  
  6. [  
  7.     {  
  8.         "v" : 1,  
  9.         "key" : {  
  10.             "_id" : 1  
  11.         },  
  12.         "ns" : "test.foo",  
  13.         "name" : "_id_"  
  14.     }  
  15. ]  
  16. > db.foo.ensureIndex({"homer":1})            // 新建索引  
  17. > db.foo.getIndexes()                        // 验证新建索引  
  18. [  
  19.     {  
  20.         "v" : 1,  
  21.         "key" : {  
  22.             "_id" : 1  
  23.         },  
  24.         "ns" : "test.foo",  
  25.         "name" : "_id_"  
  26.     },  
  27.     {  
  28.         "v" : 1,  
  29.         "key" : {  
  30.             "homer" : 1  
  31.         },  
  32.         "ns" : "test.foo",  
  33.         "name" : "homer_1"  
  34.     }  
  35. ]  
  36. > db.foo.dropIndex({"homer":1})              // 删除索引  
  37. "nIndexesWas" : 2, "ok" : 1 }  
  38. > db.foo.getIndexes()                        // 验证删除索引  
  39. [  
  40.     {  
  41.         "v" : 1,  
  42.         "key" : {  
  43.             "_id" : 1  
  44.         },  
  45.         "ns" : "test.foo",  
  46.         "name" : "_id_"  
  47.     }  
  48. ]  


6, 应用示例

[php] view plaincopyprint?在CODE上查看代码片派生到我的代码片
  1. > db.foo_test.insert({"homer":1})  
  2. for(var i = 1; i<=5; i++) db.foo_test.save({"homer":2, "score":i})            // 数组生成  
  3. > db.foo_test.find()  
  4. "_id" : ObjectId("5111016cc0e386db7d954ae7"), "homer" : 1 }  
  5. "_id" : ObjectId("5111030f0e2d3a50c56b49a9"), "homer" : 2, "score" : 6 }  
  6. "_id" : ObjectId("511103200e2d3a50c56b49aa"), "homer" : 2, "score" : 1 }  
  7. "_id" : ObjectId("511103200e2d3a50c56b49ab"), "homer" : 2, "score" : 2 }  
  8. "_id" : ObjectId("511103200e2d3a50c56b49ac"), "homer" : 2, "score" : 3 }  
  9. "_id" : ObjectId("511103200e2d3a50c56b49ad"), "homer" : 2, "score" : 4 }  
  10. "_id" : ObjectId("511103200e2d3a50c56b49ae"), "homer" : 2, "score" : 5 }  
  11. >   
  12. var cursor = db.foo_test.find(); while(cursor.hasNext())printjson(cursor.next());      // 遍历数组  
  13. "_id" : ObjectId("5111016cc0e386db7d954ae7"), "homer" : 1 }  
  14. "_id" : ObjectId("5111030f0e2d3a50c56b49a9"), "homer" : 2, "score" : 6 }  
  15. "_id" : ObjectId("511103200e2d3a50c56b49aa"), "homer" : 2, "score" : 1 }  
  16. "_id" : ObjectId("511103200e2d3a50c56b49ab"), "homer" : 2, "score" : 2 }  
  17. "_id" : ObjectId("511103200e2d3a50c56b49ac"), "homer" : 2, "score" : 3 }  
  18. "_id" : ObjectId("511103200e2d3a50c56b49ad"), "homer" : 2, "score" : 4 }  
  19. "_id" : ObjectId("511103200e2d3a50c56b49ae"), "homer" : 2, "score" : 5 }  
  20. >   
  21. var cursor = db.foo_test.find(); printjson(cursor[4])                          // 查询(迭代)  
  22. "_id" : ObjectId("511103200e2d3a50c56b49ac"), "homer" : 2, "score" : 3 }  
  23. var arr = db.foo_test.find().toArray(); arr[5]                             // 查询(数组)  
  24. "_id" : ObjectId("511103200e2d3a50c56b49ad"), "homer" : 2, "score" : 4 }  
  25. > db.foo_test.find().limit(3)                                                    // 查询(前三)  
  26. "_id" : ObjectId("5111016cc0e386db7d954ae7"), "homer" : 1 }  
  27. "_id" : ObjectId("5111030f0e2d3a50c56b49a9"), "homer" : 2, "score" : 6 }  
  28. "_id" : ObjectId("511103200e2d3a50c56b49aa"), "homer" : 2, "score" : 1 }
0 0
原创粉丝点击