MongoDB 概述、下载安装、配置 、启动与连接

目录

NoSql 简介

MongoDB 简介

MongoDB 数据类型

MongoDB 下载安装

文件、目录介绍

环境变量 配置

MongoDB 操作

启动 MongoDB 数据库

连接 MongoDB 数据库

关闭 MongoDB 数据库


NoSql 简介

1、熟悉 MongoDB 之前,先要了解一下 NoSql !

2、NoSQL(Not Only SQL ),意即“不仅仅是SQL” ,指的是非关系型的数据库 。是一项全新的数据库革命性运动,早期就有人提出,发展至2009年趋势越发高涨。NoSQL的拥护者们提倡运用非关系型的数据存储,相对于铺天盖地的关系型数据库运用,这一概念无疑是一种全新的思维的注入。

3、关系型数据库中的表都是存储一些结构化的数据,每条记录的字段的组成都一样,即使不是每条记录都需要所有的字段,但数据库会为每条数据分配所有的字段。而非关系型数据库以键值对(key-value)存储,它的结构不固定,每一条记录可以有不一样的键,每条记录可以根据需要增加一些自己的键值对,这样就不会局限于固定的结构,可以减少一些时间和空间的开销。

常见的 NoSql (非关系型数据库) 数据库
NoSql 数据库应用网站有
CouchDB
Redis新浪微博
MongoDB视觉中国、优酷运营数据中心
Neo4j
HBase
BigTableGoogle

MongoDB 简介

1、MongoDB 是用 C++ 语言编写的非关系型数据库。

2、MongoDB 特点是高性能、易部署、易使用,存储数据十分方便,主要特性有:

1、面向集合存储,易于存储对象类型的数据
2、模式自由
3、支持动态查询
4、支持完全索引,包含内部对象
5、支持复制和故障恢复
6、使用高效的二进制数据存储,包括大型对象
7、文件存储格式为 BSON (一种JSON的扩展)

3、MongoDB 官网:https://www.mongodb.com/

MongoDB 对比 关系型数据库
对比项MongoDBMysql、Oracle
集合二维表(table)
表的一行数据文档(document)一条记录(record)
表字段键 (key)字段(field)
字段值值(value)值 (value)
主外键PK、FK
灵活度、扩展性极高

1、关系型数据表的每条记录(record)必须保证拥有每一个 field
2、MongDB 的每一个 document 的 key 可以不一样
3、关系型数据库查询使用 SQL,MongoDB 查询使用内置的 find 函数(基于 BSON的特殊查询工具)
4、关系型数据库(如 Mysql):数据库(database)、表(table)、记录(rows) 三个层次概念组成。
5、非关系型数据库(如 MongoDB):数据库(database)、集合(collection)、文档对象(document)三个层次概念组成。
6、MongoDB 中的集合对应 Mysql 中的表,但是 集合 中没有列和行的概念,集合中只有文档,一个文档就相当于一条记录,体现了模式自由的特点。

MongoDB 基本概念
文档(document)  MongoDB 中数据的基本单元,非常类似于关系型数据库系统中的行(但是比行要复杂的多)
集合(collection) 就是一组文档,如果说 MongoDB 中的文档类似于关系型数据库中的行,那么集合就如同表
MongoDB 的单个计算机可以容纳多个独立的数据库,每一个数据库都有自己的集合和权限
MongoDB 自带简洁但功能强大的 JavaScript shell,这个工具对于管理 MongoDB 实例和操作数据作用非常大
每一个文档都有一个特殊的键 "_id",它在文档所处的集合中是唯一的,相当于关系数据库中的表的主键

MongoDB 数据类型

