Manage mongod Processes管理mongod流程

On this page本页内容

MongoDB runs as a standard program. MongoDB作为标准程序运行。You can start MongoDB from a command line by issuing the mongod command and specifying options. 通过发出mongod命令并指定选项,可以从命令行启动MongoDB。For a list of options, see the mongod reference.有关选项列表,请参阅mongod参考。

The following examples assume the directory containing the mongod process is in your system paths. 以下示例假定包含mongod进程的目录位于系统路径中。The mongod process is the primary database process that runs on an individual server. mongod进程是在单个服务器上运行的主数据库进程。mongos provides a coherent MongoDB interface equivalent to a mongod from the perspective of a client. mongos提供了一个连贯的MongoDB接口,从客户端的角度来看,它相当于mongodThe mongosh binary provides the administrative shell.mongosh二进制文件提供了管理shell。

This document discusses the mongod process; however, some portions of this document may be applicable to mongos instances.本文件讨论了mongod过程;但是,本文档的某些部分可能适用于mongos实例。

Start mongod Processes启动mongod进程

By default, MongoDB listens for connections from clients on port 27017, and stores data in the /data/db directory.默认情况下,MongoDB在端口27017上侦听来自客户端的连接,并将数据存储在/data/db目录中。

On Windows, this path is on the drive from which you start MongoDB. 在Windows上,此路径位于启动MongoDB的驱动器上。For example, if you do not specify a --dbpath, starting a MongoDB server on the C:\ drive stores all data files in C:\data\db.例如,如果未指定--dbpath,则在C:\驱动器上启动MongoDB服务器会将所有数据文件存储在C:\data\db中。

To start MongoDB using all defaults, issue the following command at the system shell:要使用所有默认值启动MongoDB,请在系统外壳上发出以下命令:

mongod

Specify a Data Directory指定一个数据目录

If you want mongod to store data files at a path other than /data/db you can specify a dbPath. 如果希望mongod/data/db以外的路径存储数据文件,可以指定dbPathThe dbPath must exist before you start mongod. 在启动mongod之前,dbPath必须存在。If it does not exist, create the directory and the permissions so that mongod can read and write data to this path. 如果不存在,请创建目录和权限,以便mongod可以读取和写入此路径中的数据。For more information on permissions, see the security operations documentation.有关权限的更多信息,请参阅安全操作文档

To specify a dbPath for mongod to use as a data directory, use the --dbpath option. 要为mongod指定用作数据目录的dbPath,请使用--dbPath选项。The following invocation will start a mongod instance and store data in the /srv/mongodb path以下调用将启动mongod实例并将数据存储在/srv/mongodb路径中

mongod --dbpath /srv/mongodb/

Specify a TCP Port指定一个TCP端口

Only a single process can listen for connections on a network interface at a time. 一次只能有一个进程监听网络接口上的连接。If you run multiple mongod processes on a single machine, or have other processes that must use this port, you must assign each a different port to listen on for client connections.如果在一台机器上运行多个mongod进程,或者有其他进程必须使用此端口,则必须为每个进程分配一个不同的端口以侦听客户端连接。

To specify a port to mongod, use the --port option on the command line. 要指定mongod的端口,请使用命令行上的--port选项。The following command starts mongod listening on port 12345:以下命令在端口12345上启动mongod侦听:

mongod --port 12345

Use the default port number when possible, to avoid confusion.尽可能使用默认端口号,以避免混淆。

Start mongod as a Daemonmongod作为守护进程启动

To run a mongod process as a daemon (i.e. fork), and write its output to a log file, use the --fork and --logpath options. 要将mongod进程作为守护进程(即fork)运行,并将其输出写入日志文件,请使用--fork--logpath选项。You must create the log directory; however, mongod will create the log file if it does not exist.必须创建日志目录;但是,如果日志文件不存在,mongod将创建日志文件。

The following command starts mongod as a daemon and records log output to /var/log/mongodb/mongod.log.以下命令将mongod作为守护进程启动,并将日志输出记录到/var/log/mongodb/mongod.log

mongod --fork --logpath /var/log/mongodb/mongod.log

Additional Configuration Options其他配置选项

For an overview of common configurations and deployments for common use cases, see Run-time Database Configuration.有关常见用例的常见配置和部署的概述,请参阅运行时数据库配置

Stop mongod Processes停止mongod进程

In a clean shutdown a mongod completes all pending operations, flushes all data to data files, and closes all data files. 在完全关闭的情况下,mongod会完成所有挂起的操作,将所有数据刷新到数据文件中,并关闭所有数据文件。Other shutdowns are unclean and can compromise the validity of the data files.其他关闭是不干净的,可能会损害数据文件的有效性。

To ensure a clean shutdown, always shutdown mongod instances using one of the following methods:为确保干净关闭,请始终使用以下方法之一关闭mongod实例:

Use 使用shutdownServer()

Shut down the mongod from mongosh using the db.shutdownServer() method as follows:使用db.shutdownServer()方法从mongosh关闭mongod,如下所示:

use admin
db.shutdownServer()

