如何安装,配置MongoDB

如何安装,配置MongoDB,第1张

nstall MongoDB on Windows

Overview

Use this tutorial to install MongoDB on a Windows systems.

PLATFORM SUPPORT

Starting in version 2.2, MongoDB does not support Windows XP. Please use a more recent version of Windows to use more recent releases of MongoDB.

IMPORTANT

If you are running any edition of Windows Server 2008 R2 or Windows 7, please install a hotfix to resolve an issue with memory mapped files on Windows.

Install MongoDB

1

Determine which MongoDB build you need.

There are three builds of MongoDB for Windows:

MongoDB for Windows Server 2008 R2 edition (i.e. 2008R2) runs only 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. 32-bit versions of MongoDB only support databases smaller than 2GB.

To find which version of Windows you are running, enter the following command in the Command Prompt:

wmic os get osarchitecture

2

Download MongoDB for Windows.

Download the latest production release of MongoDB from the MongoDB downloads page. Ensure you download the correct version of MongoDB for your Windows system. The 64-bit versions of MongoDB does not work with 32-bit Windows.

3

Install the downloaded file.

In Windows Explorer, locate the downloaded MongoDB msi file, which typically is located in the default Downloads folder. Double-click the msi file. A set of screens will appear to guide you through the installation process.

4

Move the MongoDB folder to another location (optional).

To move the MongoDB folder, you must issue the move command as an Administrator. For example, to move the folder to C:\mongodb:

Select Start Menu >All Programs >Accessories.

Right-click Command Prompt and select Run as Administrator from the popup menu.

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 folder (e.g. D:\test\mongodb)

Run MongoDB

WARNING

Do not make mongod.exe visible on public networks without running in “Secure Mode” with the authsetting. MongoDB is designed to be run in trusted environments, and the database does not enable “Secure Mode” by default.

1

Set up the MongoDB environment.

MongoDB requires a data directory to store all data. MongoDB’s default data directory path is\data\db. Create this folder using the following commands from a Command Prompt:

md \data\db

You can specify an alternate path for data files using the --dbpath option to mongod.exe, for example:

C:\mongodb\bin\mongod.exe --dbpath d:\test\mongodb\data

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

C:\mongodb\bin\mongod.exe --dbpath "d:\test\mongo db data"

2

Start MongoDB.

To start MongoDB, run mongod.exe. For example, from the Command Prompt:

C:\Program Files\MongoDB\bin\mongod.exe

This starts 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 may pop up a Security Alert dialog box about blocking “some features” of C:\Program Files\MongoDB\bin\mongod.exe from communicating on networks. All users should select Private Networks, such as my homeor work network and click Allow access. For additional information on security and MongoDB, please see the Security Documentation.

3

Connect to MongoDB.

To connect to MongoDB through the mongo.exe shell, open another Command Prompt. When connecting, specify the data directory if necessary. This step provides several example connection commands.

If your MongoDB installation uses the default data directory, connect without specifying the data directory:

C:\mongodb\bin\mongo.exe

If you installation uses a different data directory, specify the directory when connecting, as in this example:

C:\mongodb\bin\mongod.exe --dbpath d:\test\mongodb\data

If your path includes spaces, enclose the entire path in double quotes. For example:

C:\mongodb\bin\mongod.exe --dbpath "d:\test\mongo db data"

If you want to develop applications using .NET, see the documentation of C# and MongoDB for more information.

4

Begin using MongoDB.

To begin using MongoDB, see Getting Started with MongoDB. Also consider the Production Notesdocument before deploying MongoDB in a production environment.

Later, to stop MongoDB, press Control+C in the terminal where the mongod instance is running.

Configure a Windows Service for MongoDB

NOTE

There is a known issue for MongoDB 2.6.0, SERVER-13515, which prevents the use of the instructions in this section. For MongoDB 2.6.0, use Manually Create a Windows Service for MongoDB to create a Windows Service for MongoDB instead.

1

Configure directories and files.

Create a configuration file and a directory path for MongoDB log output (logpath):

Create a specific directory for MongoDB log files:

md "C:\Program Files\MongoDB\log"

In the Command Prompt, create a configuration file for the logpath option for MongoDB:

echo logpath=C:\Program Files\MongoDB\log\mongo.log >"C:\Program Files\MongoDB\mongod.cfg"

2

Run the MongoDB service.

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

Install the MongoDB service. For --install to succeed, you must specify the logpath run-time option.

"C:\Program Files\MongoDB\bin\mongod.exe" --config "C:\Program Files\MongoDB\mongod.cfg" --install

Modify the path to the mongod.cfg file as needed.

To use an alternate dbpath, specify the path in the configuration file (e.g. C:\ProgramFiles\MongoDB\mongod.cfg) or on the command line with the --dbpath option.

If the dbpath directory does not exist, mongod.exe will not start. The default value for dbpath is\data\db.

If needed, you can install services for multiple instances of mongod.exe or mongos.exe. Install each service with a unique --serviceName and --serviceDisplayName. Use multiple instances only when sufficient system resources exist and your system design requires it.

3

Stop or remove the MongoDB service as needed.

To stop the MongoDB service use the following command:

net stop MongoDB

To remove the MongoDB service use the following command:

"C:\Program Files\MongoDB\bin\mongod.exe" --remove

Manually Create a Windows Service for MongoDB

The following procedure assumes you have installed MongoDB using the MSI installer, with the default path C:\Program Files\MongoDB 2.6 Standard.

