发布于 2015-09-14 14:42:01 | 201 次阅读 | 评论: 0 | 来源: 网络整理

Synopsis

This tutorial provides a method for installing and running the MongoDB server (i.e. “mongod.exe”) on the Microsoft Windows platform through the Command Prompt and outlines the process for setting up MongoDB as a Windows Service.

Operating MongoDB with Windows is similar to MongoDB on other platforms. Most components share the same operational patterns.

Procedure

Download MongoDB for Windows

Download the latest production release of MongoDB from the MongoDB downloads page.

There are three builds of MongoDB for Windows:

  • MongoDB for Windows Server 2008 R2 edition only runs on Windows Server 2008 R2, Windows 7 64-bit, and newer versions of Windows. This build takes advantage of recent enhancements to the Windows Platform and cannot operate on older versions of Windows.
  • MongoDB for Windows 64-bit runs on any 64-bit version of Windows newer than Windows XP, including Windows Server 2008 R2 and Windows 7 64-bit.
  • MongoDB for Windows 32-bit runs on any 32-bit version of Windows newer than Windows XP. 32-bit versions of MongoDB are only intended for older systems and for use in testing and development systems.

在 2.2 版更改: MongoDB does not support Windows XP. Please use a more recent version of Windows to use more recent releases of MongoDB.

注解

Always download the correct version of MongoDB for your Windows system. The 64-bit versions of MongoDB will not work with 32-bit Windows.

32-bit versions of MongoDB are suitable only for testing and evaluation purposes and only support databases smaller than 2GB.

You can find the architecture of your version of Windows platform using the following command in the Command Prompt

wmic os get osarchitecture

In Windows Explorer, find the MongoDB download file, typically in the default Downloads directory. Extract the archive to C: by right clicking on the archive and selecting Extract All and browsing to C:.

注解

The folder name will be either:

C:mongodb-win32-i386-[version]

Or:

C:mongodb-win32-x86_64-[version]

In both examples, replace [version] with the version of MongoDB downloaded.

Set up the Environment

Start the Command Prompt by selecting the Start Menu, then All Programs, then Accessories, then right click Command Prompt, and select Run as Administrator from the popup menu. In the Command Prompt, issue the following commands:

cd 
move C:mongodb-win32-* C:mongodb

注解

MongoDB is self-contained and does not have any other system dependencies. You can run MongoDB from any folder you choose. You may install MongoDB in any directory (e.g. D:testmongodb)

MongoDB requires a data folder to store its files. The default location for the MongoDB data directory is C:datadb. Create this folder using the Command Prompt. Issue the following command sequence:

md data
md datadb

注解

You may specify an alternate path for datadb with the dbpath setting for mongod.exe, as in the following example:

C:mongodbbinmongod.exe --dbpath d:testmongodbdata

If your path includes spaces, enclose the entire path in double quotations, for example:

C:mongodbbinmongod.exe --dbpath "d:testmongo db data"

Start MongoDB

To start MongoDB, execute from the Command Prompt:

C:mongodbbinmongod.exe

This will start the main MongoDB database process. The waiting for connections message in the console output indicates that the mongod.exe process is running successfully.

注解

Depending on the security level of your system, Windows will issue a Security Alert dialog box about blocking “some features” of C:mongodbbinmongod.exe from communicating on networks. All users should select Private Networks, such as my home or work network and click Allow access. For additional information on security and MongoDB, please read the 安全规范和管理 page.

警告

Do not allow mongod.exe to be accessible to public networks without running in “Secure Mode” (i.e. auth.) MongoDB is designed to be run in “trusted environments” and the database does not enable authentication or “Secure Mode” by default.

Connect to MongoDB using the mongo.exe shell. Open another Command Prompt and issue the following command:

C:mongodbbinmongo.exe

注解

Executing the command start C:mongodbbinmongo.exe will automatically start the mongo.exe shell in a separate Command Prompt window.

The mongo.exe shell will connect to mongod.exe running on the localhost interface and port 27017 by default. At the mongo.exe prompt, issue the following two commands to insert a record in the test collection of the default test database and then retrieve that record:

> db.test.save( { a: 1 } )
> db.test.find()

也可以参考

mongo” and “mongo Shell JavaScript 快参.” If you want to develop applications using .NET, see the documentation of C# and MongoDB for more information.

MongoDB as a Windows Service

2.0 新版功能.

Setup MongoDB as a Windows Service, so that the database will start automatically following each reboot cycle.

注解

mongod.exe added support for running as a Windows service in version 2.0, and mongos.exe added support for running as a Windows Service in version 2.1.1.

Configure the System

You should specify two options when running MongoDB as a Windows Service: a path for the log output (i.e. logpath) and a configuration file.

  1. Create a specific directory for MongoDB log files:

    md C:mongodblog
  2. Create a configuration file for the logpath option for MongoDB in the Command Prompt by issuing this command:

    echo logpath=C:mongodblogmongo.log > C:mongodbmongod.cfg

While these optional steps are optional, creating a specific location for log files and using the configuration file are good practice.

注解

Consider setting the logappend option. If you do not, mongod.exe will delete the contents of the existing log file when starting.

在 2.2 版更改: The default logpath and logappend behavior changed in the 2.2 release.

Install and Run the MongoDB Service

Run all of the following commands in Command Prompt with “Administrative Privileges:”

  1. To install the MongoDB service:

    C:mongodbbinmongod.exe --config C:mongodbmongod.cfg --install

    Modify the path to the mongod.cfg file as needed. For the --install option to succeed, you must specify a logpath setting or the --logpath run-time option.

  2. To run the MongoDB service:

    net start MongoDB
    

注解

If you wish to use an alternate path for your dbpath specify it in the config file (e.g. C:mongodbmongod.cfg) on that you specified in the --install operation. You may also specify --dbpath on the command line; however, always prefer the configuration file.

If the dbpath directory does not exist, mongod.exe will not be able to start. The default value for dbpath is datadb.

Stop or Remove the MongoDB Service

  • To stop the MongoDB service:

    net stop MongoDB
    
  • To remove the MongoDB service:

    C:mongodbbinmongod.exe --remove
最新网友评论  共有(0)条评论 发布评论 返回顶部

Copyright © 2007-2017 PHPERZ.COM All Rights Reserved   冀ICP备14009818号  版权声明  广告服务