如何在 Windows 上安装 MongoDB

如何在 Windows 上安装 MongoDB

原文: https://howtodoinjava.com/mongodb/how-to-install-mongodb-on-windows/

在本 MongoDB 教程中,我列出了在 Windows 计算机上安装 MongoDB 的步骤。 另外,我们还将学习与 MongoDB 的启动和关闭有关的一些基本操作。

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(下载链接

2)Windows 版 64 位 MongoDB(下载链接

3)Windows 版 32 位 MongoDB(下载链接

要查找您正在运行的 Windows 版本,请在命令提示符中输入以下命令:c:\> wmic os get osarchitecture

更多下载选项在 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。 现在将以下配置选项放置在文件中。 您可以随意更改选项的值。

##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 dont really care about your data (or more so, the loss of it).
journal = true

4. 启动/关闭 MongoDB 服务器

启动 MongoDB 服务器,请在命令提示符下使用以下命令:

mongod.exe –config d:\mongodb\mongo.config

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

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()

> 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

以下是上述所有四个命令的示例运行:

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 驱动

从此下载链接下载 MongoDB Java 驱动(mongo-java-driver-2.9.3.jar)。 这是一个 jar 文件,您需要在要使用 MongoDB 的项目的libpath文件夹中的classpath/copy中包含该文件。

7. 验证 MongoDB 安装

要验证是否已安装 MongoDB 并正常工作,请执行以下程序:

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 操作。 跟着我保持关注。

学习愉快!