MongoDB 下载_安装_配置 及 启动与连接

本文导读

  • 本文承接《 MongoDB 理论简介》,熟悉 MongoDB 理论之后,本文介绍 MongoDB  在 Windows 系统中的 下载与安装配置。

MongoDB 下载

  • Commutity Server:社区版本,免费的版本,学习或者一般的项目用这种就足够了。
  • Enterprise Server :商业版本,收费的版本。
  • 以社区版本为例,选择自己对应的操作系统 “ Windows”、"Linux" 等,然后选择系统版本
  • 可以点击 " DOWNLOAD(msi)"  按钮直接下载 安装版的 .msi 文件,msi 文件是一种安装文件,点击按钮之后就会进行下载,注意如果网速慢,则可能要等一段时间,如果还是不行建议翻墙!
  • 也可以点击 "ALL Version Binaries" 链接,此时不仅可以下载所有的历史版本,而且还可以下载 解压版的软件,即下载后解压即可使用,无需安装,推荐方式,

  • 如上所示,可以下载任意的历史版本,*.zip 是解压版本,*.msi 是安装版本。
  • 推荐使用 解压版 即可,因为安装版本安装完成后目录结构与解压版是一样的(我猜的)。

目录介绍

  • 以上面 Windows 系统的 MongoDB 4.0.2 版本为例进行说明:
mongo.exe 客户端程序,连接 MongoDB
mongod.exe 服务端程序,启动 MongoDB
mongodump.exe 备份程序
mongoexport.exe 数据导出程序
mongofiles.exe GridFS工具,内建的分布式文件系统
mongoimport.exe 数据导入程序
mongorestore.exe 数据恢复程序
mongos.exe 数据分片程序,支持数据的横向扩展
mongostat.exe 监视程序

环境变量 配置

  • 为了能在 cmd 命令行中方便操作,需要将 MongoDB 的 bin 目录配置系统环境变量中的 path 变量中,如同之前配置 Java JDK 一样
  • 将上面准备好的 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

猜你喜欢

转载自blog.csdn.net/wangmx1993328/article/details/82585943