Manage mongod Processes过程
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接口,相当于mongod。The 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进程
mongod ProcessesBy default, MongoDB listens for connections from clients on port 默认情况下,MongoDB在端口27017上侦听来自客户端的连接,并将数据存储在27017, and stores data in the /data/db directory./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以外的路径,则可以指定dbPath。The 启动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 pathmongod实例并将数据存储在/srv/mongodb路径中
mongod --dbpath /srv/mongodb/
mongod includes a Full Time Diagnostic Data Capture mechanism to assist MongoDB engineers with troubleshooting deployments. mongod包括一个全职诊断数据捕获机制,以帮助MongoDB工程师进行故障排除部署。If this thread fails, it terminates the originating process. 如果这个线程失败,它将终止发起进程。To avoid the most common failures, confirm that the user running the process has permissions to create the FTDC 为了避免最常见的故障,请确认运行进程的用户具有创建FTDC诊diagnostic.data directory. diagnostic.data目录的权限。For 对于mongod the directory is within storage.dbPath. mongod,目录位于storage.dbPath中。For 对于mongos it is parallel to systemLog.path.mongos,它与systemLog.path并行。
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 Daemon将mongod作为精灵启动
mongod as a DaemonTo 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 creates 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进程
mongod ProcessesIn 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:dbshutdownServer()方法从mongosh中关闭mongod,如下所示:
use admin
db.shutdownServer()
Calling the same method from a init script accomplishes the same result.从init脚本调用相同的方法可以获得相同的结果。
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.authorization的系统,用户只能在向admin数据库进行身份验证时,或者在未启用authorization的系统上通过localhost接口发出db.shutdownServer()。
Use 使用--shutdown
Supported on Linux only. From the command line, shut down the 仅在Linux上受支持。在命令行中,使用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.mongod实例(即不带--fork)时,发出Control-C以执行干净的关闭。
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和副本集
If a replica set primary receives a 如果复制集主服务器接收到SIGTERM, the primary attempts to step down before shutting down.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
Never use 永远不要使用kill -9 (i.e. SIGKILL) to terminate a mongod instance.kill-9(即SIGKILL)来终止mongod实例。
Troubleshoot mongod Processesmongod进程疑难解答
mongod ProcessesGenerate a Backtrace生成回溯
Starting in MongoDB 4.4 running on Linux:在Linux上运行的MongoDB 4.4中启动:
When the当mongodandmongosprocesses receive aSIGUSR2signal, backtrace details are added to the logs for each process thread.mongod和mongos进程接收到SIGUSR2信号时,会将回溯详细信息添加到每个进程线程的日志中。Backtrace details show the function calls for the process, which can be used for diagnostics and provided to MongoDB Support if required.回溯详细信息显示了流程的函数调用,可用于诊断,并在需要时提供给MongoDB支持。
The backtrace functionality is available for these architectures:回溯功能可用于以下体系结构:
x86_64arm64(starting in MongoDB 4.4.15, 5.0.10, and 6.0)
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实例的关闭过程包括以下步骤:
Check how up-to-date the secondaries are.检查secondary的最新情况。If no secondary is within 10 seconds of the primary,如果在主服务器的10秒内没有辅助服务器,mongodwill return a message that it will not shut down.mongod将返回一条不会关闭的消息。You can pass the您可以向shutdowncommand atimeoutSecsargument to wait for a secondary to catch up.shutdown命令传递一个timeoutSecs参数,以等待secondary命令赶上。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秒内有辅助设备,主设备将退出并等待辅助设备赶上。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参数的关闭命令,如以下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参数发出shutdown。MongoDB 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})