MongoDB教程7-MongoDB的安装与测试
MongoDB提供了可用于32位和64位系统的预编译二进制包,你可以从MongoDB官网下载安装,MongoDB预编译二进制包下载地址:
https://www.mongodb.com/try/download/community
本节以 Windows 为例,具体安装步骤可参考官网手册。
1) 本节采用的 MongoDB 版本为 4.2.8,安装环境为 Windows 64 位系统。安装步骤如下。
下载zip包后,解压安装包,并安装它。
安装过程中,你可以通过点击 "Custom(自定义)" 按钮来设置你的安装目录。
下一步安装 "install mongoDB compass" 不勾选(当然你也可以选择安装它,可能需要更久的安装时间),MongoDB Compass 是一个图形界面管理工具,我们可以在后面自己到官网下载安装,下载地址:https://www.mongodb.com/download-center/compass。
注意:实际中将 MongoDB 安装在了目录"D:\Program Files (x86)\MongoDB\Server\4.2"下
安装完毕后先不要启动,MongoDB将数据目录存储在 db 目录下。但是这个数据目录不会主动创建,我们在安装完成后需要创建它, 在 MongoDB 安装目录(D:\Program Files (x86)\MongoDB\Server\4.2)下创建 db 目录用于存储数据,创建 log 目录用于存储日志文件,创建完毕后的界面如图所示。
2) 与传统数据库一样,MongoDB 需要先开启服务端,再开启客户端,启动步骤如下。
① 配置MongoDB服务器。
在命令窗口切换到 D:\Program Files (x86)\MongoDB\Server\4.2\bin 目录,运行 mongod.exe 命令,同时指定数据库db和 log 日志文件的路径:
mongod.exe -dbpath "D:\Program Files (x86)\MongoDB\Server\4.2\db" -logpath "D:\Program Files (x86)\MongoDB\Server\4.2\log\mon.log"
查看 db 和 log 目录,会发现 MongoDB 自动创建了运行所需的文件,这种方式启动 MongoDB 为前台启动,命令行窗口不能关闭。
②启动 MongoDB 客户端。
在"D:\Program Files (x86)\MongoDB\Server\4.2\bin"下另开一个命令窗口,执行 mongo 命令进入 MongoDB 的 Shell 交互界面,如图所示。

$ mongo.exe MongoDB shell version v4.2.8 connecting to: mongodb://127.0.0.1:27017/?compressors=disabled&gssapiServiceName=mongodb Implicit session: session { "id" : UUID("059743c7-f2a2-43ab-bcc1-27dfe6eef79a") } MongoDB server version: 4.2.8 Server has startup warnings: 2020-09-02T14:04:35.400+0800 I CONTROL [initandlisten] 2020-09-02T14:04:35.400+0800 I CONTROL [initandlisten] ** WARNING: Access control is not enabled for the database. 2020-09-02T14:04:35.400+0800 I CONTROL [initandlisten] ** Read and write access to data and configuration is unrestricted. 2020-09-02T14:04:35.401+0800 I CONTROL [initandlisten] 2020-09-02T14:04:35.401+0800 I CONTROL [initandlisten] ** WARNING: This server is bound to localhost. 2020-09-02T14:04:35.401+0800 I CONTROL [initandlisten] ** Remote systems will be unable to connect to this server. 2020-09-02T14:04:35.401+0800 I CONTROL [initandlisten] ** Start the server with --bind_ip <address> to specify which IP 2020-09-02T14:04:35.401+0800 I CONTROL [initandlisten] ** addresses it should serve responses from, or with --bind_ip_all to 2020-09-02T14:04:35.401+0800 I CONTROL [initandlisten] ** bind to all interfaces. If this behavior is desired, start the 2020-09-02T14:04:35.401+0800 I CONTROL [initandlisten] ** server with --bind_ip 127.0.0.1 to disable this warning. 2020-09-02T14:04:35.417+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() --- >
3) 用户可将 MongoDB 服务设置为开机自启,方法如下。
① 配置 MongoDB 服务开机自启。
使用管理员身份进入命令窗口,切换到 D:\Program Files (x86)\MongoDB\Server\4.2\bin 目录,执行以下命令将 MongoDB 服务添加到系统服务中:
mongod.exe -dbpath "D:\Program Files (x86)\MongoDB\Server\4.2\db" -logpath "D:\Program Files (x86)\MongoDB\Server\4.2\log\mon.log" --install --serviceName "MongoDB"
② 开启 MongoDB 服务。
使用 net start 命令即可完成服务开机自启动设置,需要注意的是,一定要使用管理员身份打开 CMD 窗口。
net start MongoDB
③ 移除MongoDB服务开机自启。
使用管理员身份进入命令窗口,切换到 bin 目录,执行以下命令:
先停止服务
net stop MongoDB
移除服务
mongod.exe -dbpath "D:\Program Files (x86)\MongoDB\Server\4.2\db" -logpath "D:\Program Files (x86)\MongoDB\Server\4.2\log\mon.log" --remove --serviceName "MongoDB"
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
· 分享4款.NET开源、免费、实用的商城系统
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
· 上周热点回顾(2.24-3.2)