MongoDB的安装及简单使用
MongoDB的介绍
MongoDB是一个个以 JSON 为数据模型的文档数据库。
文档数据库:文档来自于“JSON Document”,并非我们一般理解的 PDF,WORD 文档。
上市公司 MongoDB Inc. ,总部位于美国纽约。
用途:应用数据库,类似于 Oracle, MySQL 海量数据处理,数据平台。
特点:建模为可选 JSON 数据模型比较适合开发者 横向扩展可以支撑很大数据量和并发
MongoDB 是免费的吗?:MongoDB 有两个发布版本:社区版和企业版。 社区版是基于 SSPL,一种和 AGPL 基本类似的开源协议 。 企业版是基于商业协议,需付费使用。
MongoDB 版本变迁
0.x: 起步阶段 2008--->1.x: 支持复制集 和分片集 2010------>2.x: 更丰富的数 据库功能 2012------->3.x: WiredTiger 和周边生态环境 2014------->4.x: 分布式事务 支持 2018
MongoDB与关系型数据库对比
对比项 | MongoDB | 关系型数据库 |
数据模型 | 文档模型 | 关系模型 |
数据库类型 | OLTB | OLTB |
高可用 | 复制集 | 集群模式 |
横向扩展能力 | 通过原生分片完善支持 | 数据分区或者应用侵入式 |
索引支持 | B-树、全文索引、地理位置索引、多键(multikey) 索引、TTL 索引 | B树 |
开发难度 | 简单 | 困难 |
数据容量 | 没有理论上限 | 千万 、亿 |
扩展方式 | 水平扩张+垂直扩展 | 垂直扩展 |
MongoDB 优势:面向开发者的易用+高效数据库
简单直观: 以自然的方式来建模,以直观的 方式来与数据库交互
结构灵活: 弹性模式从容响应需求的频繁变化
快速开发: 做更多的事,写更少的代码
从错综复杂的关系模型->到一目了然的对象模型
灵活: 快速响应业务变化
多形性: 同一个集合中可以包含 不同字段(类型)的文档对象
动态性: 线上修改数据模式,修 改是应用与数据库均无须下线
数据治理: 支持使用 JSON Schema 来规范数据模式。在保 证模式灵活动态的前提下,提供 数据治理能力
快速: 最简单快速的开发方式
JSON 模型之快速特性: 数据库引擎只需要在一个存储区读写 。 反范式、无关联的组织极大优化查询 速度 。 程序 API 自然,开发快速
快速: MongoDB 只需要两行代码
MongoDB 优势:原生的高可用和横向扩展能力。Replica Set – 2 to 50 个成员 。自恢复 。 多中心容灾能力 。 滚动服务 – 最小化服务终端
MongoDB 优势:横向扩展能力需要的时候无缝扩展 。 应用全透明 。 多种数据分布策略 。 轻松支持 TB – PB 数量级
MongoDB 技术优势总结
JSON 结构和对象模型接近,开发代码量低 。JSON 的动态模型意味着更容易响应新的业务需求 。复制集提供 99.999% 高可用 。分片架构支持海量数据和无缝扩容
MongoDB的安装
下载
[root@master conf]# wget https://fastdl.mongodb.org/linux/mongodb-linux-x86_64-rhel70-4.2.5.tgz --2020-04-16 10:19:15-- https://fastdl.mongodb.org/linux/mongodb-linux-x86_64-rhel70-4.2.5.tgz 正在解析主机 fastdl.mongodb.org (fastdl.mongodb.org)... 13.226.71.43, 13.226.71.101, 13.226.71.29, ... 正在连接 fastdl.mongodb.org (fastdl.mongodb.org)|13.226.71.43|:443... 已连接。 已发出 HTTP 请求,正在等待回应... 200 OK 长度:132702741 (127M) [application/gzip] 正在保存至: “mongodb-linux-x86_64-rhel70-4.2.5.tgz” 100%[=====================================================================================================================================================>] 132,702,741 4.58MB/s 用时 33s 2020-04-16 10:19:54 (3.85 MB/s) - 已保存 “mongodb-linux-x86_64-rhel70-4.2.5.tgz” [132702741/132702741])
安装
[root@master conf]# mkdir -p /dada/db [root@master conf]# mv mongodb-linux-x86_64-rhel70-4.2.5.tgz /data/ [root@master conf]# cd /data/ [root@master data]# tar xf mongodb-linux-x86_64-rhel70-4.2.5.tgz [root@master data]# vim /etc/profile.d/mongodb.sh export PATH=$PATH:/data/mongodb-linux-x86_64-rhel70-4.2.5/bin/ [root@master data]# exec bash [root@master data]# mkdir db [root@master data]# mongod --dbpath /data/db --port 27017 --logpath /data/db/mongod.log --fork [root@master data]# mongo MongoDB shell version v4.2.5 connecting to: mongodb://127.0.0.1:27017/?compressors=disabled&gssapiServiceName=mongodb Implicit session: session { "id" : UUID("347ea861-edd1-4c69-96ae-944ddadc8e9b") } MongoDB server version: 4.2.5 Welcome to the MongoDB shell. For interactive help, type "help". For more comprehensive documentation, see http://docs.mongodb.org/ Questions? Try the support group http://groups.google.com/group/mongodb-user Server has startup warnings: 2020-04-16T11:35:00.912+0800 I CONTROL [initandlisten] 2020-04-16T11:35:00.914+0800 I CONTROL [initandlisten] ** WARNING: Access control is not enabled for the database. 2020-04-16T11:35:00.914+0800 I CONTROL [initandlisten] ** Read and write access to data and configuration is unrestricted. 2020-04-16T11:35:00.914+0800 I CONTROL [initandlisten] ** WARNING: You are running this process as the root user, which is not recommended. 2020-04-16T11:35:00.914+0800 I CONTROL [initandlisten] 2020-04-16T11:35:00.914+0800 I CONTROL [initandlisten] ** WARNING: This server is bound to localhost. 2020-04-16T11:35:00.914+0800 I CONTROL [initandlisten] ** Remote systems will be unable to connect to this server. 2020-04-16T11:35:00.914+0800 I CONTROL [initandlisten] ** Start the server with --bind_ip <address> to specify which IP 2020-04-16T11:35:00.914+0800 I CONTROL [initandlisten] ** addresses it should serve responses from, or with --bind_ip_all to 2020-04-16T11:35:00.914+0800 I CONTROL [initandlisten] ** bind to all interfaces. If this behavior is desired, start the 2020-04-16T11:35:00.914+0800 I CONTROL [initandlisten] ** server with --bind_ip 127.0.0.1 to disable this warning. 2020-04-16T11:35:00.914+0800 I CONTROL [initandlisten] 2020-04-16T11:35:00.914+0800 I CONTROL [initandlisten] 2020-04-16T11:35:00.914+0800 I CONTROL [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/enabled is 'always'. 2020-04-16T11:35:00.914+0800 I CONTROL [initandlisten] ** We suggest setting it to 'never' 2020-04-16T11:35:00.914+0800 I CONTROL [initandlisten] 2020-04-16T11:35:00.914+0800 I CONTROL [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/defrag is 'always'. 2020-04-16T11:35:00.914+0800 I CONTROL [initandlisten] ** We suggest setting it to 'never' 2020-04-16T11:35:00.914+0800 I CONTROL [initandlisten] --- Enable MongoDB's free cloud-based monitoring service, which will then receive and display metrics about your deployment (disk utilization, CPU, operation statistics, etc). The monitoring data will be available on a MongoDB website with a unique URL accessible to you and anyone you share the URL with. MongoDB may use this information to make product improvements and to suggest MongoDB products and deployment options to you. To enable free monitoring, run the following command: db.enableFreeMonitoring() To permanently disable this reminder, run the following command: db.disableFreeMonitoring() --- >
curl -O -k https://raw.githubusercontent.com/tapdata/geektimemongodb-course/master/aggregation/dump.tar.gz [root@master data]# tar xf /root/dump.tar.gz -C . [root@master data]# mongorestore -h 127.0.0.1:27017 2020-04-16T14:42:57.930+0800 using default 'dump' directory 2020-04-16T14:42:57.930+0800 preparing collections to restore from 2020-04-16T14:42:57.931+0800 reading metadata for mock.orders from dump/mock/orders.metadata.json 2020-04-16T14:42:57.956+0800 restoring mock.orders from dump/mock/orders.bson 2020-04-16T14:43:00.933+0800 [##################......] mock.orders 89.5MB/115MB (78.1%) 2020-04-16T14:43:03.929+0800 [#####################...] mock.orders 102MB/115MB (89.1%) 2020-04-16T14:43:05.803+0800 [########################] mock.orders 115MB/115MB (100.0%) 2020-04-16T14:43:05.803+0800 no indexes to restore 2020-04-16T14:43:05.803+0800 finished restoring mock.orders (100000 documents, 0 failures) 2020-04-16T14:43:05.803+0800 100000 document(s) restored successfully. 0 document(s) failed to restore.
重启设置监听地址;默认127.0.0.1
[root@master bin]# mongod --shutdown 2020-04-16T15:04:40.206+0800 I CONTROL [main] Automatically disabling TLS 1.0, to force-enable TLS 1.0 specify --sslDisabledProtocols 'none' killing process with pid: 10896 [root@master data]# mongod --dbpath /data/db --port 27017 --logpath /data/db/mongod.log --fork --bind_ip 192.168.10.16 about to fork child process, waiting until server is ready for connections. forked process: 10940 child process started successfully, parent exiting
使用mongo客户端连进去
[root@master data]# mongo --host 192.168.10.16 --port 27017 MongoDB shell version v4.2.1 connecting to: mongodb://192.168.10.16:27017/?compressors=disabled&gssapiServiceName=mongodb Implicit session: session { "id" : UUID("b9ca7e5a-4c5d-4066-9b69-3608bd487caf") } MongoDB server version: 4.2.1 Server has startup warnings: 2020-04-16T15:05:22.267+0800 I CONTROL [initandlisten] 2020-04-16T15:05:22.267+0800 I CONTROL [initandlisten] ** WARNING: Access control is not enabled for the database. 2020-04-16T15:05:22.267+0800 I CONTROL [initandlisten] ** Read and write access to data and configuration is unrestricted. 2020-04-16T15:05:22.267+0800 I CONTROL [initandlisten] ** WARNING: You are running this process as the root user, which is not recommended. 2020-04-16T15:05:22.267+0800 I CONTROL [initandlisten] 2020-04-16T15:05:22.267+0800 I CONTROL [initandlisten] 2020-04-16T15:05:22.267+0800 I CONTROL [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/enabled is 'always'. 2020-04-16T15:05:22.267+0800 I CONTROL [initandlisten] ** We suggest setting it to 'never' 2020-04-16T15:05:22.267+0800 I CONTROL [initandlisten] 2020-04-16T15:05:22.267+0800 I CONTROL [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/defrag is 'always'. 2020-04-16T15:05:22.267+0800 I CONTROL [initandlisten] ** We suggest setting it to 'never' 2020-04-16T15:05:22.267+0800 I CONTROL [initandlisten] --- Enable MongoDB's free cloud-based monitoring service, which will then receive and display metrics about your deployment (disk utilization, CPU, operation statistics, etc). The monitoring data will be available on a MongoDB website with a unique URL accessible to you and anyone you share the URL with. MongoDB may use this information to make product improvements and to suggest MongoDB products and deployment options to you. To enable free monitoring, run the following command: db.enableFreeMonitoring() To permanently disable this reminder, run the following command: db.disableFreeMonitoring() --- > show dbs 查看 admin 0.000GB config 0.000GB local 0.000GB mock 0.046GB > use mock 进入 switched to db mock > db.orders.findOne() 查看 { "_id" : ObjectId("5dbe7a545368f69de2b4d36e"), "street" : "493 Hilll Curve", "city" : "Champlinberg", "state" : "Texas", "country" : "Malaysia", "zip" : "24344-1715", "phone" : "425.956.7743 x4621", "name" : "Destinee Schneider", "userId" : 3573, "orderDate" : ISODate("2019-03-26T03:20:08.805Z"), "status" : "created", "shippingFee" : NumberDecimal("8.00"), "orderLines" : [ { "product" : "Refined Fresh Tuna", "sku" : "2057", "qty" : 25, "price" : NumberDecimal("56.00"), "cost" : NumberDecimal("46.48") }, { "product" : "Refined Concrete Ball", "sku" : "1738", "qty" : 61, "price" : NumberDecimal("47.00"), "cost" : NumberDecimal("47") }, { "product" : "Rustic Granite Towels", "sku" : "500", "qty" : 62, "price" : NumberDecimal("74.00"), "cost" : NumberDecimal("62.16") }, { "product" : "Refined Rubber Salad", "sku" : "1400", "qty" : 73, "price" : NumberDecimal("93.00"), "cost" : NumberDecimal("87.42") }, { "product" : "Intelligent Wooden Towels", "sku" : "5674", "qty" : 72, "price" : NumberDecimal("84.00"), "cost" : NumberDecimal("68.88") }, { "product" : "Refined Steel Bacon", "sku" : "5009", "qty" : 8, "price" : NumberDecimal("53.00"), "cost" : NumberDecimal("50.35") } ], "total" : NumberDecimal("407") } >
使用图形客户端MongoDB compass
连上去