If you have installed in an alternative directory, you will need to adjust the paths as appropriate.

1

Open an Administrator command prompt.

Windows 7 / Vista / Server 2008 (and R2)

Press Win + R, then type cmd, then press Ctrl + Shift + Enter.

Windows 8

Press Win + X, then press A.

Execute the remaining steps from the Administrator command prompt.

2

Create directories.

Create directories for your database and log files:

mkdir c:\data\db

mkdir c:\data\log

3

Create a configuration file.

Create a configuration file. This file can include any of the configuration options for mongod, butmust include a valid setting for logpath:

The following creates a configuration file, specifying both the logpath and the dbpath settings in the configuration file:

echo logpath=c:\data\log\mongod.log>"C:\Program Files\MongoDB 2.6 Standard\mongod.cfg"

echo dbpath=c:\data\db>>"C:\Program Files\MongoDB 2.6 Standard\mongod.cfg"

4

Create the MongoDB service.

Create the MongoDB service.

sc.exe create MongoDB binPath= "\"C:\Program Files\MongoDB 2.6 Standard\bin\mongod.exe\" --service --config=\"C:\Program Files\MongoDB 2.6 Standard\mongod.cfg\"" DisplayName= "MongoDB 2.6 Standard" start= "auto"

sc.exe requires a space between “=” and the configuration values (eg “binPath= ”), and a “” to escape double quotes.

If successfully created, the following log message will display:

[SC] CreateService SUCCESS

5

Start the MongoDB service.

net start MongoDB

6

Stop or remove the MongoDB service as needed.

To stop the MongoDB service, use the following command:

net stop MongoDB

To remove the MongoDB service, first stop the service and then run the following command:

sc.exe delete MongoDB

方法/步骤

1

打开百度首页,搜索“MongoDB”,如下图所示

2

网站主页中,点击“DOWNLOAD MONGODB”,并下载最新稳定版本,如下图所示:

3

下载下来的是一个msi安装文件,点击进行安装,默认安装到C:\Program File或C:\Program Files (x86)文件夹内,如下图所示:

4

配置环境变量,右键“我的电脑”->"高级系统设置"->"高级"->"环境变量",在系统变量中找到Path变量值,进行修改,在末尾追加 C:\Program Files (x86)\MongoDB\bin,(当然你也可以将第3步骤中的MongoDB文件夹拷贝到其他磁盘路径下,我就是这样做的),如下图所示:

5

为了验证环境变量配置成功,打开CMD命令提示符,输入 mongod -help,下面会罗列一些列的配置参数,说明已成功,如下图所示:

6

在MongoDB文件夹下和bin目录同级目录下,新建一个data文件夹,data文件夹下再分别新建一个db和log文件夹,分别用来存放数据文件和日志文件,如下图所示:

7

在命令行中输入以下内容

mongod --dbpath "F:\MongoDB\data\db" --logpath

"F:\MongoDB\data\log\MongoDB.log" --install --serviceName "MongoDB"

这里是日志路径和数据路径,具体的路径根据自己数据和日志文件的文件路径相应进行修改,这里MongoDB.log就是开始建立的日志文件,--serviceName "MongoDB" 服务名为MongoDB。这样就在Windows下成功建立了MongoDB的服务,如下图所示:

8

在“运行”中输入“services.msc”,打开“服务”管理界面,可以看到MongoDB这个服务,点击列表中的“MongoDB”,再点击“启动”按钮,就将MongoDB成功启动了

1.首先安装mongodb

2.解压缩到自己想要安装的目录,比如d:\mongodb

3.创建文件夹d:\mongodb\data\db、d:\mongodb\data\log,分别用来安装db和日志文件,在log文件夹下创建一个日志文件MongoDB.log,即d:\mongodb\data\log\MongoDB.log

4.运行cmd.exe进入dos命令界面,执行下列命令

>cd d:\mongodb\bin

>d:\mongodb\bin>mongod -dbpath "d:\mongodb\data\db"

 看到类似的信息,则说明启动成功,默认MongoDB监听的端口是27017,mysql的是3306

5.测试连接

 新开一个cmd窗口,进入mongodb的bin目录,输入mongo或者mongo.exe,出现如下信息说明测试通过,此时我们已经进入了test这个数据库,如何进入其他数据库下面会说。

 

 输入exit或者ctrl+C可退出。

6.当mongod.exe被关闭时,mongo.exe 就无法连接到数据库了,因此每次想使用mongodb数据库都要开启mongod.exe程序,所以比较麻烦,此时我们可以将MongoDB安装为windows服务

 还是运行cmd,进入bin文件夹,执行下列命令

 >d:\mongodb\bin>mongod --dbpath "d:\mongodb\data\db" --logpath "d:\mongodb\data\log\MongoDB.log" --install --serviceName "MongoDB"

 这里MongoDB.log就是开始建立的日志文件,--serviceName "MongoDB" 服务名为MongoDB

 接着启动mongodb服务

 >d:\mongodb\bin>NET START MongoDB

 

 打开任务管理器,可以看到进程已经启动

7.关闭服务和删除进程

 >d:\mongodb\bin>NET stop MongoDB (关闭服务)

 >d:\mongodb\bin>mongod --dbpath "d:\mongodb\data\db" --logpath "d:\mongodb\data\log\MongoDB.log" --remove --serviceName "MongoDB" (删除,注意不是--install了)


欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/sjk/10073108.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2023-05-05
下一篇2023-05-05

发表评论

登录后才能评论

评论列表(0条)

    保存