数据类型描述举例
null表示空值或者未定义的对象  {"x":null}
布尔值 真或者假:true 或者 false {"x":true}
32位整数   32位整数。shell是不支持该类型的,shell中默认会转换成64位浮点数
64位整数 64位整数shell是不支持该类型的,shell中默认会转换成64位浮点数
64位浮点数64位浮点数,shell中的数字就是这一种类型{"x":3.14,"y":3}
字符串UTF-8字符串{"foo":"bar"}
符号shell不支持,shell会将数据库中的符号类型的数据自动转换成字符串
对象id文档的12字节的唯一id                 {"id": ObjectId()}
日期从标准纪元开始的毫秒数           {"date":new Date()}
正则表达式文档中可以包含正则表达式,遵循JavaScript的语法  {"foo":/foobar/i}
代码文档中可以包含JavaScript代码 {"x":function() {}}
未定义         undefined                                  {"x":undefined}
数组     值的集合或者列表                                {"arr": ["a","b"]}
内嵌文档  文档可以作为文档中某个key的value {"x":{"foo":"bar"}}

MongoDB 下载安装

1、MongoDB 官网下载地址:https://www.mongodb.com/try/download/community

2、选择 MongoDB  版本、操作系统、安装包类型即可下载,

Commutity Server:社区版本,免费的版本(推荐)。

Enterprise Server :商业版本,收费的版本。

以Windows系统为例:.msi 文件是安装版,.zip 是绿色版(解压即可使用,推荐)

namemodifiedsizemd5sigsha1sha256
https://repo.mongodb.org/yum/redhat/7/mongodb-org/7.0/x86_64/RPMS/mongodb-org-server-7.0.12-1.el7.x86_64.rpm
https://fastdl.mongodb.org/linux/mongodb-linux-x86_64-rhel70-7.0.12.tgz
https://fastdl.mongodb.org/windows/mongodb-windows-x86_64-7.0.12-signed.msi
https://fastdl.mongodb.org/windows/mongodb-windows-x86_64-7.0.12.zip
win32/mongodb-win32-x86_64-2008plus-ssl-debugsymbols-4.0.9.zip2019-04-11 02:44:28182641626md5sigsha1sha256
win32/mongodb-win32-x86_64-2008plus-ssl-4.0.9-signed.msi2019-04-11 02:42:57220098560md5sha1sha256
win32/mongodb-win32-x86_64-2008plus-ssl-4.0.9.zip2019-04-11 02:42:35255696155md5sigsha1sha256

推荐使用 解压版 即可,因为安装版本安装完成后目录结构与解压版是一样的(我猜的)。

文件、目录介绍

以上面 Windows 系统的 MongoDB 4.0.2 版本为例进行说明:

mongo.exe客户端程序,连接 MongoDB
mongod.exe服务端程序,启动 MongoDB
mongodump.exe备份程序
mongoexport.exe数据导出程序
mongofiles.exeGridFS工具,内建的分布式文件系统
mongoimport.exe数据导入程序
mongorestore.exe数据恢复程序
mongos.exe数据分片程序,支持数据的横向扩展
mongostat.exe监视程序

环境变量 配置

1、为了能在 cmd 命令行中方便操作,需要将 MongoDB 的 bin 目录配置系统环境变量中的 path 变量中,如同之前配置 Java JDK 一样。

2、将上面准备好的 D:\MongoDB\mongodb-win32-x86_64-2008plus-ssl-4.0.2-rc0\bin 追加到 Path 环境变量中

MongoDB 操作

启动 MongoDB 数据库

启动 MongoDB 数据库之前,要手动创建一个存放 MongoDB 数据文件的目录,如 D:\MongoDB\Data

在命令行执行 mongod --dbpath=D:\MongoDB\Data 启动 MongoDB 数据库,bin 目录下的 mongod.exe 程序就是用于启动 MongoDB 的。

--dbpath 后面接 MongoDB 数据库文件存储目录,注意这个物理目录就是一个 MongoDB 数据库

如下所示 MongoDB 数据库 启动 成功!

Microsoft Windows [版本 10.0.17134.112]
(c) 2018 Microsoft Corporation。保留所有权利。

