企业🤖AI智能体构建引擎,智能编排和调试,一键部署,支持私有化部署方案 广告
# 如何在 Windows 上安装 MongoDB > 原文: [https://howtodoinjava.com/mongodb/how-to-install-mongodb-on-windows/](https://howtodoinjava.com/mongodb/how-to-install-mongodb-on-windows/) 在本 MongoDB 教程中,我列出了**在 Windows** 计算机上安装 MongoDB 的步骤。 另外,我们还将学习与 MongoDB 的启动和关闭有关的一些基本操作。 ```java Table of Contents 1) Download MongoDB 2) Install MongoDB 3) Create mongo.config Configuration File 4) Start/Shutdown the MongoDB Server 5) MongoDB Windows Service 6) Download/Use MongoDB Java Driver 7) Verify MongoDB Installation ``` ## 1\. 下载 MongoDB Windows 有 MongoDB 的三种构建: 1)Windows Server 2008 R2 版的 MongoDB([下载链接](https://fastdl.mongodb.org/win32/mongodb-win32-x86_64-2008plus-2.6.1.zip "download mongodb for windows server 2008")) 2)Windows 版 64 位 MongoDB([下载链接](https://fastdl.mongodb.org/win32/mongodb-win32-x86_64-2.6.1.zip "Download mongodb for windows 64 bit")) 3)Windows 版 32 位 MongoDB([下载链接](https://fastdl.mongodb.org/win32/mongodb-win32-i386-2.6.1.zip "Download mongodb for windows 32 bit")) 要查找您正在运行的 Windows 版本,请在命令提示符中输入以下命令:`c:\> wmic os get osarchitecture` [**更多下载选项**](https://www.mongodb.org/downloads "MongoDB downloads")在 MongoDB 的官方网站上提供。 ## 2\. 安装 MongoDB 上面给定的链接将下载 zip 文件,您可以将其直接提取到所选系统中的任何位置。 我已经将它们提取到“`d:/mongodb`”中。 因此,本文中的所有代码示例以及以后的文章都将引用此位置。 建议在 Windows 环境变量中添加`d:/mongodb/bin`,以便您可以直接在命令提示符下访问 MongoDB 的命令。 另外,请**在`d:/mongodb`中创建以下目录** * `D:\mongodb\data` * `D:\mongodb\log` ## 3\. 创建`mongo.config`配置文件 这是前进之前的重要一步。 在位置`d:/mongodb`中创建一个普通文本文件,并将其保存为名称`mongo.config`。 现在将以下配置选项放置在文件中。 您可以随意更改选项的值。 ```java ##Which IP address(es) mongod should bind to. bind_ip = 127.0.0.1 ##Which port mongod should bind to. port = 27017 ##I set this to true, so that only critical events and errors are logged. quiet = true ##store data here dbpath=D:\mongodb\data ##The path to the log file to which mongod should write its log messages. logpath=D:\mongodb\log\mongo.log ##I set this to true so that the log is not overwritten upon restart of mongod. logappend = true ##log read and write operations diaglog=3 ##It ensures write durability and data consistency much as any journaling scheme would be expected to do. ##Only set this to false if you don’t really care about your data (or more so, the loss of it). journal = true ``` ## 4\. 启动/关闭 MongoDB 服务器 要**启动 MongoDB 服务器**,请在命令提示符下使用以下命令: > `mongod.exe –config d:\mongodb\mongo.config` ```java D:\mongodb\bin>mongod --config D:\mongodb\mongo.config --journal 2014-05-25T16:51:18.433+0530 warning: --diaglog is deprecated and will be removed in a future release 2014-05-25T16:51:18.434+0530 diagLogging level=3 2014-05-25T16:51:18.435+0530 diagLogging using file D:\mongodb\data/diaglog.5381d22e ``` 要在命令提示符下将**连接到 MongoDB** ,请使用以下命令: > `d:\mongodb\bin>mongo` ```java D:\mongodb\bin>mongo MongoDB shell version: 2.6.1 connecting to: test 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: 2014-05-25T16:52:09.158+0530 [initandlisten] 2014-05-25T16:52:09.158+0530 [initandlisten] ** NOTE: This is a 32 bit MongoDB binary. 2014-05-25T16:52:09.158+0530 [initandlisten] ** 32 bit builds are limited to less than 2GB of data (or less with --jour nal). 2014-05-25T16:52:09.158+0530 [initandlisten] ** See http://dochub.mongodb.org/core/32bit 2014-05-25T16:52:09.158+0530 [initandlisten] ``` 要**关闭 MongoDB 服务器**,您必须是授权用户。 因此,完成认证后,在命令提示符下使用以下命令: > `db.shutdownServer()` ```java > use admin switched to db admin > db.shutdownServer() 2014-05-25T19:55:25.221+0530 DBClientCursor::init call() failed server should be down... 2014-05-25T19:55:25.224+0530 trying reconnect to 127.0.0.1:27017 (127.0.0.1) failed 2014-05-25T19:55:26.225+0530 warning: Failed to connect to 127.0.0.1:27017, reason: errno:10061 No connection could be made b ecause the target machine actively refused it. 2014-05-25T19:55:26.225+0530 reconnect 127.0.0.1:27017 (127.0.0.1) failed failed couldn't connect to server 127.0.0.1:27017 ( 127.0.0.1), connection attempt failed > quit() D:\mongodb\bin> ``` ## 5\. MongoDB Windows 服务 要安装窗口服务,请使用以下命令: > `mongod –config D:\mongodb\mongo.config –install` 从命令提示符启动 Windows 服务: > `net start MongoDB` 从命令提示符处停止 Windows 服务: > `net stop MongoDB` 删除 Windows 服务 > `mongod –delete` 以下是上述所有四个命令的示例运行: ```java D:\mongodb\bin>mongod --config D:\mongodb\mongo.config --install 2014-05-25T20:07:41.191+0530 warning: --diaglog is deprecated and will be removed in a future release 2014-05-25T20:07:41.192+0530 diagLogging level=3 2014-05-25T20:07:41.193+0530 diagLogging using file D:\mongodb\data/diaglog.53820035 D:\mongodb\bin>net start MongoDB The MongoDB service was started successfully. D:\mongodb\bin>net stop MongoDB System error 109 has occurred. The pipe has been ended. D:\mongodb\bin>mongod --remove 2014-05-25T20:09:32.514+0530 2014-05-25T20:09:32.515+0530 warning: 32-bit servers don't have journaling enabled by default. Please use --journal if you wa nt durability. 2014-05-25T20:09:32.515+0530 2014-05-25T20:09:32.518+0530 Trying to remove Windows service 'MongoDB' 2014-05-25T20:09:32.520+0530 Service 'MongoDB' removed ``` ## 6\. 下载/使用 MongoDB Java 驱动 从此[**下载链接**](http://central.maven.org/maven2/org/mongodb/mongo-java-driver/2.9.3/mongo-java-driver-2.9.3.jar "mongodb java driver download")下载 MongoDB Java 驱动(`mongo-java-driver-2.9.3.jar`)。 这是一个 jar 文件,您需要在要使用 MongoDB 的项目的`libpath`文件夹中的`classpath/copy`中包含该文件。 ## 7\. 验证 MongoDB 安装 要验证是否已安装 MongoDB 并正常工作,请执行以下程序: ```java package examples.mongodb.install; import java.net.UnknownHostException; import java.util.List; import com.mongodb.MongoClient; public class VerifyMongoDBInstallation { public static void main(String[] args) throws UnknownHostException { MongoClient mongo = new MongoClient("localhost", 27017); List<String> dbs = mongo.getDatabaseNames(); for (String db : dbs) { System.out.println(db); } } } Output: local admin ``` 全部完成 **MongoDB Windows 安装**,启动和关闭操作。 接下来,我们将学习一些 CRUD 操作。 跟着我保持关注。 学习愉快!