MongoDB Enterprise 和 MongoDB Community 可以在 Windows 上作为 Windows 服务运行,通过 net 命令、mongod.exe 命令行界面或手动通过服务控制台进行托管。
语法
net <command> MongoDB
在“服务”控制台中右键单击 MongoDB 服务,然后选择要执行的操作。
"C:\Program Files\MongoDB\Server\9.0\bin\mongod.exe" <opts>
兼容性
在安装于 Windows 上的 MongoDB Enterprise 和 MongoDB Community 自管理部署中,您可以将 mongod 进程作为 Windows 服务进行管理。
有关支持平台的更多信息,请参阅:
行为
用户权限
大多数管理 Windows 服务的命令都需要提升权限。在运行这些命令之前,请以 Administrator 的身份打开 Windows 命令提示符 (cmd.exe)。
默认情况下,该服务在本地系统帐户的上下文中运行。要以特定用户身份运行该服务,请使用 --serviceUser 和 --servicePassword 选项对其进行注册。该用户必须拥有 Log on as a service 权限。
数据目录
在启动 MongoDB 进程之前,请确保数据目录存在。要创建默认的 \data\db 数据目录,请从 Command Interpreter 运行以下命令:
cd C:\ md "\data\db"
默认数据目录路径是启动 MongoDB 的驱动器上的绝对路径 \data\db。
配置文件
您可以通过命令行选项或配置文件来配置 MongoDB 进程。如果您计划长时间运行 MongoDB 或需要它始终以特定选项运行,请使用配置文件。
如果您使用 Windows 安装程序安装了 MongoDB,安装程序会为您在 C:\Program Files\MongoDB\Server\9.0\bin\mongod.cfg 处创建一个配置文件。
如果您是从 ZIP 存档安装的 MongoDB,请自行创建该文件。示例:
storage: dbPath: C:\data\db systemLog: destination: file path: C:\data\log.txt logAppend: true net: port: 27017 bindIp: 127.0.0.1
运行该服务的用户必须具有对配置文件的读取权限,以及对 dbPath 和 path 目录的读取和写入权限。
重要
将 bindIp 设置为 127.0.0.1 以限制与本地机器的连接。要接受远程连接,请将 bindIp 设置为私有或内部网络接口。
在绑定到可公开访问的地址之前,请确保部署安全。要了解更多信息,请参阅 自管理部署的安全检查清单。
服务注册
Windows 安装程序会为您注册 MongoDB 服务,安装成功后该服务会自动启动。系统启动时,该服务也会随之启动。
如果您是从 ZIP 存档安装的 MongoDB,则必须先使用 --install 选项自行注册服务,然后才能使用 net 命令对其进行管理。
示例
注册 MongoDB 服务
要将 mongod.exe 注册为 Windows 服务,请使用 --install 选项运行 mongod.exe。传递 --config 选项以使用它在启动时读取的配置文件注册服务:
"C:\Program Files\MongoDB\Server\9.0\bin\mongod.exe" --config "C:\Program Files\MongoDB\Server\9.0\bin\mongod.cfg" --install
该服务会存储您注册它时所使用的选项。当您稍后使用 net start MongoDB 启动服务时,它会读取您在此处指定的配置文件。
您只需要注册一次该服务。Windows 安装程序会为您执行此步骤。
启动 MongoDB。
要启动 MongoDB 服务,请使用以下方法之一:
net start MongoDB
在“服务”控制台中右键单击 MongoDB 服务,然后单击Start。
"C:\Program Files\MongoDB\Server\9.0\bin\mongod.exe" --dbpath="C:\data\db"
此命令在后台运行 mongod.exe。--fork 选项在 Windows 系统上不受支持。
验证 MongoDB 是否正在运行
如果您将 MongoDB 作为 Windows 服务启动,请从命令提示符查询该服务:
sc query MongoDB
该命令返回服务详细信息。当服务运行时,STATE 字段显示为 RUNNING;当服务未运行时,显示为 STOPPED。
如果您将 MongoDB 作为 Windows 服务启动,请在“服务”控制台中找到 MongoDB 服务。您应该会看到该服务在 Status 列中列为 Running。
如果您手动在前台启动 MongoDB,当数据库服务器正常运行时,Command Interpreter 会显示以下行:
[initandlisten] waiting for connections
To show runtime information for the running mongod.exe process, use mongosh to run the serverStatus command:
mongosh --quiet --eval "db.runCommand( { serverStatus: 1, none: 1, mem: 1 } )"
{ host: 'example', version: '9.0.0', process: 'mongod', pid: Long('71115'), uptime: 61, uptimeMillis: Long('61213'), uptimeEstimate: Long('61'), localTime: ISODate('2026-10-02T16:57:13.762Z'), ok: 1, '$clusterTime': { clusterTime: Timestamp({ t: 1790960233, i: 1 }), signature: { hash: Binary.createFromBase64('AAAAAAAAAAAAAAAAAAAAAAAAAAA=', 0), keyId: Long('0') } }, operationTime: Timestamp({ t: 1790960233, i: 1 }) }
您还可以查看日志文件以了解 mongod.exe 进程的当前状态。日志文件是您使用 --logpath 选项设置的路径。
停止 MongoDB
要停止 MongoDB 服务,请使用以下方法之一:
net stop MongoDB
在“服务”控制台中右键单击 MongoDB 服务,然后单击Stop。
Connect to the server with mongosh and run the db.shutdownServer() method on the admin database:
mongosh admin --quiet --eval "db.shutdownServer()"
--shutdown 选项仅适用于 Linux 系统。如果您在前台启动了 mongod.exe,也可以在 Command Interpreter 中按 Ctrl + C 来干净地关闭该进程。
重启 MongoDB
要重新启动 MongoDB 服务,请使用以下方法之一:
net stop MongoDB net start MongoDB
在“服务”控制台中右键单击 MongoDB 服务,然后单击Restart。
Connect to the server with mongosh and run the db.shutdownServer() method on the admin database. Then, use mongod.exe to start the server again:
mongosh admin --quiet --eval "db.shutdownServer()" "C:\Program Files\MongoDB\Server\9.0\bin\mongod.exe" --dbpath="C:\data\db"
删除 MongoDB 服务
To remove the MongoDB service, first stop the service, then run the following command:
sc delete MongoDB