Calling the same method from a init script accomplishes the same result.init script调用相同的方法可以实现相同的结果。

For systems with authorization enabled, users may only issue db.shutdownServer() when authenticated to the admin database or via the localhost interface on systems without authentication enabled.

Use --shutdown

Supported on Linux only. 仅在Linux上受支持。From the command line, shut down the mongod using the --shutdown option:在命令行中,使用--shutdown选项关闭mongod

mongod --shutdown

Use CTRL-C

When running the mongod instance in interactive mode (i.e. without --fork), issue Control-C to perform a clean shutdown.

Use kill

Supported on Linux and macOS only. 仅在Linux和macOS上受支持。From the command line, shut down a specific mongod instance using one of the following commands:在命令行中,使用以下命令之一关闭特定mongod实例:

kill <mongod process ID>
kill -2 <mongod process ID>

SIGTERM and Replica Sets

Starting in MongoDB 4.0.8 (and 3.6.15), if a replica set primary receives a SIGTERM, the primary attempts to step down before shutting down.从MongoDB 4.0.8(和3.6.15)开始,如果副本集主设备接收到SIGTERM,则主设备会在关闭前尝试退出。

  • If the step down succeeds, the instance does not vote in the ensuing election of the new primary, and continues its shutdown.如果下台成功,该实例将不会在随后的新初选中投票,并继续关闭。
  • If the step down fails, the instance continues its shutdown.如果退出失败,实例将继续关闭。

SIGKILL

Warning警告

Never use kill -9 (i.e. SIGKILL) to terminate a mongod instance.

Troubleshoot mongod Processesmongod进程故障排除

Generate a Backtrace生成回溯

Starting in MongoDB 4.4, mongod and mongos processes running on Linux will now log a backtrace for each of their running threads upon receipt of a SIGUSR2 signal. 从MongoDB 4.4开始,在Linux上运行的mongodmongos进程现在将在收到SIGUSR2信号后,为每个正在运行的线程记录回溯。This backtrace can be analyzed for diagnostic information or provided to MongoDB support as needed. 可以分析此回溯以获取诊断信息,或者根据需要提供给MongoDB支持。This functionality is currently available only on the x86_64 architecture.此功能目前仅在x86_64体系结构上可用。

To issue a SIGUSR2 signal to a running mongod process, use the following command:要向正在运行的mongod进程发出SIGUSR2信号,请使用以下命令:

kill -SIGUSR2 <mongod process ID>

The resulting backtrace data is written to the mongod logfile as configured with --logpath.生成的回溯数据被写入mongod日志文件,配置为--logpath

Stop a Replica Set停止复制集

Procedure程序

If the mongod is the primary in a replica set, the shutdown process for this mongod instance has the following steps:如果mongod副本集中的primary,则此mongod实例的关闭过程包括以下步骤:

  1. Check how up-to-date the secondaries are.检查secondaries的最新情况。
  2. If no secondary is within 10 seconds of the primary, mongod will return a message that it will not shut down. 如果主设备10秒内没有辅助设备,mongod将返回一条消息,表示它不会关闭。You can pass the shutdown command a timeoutSecs argument to wait for a secondary to catch up.您可以向shutdown命令传递一个timeoutSecs参数,以等待辅助服务器赶上。
  3. If there is a secondary within 10 seconds of the primary, the primary will step down and wait for the secondary to catch up.如果在主设备的10秒内有辅助设备,主设备将退出并等待辅助设备赶上。
  4. After 60 seconds or once the secondary has caught up, the primary will shut down.60秒后或辅助设备赶上后,主设备将关闭。

Force Replica Set Shutdown强制关闭复制集

If there is no up-to-date secondary and you want the primary to shut down, issue the shutdown command with the force argument, as in the following mongosh operation:如果没有最新的辅助设备,并且希望主设备关闭,请使用force参数发出shutdown命令,如以下mongosh操作所示:

db.adminCommand({shutdown : 1, force : true})

To keep checking the secondaries for a specified number of seconds if none are immediately up-to-date, issue shutdown with the timeoutSecs argument. 要在指定的秒数内持续检查辅助设备(如果没有任何设备是即时最新的),请使用timeoutSecs参数发出shutdownMongoDB will keep checking the secondaries for the specified number of seconds if none are immediately up-to-date. MongoDB将在指定的秒数内继续检查二级数据库,如果它们都不是最新的。If any of the secondaries catch up within the allotted time, the primary will shut down. 如果任何二级服务器在分配的时间内赶上,主服务器将关闭。If no secondaries catch up, it will not shut down.如果没有二级供应商迎头赶上,它也不会关闭。

The following command issues shutdown with timeoutSecs set to 5:以下命令在timeoutSecs设置为5时发出shutdown(关机):

db.adminCommand({shutdown : 1, timeoutSecs : 5})

Alternately you can use the timeoutSecs argument with the db.shutdownServer() method:或者,您可以将timeoutSecs参数与db.shutdownServer()方法一起使用:

db.shutdownServer({timeoutSecs : 5})
←  Upgrade to the Latest Revision of MongoDBTerminate Running Operations →