C:\Users\Administrator.SC-201707281232>mongod --dbpath=D:\MongoDB\Data
2018-09-10T11:29:57.470+0800 I CONTROL  [main] Automatically disabling TLS 1.0, to force-enable TLS 1.0 specify --sslDisabledProtocols 'none'
2018-09-10T11:29:57.913+0800 I CONTROL  [initandlisten] MongoDB starting : pid=13500 port=27017 dbpath=D:\MongoDB\Data 64-bit host=SC-201707281232
2018-09-10T11:29:57.913+0800 I CONTROL  [initandlisten] targetMinOS: Windows 7/Windows Server 2008 R2
2018-09-10T11:29:57.914+0800 I CONTROL  [initandlisten] db version v4.0.2-rc0
2018-09-10T11:29:57.914+0800 I CONTROL  [initandlisten] git version: fc1573ba18aee42f97a3bb13b67af7d837826b47
2018-09-10T11:29:57.914+0800 I CONTROL  [initandlisten] allocator: tcmalloc
2018-09-10T11:29:57.914+0800 I CONTROL  [initandlisten] modules: none
2018-09-10T11:29:57.914+0800 I CONTROL  [initandlisten] build environment:
2018-09-10T11:29:57.914+0800 I CONTROL  [initandlisten]     distmod: 2008plus-ssl
2018-09-10T11:29:57.915+0800 I CONTROL  [initandlisten]     distarch: x86_64
2018-09-10T11:29:57.915+0800 I CONTROL  [initandlisten]     target_arch: x86_64
2018-09-10T11:29:57.915+0800 I CONTROL  [initandlisten] options: { storage: { dbPath: "D:\MongoDB\Data" } }
2018-09-10T11:29:57.916+0800 I STORAGE  [initandlisten] wiredtiger_open config: create,cache_size=3552M,session_max=20000,eviction=(threads_min=4,threads_max=4),config_base=false,statistics=(fast),log=(enabled=true,archive=true,path=journal,compressor=snappy),file_manager=(close_idle_time=100000),statistics_log=(wait=0),verbose=(recovery_progress),
2018-09-10T11:29:58.505+0800 I STORAGE  [initandlisten] WiredTiger message [1536550198:505724][13500:140730388790352], txn-recover: Set global recovery timestamp: 0
2018-09-10T11:29:58.681+0800 I RECOVERY [initandlisten] WiredTiger recoveryTimestamp. Ts: Timestamp(0, 0)
2018-09-10T11:29:59.159+0800 I CONTROL  [initandlisten]
2018-09-10T11:29:59.160+0800 I CONTROL  [initandlisten] ** WARNING: Access control is not enabled for the database.
2018-09-10T11:29:59.160+0800 I CONTROL  [initandlisten] **          Read and write access to data and configuration is unrestricted.
2018-09-10T11:29:59.160+0800 I CONTROL  [initandlisten]
2018-09-10T11:29:59.160+0800 I CONTROL  [initandlisten] ** WARNING: This server is bound to localhost.
2018-09-10T11:29:59.160+0800 I CONTROL  [initandlisten] **          Remote systems will be unable to connect to this server.
2018-09-10T11:29:59.161+0800 I CONTROL  [initandlisten] **          Start the server with --bind_ip <address> to specify which IP
2018-09-10T11:29:59.161+0800 I CONTROL  [initandlisten] **          addresses it should serve responses from, or with --bind_ip_all to
2018-09-10T11:29:59.162+0800 I CONTROL  [initandlisten] **          bind to all interfaces. If this behavior is desired, start the
2018-09-10T11:29:59.162+0800 I CONTROL  [initandlisten] **          server with --bind_ip 127.0.0.1 to disable this warning.
2018-09-10T11:29:59.162+0800 I CONTROL  [initandlisten]
2018-09-10T11:29:59.164+0800 I STORAGE  [initandlisten] createCollection: admin.system.version with provided UUID: ab94bc6f-4cf3-4bb7-9a64-51f27a13405a
2018-09-10T11:29:59.549+0800 I COMMAND  [initandlisten] setting featureCompatibilityVersion to 4.0
2018-09-10T11:29:59.557+0800 I STORAGE  [initandlisten] createCollection: local.startup_log with generated UUID: ffb7e260-d107-425d-bbc9-a7cbab38b91f
2018-09-10T11:30:00.428+0800 I FTDC     [initandlisten] Initializing full-time diagnostic data capture with directory 'D:/MongoDB/Data/diagnostic.data'
2018-09-10T11:30:00.434+0800 I STORAGE  [LogicalSessionCacheRefresh] createCollection: config.system.sessions with generated UUID: e32fd3e6-6b27-496a-b519-41add4bd68c7
2018-09-10T11:30:00.434+0800 I NETWORK  [initandlisten] waiting for connections on port 27017
2018-09-10T11:30:01.018+0800 I INDEX    [LogicalSessionCacheRefresh] build index on: config.system.sessions properties: { v: 2, key: { lastUse: 1 }, name: "lsidTTLIndex", ns: "config.system.sessions", expireAfterSeconds: 1800 }
2018-09-10T11:30:01.018+0800 I INDEX    [LogicalSessionCacheRefresh]     building index using bulk method; build may temporarily use up to 500 megabytes of RAM
2018-09-10T11:30:01.075+0800 I INDEX    [LogicalSessionCacheRefresh] build index done.  scanned 0 total records. 0 secs
2018-09-10T11:30:01.075+0800 I COMMAND  [LogicalSessionCacheRefresh] command config.$cmd command: createIndexes { createIndexes: "system.sessions", indexes: [ { key: { lastUse: 1 }, name: "lsidTTLIndex", expireAfterSeconds: 1800 } ], $db: "config" } numYields:0 reslen:114 locks:{ Global: { acquireCount: { r: 1, w: 1 } }, Database: { acquireCount: { W: 1 } }, Collection: { acquireCount: { w: 1 } } } protocol:op_msg 641ms

