翻译或纠错本页面

管理 mongod 进程

MongoDB是作为一个标准程序来运行的。您可以从命令行发送 mongod 命令并指定选项来启动MongoDB。 具体选项会在 mongod 里列出。MongoDB也能作为Windows服务来运行。具体细节,请见 Configure a Windows Service for MongoDB Community Edition 。安装MongoDB,请见 安装MongoDB

The following examples assume the directory containing the mongod process is in your system paths. The mongod process is the primary database process that runs on an individual server. mongos provides a coherent MongoDB interface equivalent to a mongod from the perspective of a client. The mongo binary provides the administrative shell.

接下来的例子会假设包含 mongod 进程的目录在您的系统路径下。 mongod 进程是运行在单独的服务器上的主要数据库进程。 mongos 提供了一个一致性的MongoDB接口,就如同透过一个客户端来看 mongod 一样。二进制程序 mongo 则提供了用了管理的shell。

开启``mongod``进程

默认情况下,MongoDB将数据存储在 /data/db 目录里。在Windows里,MongoDB将数据存储在 C:\data\db 里。在所有平台上,MongoDB对来自 27017 端口上的客户端连接进行监听。

On Windows, this path is on the drive from which you start 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.

全部使用默认配置来启动MongoDB,只需要在系统shell里发出以下命令即可:

mongod

指定数据目录

If you want mongod to store data files at a path other than /data/db you can specify a dbPath. The dbPath must exist before you start mongod. If it does not exist, create the directory and the permissions so that mongod can read and write data to this path. For more information on permissions, see the security operations documentation.

可以使用 --dbpath 选项,为 mongod 指定一个 dbPath 来当做数据目录使用。下面的调用将会启动一个 mongod 实例并将数据存储在 /srv/mongodb 路径下。

mongod --dbpath /srv/mongodb/

指定TCP端口

在只有一个进程的时候,可以在一个网络接口上监听所有连接。如果您在一台服务器上运行多个 mongod 进程,或者有其他一些进程必须使用这个端口,那么您必须分配不同的端口去监听每一个客户端的连接。

mongod 指定一个端口,请在命令行使用 --port 选项。下面的命令将启动 mongod 并在``12345``: 端口监听它。

mongod --port 12345

为了避免混淆,请尽可能的使用默认的端口号。

将 mongodb 以守护进程的方式启动

要运行一个 mongod 进程来作为一个守护进程(也就是 fork),并且 将输出写入到一个日志文件里,可以使用 --fork--logpath 选项。您必须创建一个log目录;可是,如果这个日志文件不存在的话,mongod 会代劳。

The following command starts mongod as a daemon and records log output to /var/log/mongodb.log.

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

附加的配置选项

For an overview of common configurations and deployments for common use cases, see Run-time Database Configuration.

停止 mongod 进程

In a clean shutdown a mongod completes all pending operations, flushes all data to data files, and closes all data files. Other shutdowns are unclean and can compromise the validity of the data files.

为了确保正常关闭实例,通常会用下列方法的一种来关闭 mongod 实例。

使用 shutdownServer()

mongo shell中使用 db.shutdownServer() 方法来关闭 mongod ,如下所示:

use admin
db.shutdownServer()

对于启用 authorization 的系统,通过验证登陆到 admin 数据库,或者在为开启验证的情况下经由本地主机的系统接口登陆到 admin 数据库时,用户可以仅仅通过发送 db.shutdownServer() 来关闭实例。

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.

使用 --shutdown

从Linux命令行关闭 mongod ,可以使用 --shutdown 选项 如下所示:

mongod --shutdown

使用 CTRL-C

当正在运行一个交互模式 (i.e. without --fork) 的 mongod 实例时,发送 Control-C 来进行正常关闭。

使用 kill

From the Linux command line, shut down a specific mongod instance using one of the following commands:

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

警告

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

停止一个复制集

步骤

If the mongod is the primary in a replica set, the shutdown process for this mongod instance has the following steps:

  1. 检查 secondaries 的oplog时间戳。

  2. 如果从节点的时间戳落后于主节点10秒, mongod 将会返回节点不会被关闭的消息。您可以传递一个 timeoutSecs 参数给 shutdown 命令来等待从节点追上主节点。

  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.
  4. 一旦从节点追上进度或者60秒之后,主节点将会关闭。

强制关闭复制集

如果从节点没有最新的数据并且您想关闭主节点,发送 shutdown 命令加上 force 参数就像下面 mongo shell里的操作一样:

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

如果没有节点能立刻更新到最新的数据,发送 shutdown 加上 timeoutSecs 参数来在指定的时间(秒)内保持对从节点的检查。如果在分配的时间内有任意一个从节点追上,主节点将会关闭。反之,主节点将不会关闭。

The following command issues shutdown with timeoutSecs set to 5:

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

Alternately you can use the timeoutSecs argument with the db.shutdownServer() method:

db.shutdownServer({timeoutSecs : 5})