如上所示,cmd 命令行中启动了 MongoDB 数据库之后,如果关闭了此 cmd 面板,则意味着 MongoDB 数据库也关闭了

如同 Mysql 启动之后会默认使用 3306 端口一样,MongoDB 的默认端口为 27017

如下所示 使用 windows 自带的 netstat 与 tasklist 命令 轻松就可查端口和进程来。

C:\Users\Administrator.SC-201707281232>netstat -ano |findstr "27017"
  TCP    127.0.0.1:27017        0.0.0.0:0              LISTENING       13500
  TCP    127.0.0.1:27017        127.0.0.1:1809         ESTABLISHED     13500
C:\Users\Administrator.SC-201707281232>tasklist |findstr 13500
mongod.exe                   13500 Console                    1     39,540 K
C:\Users\Administrator.SC-201707281232>

自定义端口启动:mongod --dbpath=D:\MongoDB\Mater --port 27018,(使用 --port 进行指定)

使用安全认证启动:mongod --dbpath=D:\MongoDB\Mater --auth,(使用 --auth  参数,可以参考《MongoDB 安全与认证》)

以主节点启动:mongod --dbpath D:\MongoDB\Mater --port 10000 --master,(主从复制可以参考《 MongoDB 主从复制(主从集群 )》)

以从节点启动:mongod --dbpath D:\MongoDB\Slave --port 10001 --slave --source localhost:10000

连接 MongoDB 数据库

新开一个 cmd 窗口,在命令行中可以使用命令 “ mongo ” 连接到 MongoDB 服务器,bin 目录下的 mongo.exe 就是连接 MongoDB 的客户端程序。

mongo :默认连接本地(localhost) 的 27017 端口的 MongoDB 数据库

mongo localhost:27017:指定连接本地 27017 端口的 MongoDB 数据库,默认连接的是 test 数据库

mongo ip:port:连接到远程数据库时,指定 ip 与 端口即可

C:\Users\Administrator.SC-201707281232>mongo
MongoDB shell version v4.0.2-rc0
connecting to: mongodb://127.0.0.1:27017
MongoDB server version: 4.0.2-rc0
Server has startup warnings:
2018-09-10T11:29:59.159+0800 I CONTROL  [initandlisten]
2018-09-10T11:29:59.160+0800 I CONTROL  [initandlisten] ** WARNING: Access control is not enabled for the database.
2018-09-10T11:29:59.160+0800 I CONTROL  [initandlisten] **          Read and write access to data and configuration is unrestricted.
2018-09-10T11:29:59.160+0800 I CONTROL  [initandlisten]
2018-09-10T11:29:59.160+0800 I CONTROL  [initandlisten] ** WARNING: This server is bound to localhost.
2018-09-10T11:29:59.160+0800 I CONTROL  [initandlisten] **          Remote systems will be unable to connect to this server.
2018-09-10T11:29:59.161+0800 I CONTROL  [initandlisten] **          Start the server with --bind_ip <address> to specify which IP
2018-09-10T11:29:59.161+0800 I CONTROL  [initandlisten] **          addresses it should serve responses from, or with --bind_ip_all to
2018-09-10T11:29:59.162+0800 I CONTROL  [initandlisten] **          bind to all interfaces. If this behavior is desired, start the
2018-09-10T11:29:59.162+0800 I CONTROL  [initandlisten] **          server with --bind_ip 127.0.0.1 to disable this warning.
2018-09-10T11:29:59.162+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()
---
>
C:\Users\Administrator.SC-201707281232>mongo localhost:27017
MongoDB shell version v4.0.2-rc0
connecting to: mongodb://localhost:27017/test
MongoDB server version: 4.0.2-rc0
Server has startup warnings:
2018-09-10T11:29:59.159+0800 I CONTROL  [initandlisten]
2018-09-10T11:29:59.160+0800 I CONTROL  [initandlisten] ** WARNING: Access control is not enabled for the database.
2018-09-10T11:29:59.160+0800 I CONTROL  [initandlisten] **          Read and write access to data and configuration is unrestricted.
2018-09-10T11:29:59.160+0800 I CONTROL  [initandlisten]
2018-09-10T11:29:59.160+0800 I CONTROL  [initandlisten] ** WARNING: This server is bound to localhost.
2018-09-10T11:29:59.160+0800 I CONTROL  [initandlisten] **          Remote systems will be unable to connect to this server.
2018-09-10T11:29:59.161+0800 I CONTROL  [initandlisten] **          Start the server with --bind_ip <address> to specify which IP
2018-09-10T11:29:59.161+0800 I CONTROL  [initandlisten] **          addresses it should serve responses from, or with --bind_ip_all to
2018-09-10T11:29:59.162+0800 I CONTROL  [initandlisten] **          bind to all interfaces. If this behavior is desired, start the
2018-09-10T11:29:59.162+0800 I CONTROL  [initandlisten] **          server with --bind_ip 127.0.0.1 to disable this warning.
2018-09-10T11:29:59.162+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()
---
>

输入 "exit" 可以退出连接。

关闭 MongoDB 数据库

关闭数据库:先使用 use admin 切换数据库,然后输出 db.shutdownServer() 关闭数据库实例

当然也可以简单粗暴的直接关闭 数据库的 cmd 窗口

> use admin
switched to db admin
> db.shutdownServer()
server should be down...
2018-09-11T16:41:37.684+0800 I NETWORK  [js] trying reconnect to localhost:27017 failed
2018-09-11T16:41:38.751+0800 I NETWORK  [js] reconnect localhost:27017 failed failed

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

蚩尤后裔-汪茂雄

芝兰生于深林,不以无人而不芳。

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值