Docs HomeMongoDB Manual

Restart a Sharded Cluster重新启动分片群集

The tutorial is specific to MongoDB 6.0. For earlier versions of MongoDB, refer to the corresponding version of the MongoDB Manual.本教程专门针对MongoDB 6.0。有关MongoDB的早期版本,请参阅相应版本的MongoDB手册。

This procedure demonstrates the shutdown and startup sequence for restarting a sharded cluster. 此过程演示了重新启动分片集群的关闭和启动顺序。Stopping or starting the components of a sharded cluster in a different order may cause communication errors between members. 以不同的顺序停止或启动分片集群的组件可能会导致成员之间的通信错误。For example, shard servers may appear to hang if there are no config servers available.例如,如果没有可用的config服务器,则分片服务器可能会挂起。

Important

This procedure should only be performed during a planned maintenance period. 此程序只能在计划维护期间执行。During this period, applications should stop all reads and writes to the cluster in order to prevent potential data loss or reading stale data.在此期间,应用程序应停止对集群的所有读取和写入,以防止潜在的数据丢失或读取过时的数据。

Disable the Balancer禁用平衡器

Disable the balancer to stop chunk migration and do not perform any metadata write operations until the process finishes. 禁用平衡器以停止区块迁移,并且在进程完成之前不执行任何元数据写入操作。If a migration is in progress, the balancer will complete the in-progress migration before stopping.如果正在进行迁移,平衡器将在停止之前完成正在进行的迁移。

To disable the balancer, connect to one of the cluster's mongos instances and issue the following command: 要禁用平衡器,请连接到集群的一个mongos实例,然后发出以下命令:[1]

sh.stopBalancer()

To check the balancer state, issue the sh.getBalancerState() command.要检查平衡器状态,请发出sh.getBalancerState()命令。

For more information, see Disable the Balancer.有关详细信息,请参阅禁用平衡器

[1] Starting in MongoDB 6.1, automatic chunk splitting is not performed. 从MongoDB 6.1开始,不执行自动区块分割。This is because of balancing policy improvements. 这是因为平衡政策的改进。Auto-splitting commands still exist, but do not perform an operation. 自动拆分命令仍然存在,但不执行操作。For details, see Balancing Policy Changes.有关详细信息,请参阅平衡策略更改In MongoDB versions earlier than 6.1, sh.stopBalancer() also disables auto-splitting for the sharded cluster.在6.1之前的MongoDB版本中,sh.stopBalancer()还禁用了分片集群的自动拆分。

Stop Sharded Cluster停止分片群集

1

Stop mongos routers.停止mongos路由器。

Run db.shutdownServer() from the admin database on each mongos router:在每个mongos路由器上的管理数据库中运行db.shutdownServer()

use admin
db.shutdownServer()
2

Stop each shard replica set.停止每个分片复制集。

Run db.shutdownServer() from the admin database on each shard replica set member to shutdown its mongod process. Shutdown all secondary members before shutting down the primary in each replica set.在每个分片副本集成员的管理数据库中运行db.shutdownServer(),以关闭其mongod进程。在关闭每个复制集中的主成员之前,请先关闭所有辅助成员。

3

Stop config servers.停止配置服务器。

Run db.shutdownServer() from the admin database on each of the config servers to shutdown its mongod process. Shutdown all secondary members before shutting down the primary.从每个config服务器上的管理数据库运行db.shutdownServer()以关闭其mongod进程。在关闭主成员之前关闭所有辅助成员。

Start Sharded Cluster启动分片群集

1

Start config servers.启动配置服务器。

When starting each mongod, specify the mongod settings using either a configuration file or the command line. 启动每个mongod时,使用配置文件或命令行指定mongod设置。For more information on startup parameters, see the mongod reference page.有关启动参数的更多信息,请参阅mongod参考页面。

Configuration File配置文件

If using a configuration file, start the mongod with the --config option set to the configuration file path.如果使用配置文件,启动mongod时将--config选项设置为配置文件路径。

mongod --config <path-to-config-file>

Command Line命令行

If using the command line options, start the mongod with the --configsvr, --replSet, --bind_ip, and other options as appropriate to your deployment. 如果使用命令行选项,请使用--configsvr--replSet--bind_ip和其他适合您的部署的选项启动mongodFor example:例如:

mongod --configsvr --replSet <replica set name> --dbpath <path> --bind_ip localhost,<hostname(s)|ip address(es)>

After starting all config servers, connect to the primary mongod and run rs.status() to confirm the health and availability of each CSRS member.启动所有配置服务器后,连接到主mongod并运行rs.status()以确认每个CSRS成员的运行状况和可用性。

2

Start each shard replica set.启动每个分片复制集。

When starting each mongod, specify the mongod settings using either a configuration file or the command line.启动每个mongod时,使用配置文件或命令行指定mongod设置。

Configuration File配置文件

If using a configuration file, start the mongod with the --config option set to the configuration file path.如果使用配置文件,启动mongod时将--config选项设置为配置文件路径。

mongod --config <path-to-config-file>

Command Line命令行

If using the command line option, start the mongod with the --replSet, --shardsvr, and --bind_ip options, and other options as appropriate to your deployment. 如果使用命令行选项,请使用--replSet--shardsvr--bind_ip选项以及其他适合您的部署的选项启动mongodFor example:例如:

mongod --shardsvr --replSet <replSetname> --dbpath <path> --bind_ip localhost,<hostname(s)|ip address(es)>

After starting all members of each shard, connect to each primary mongod and run rs.status() to confirm the health and availability of each member.启动每个分片的所有成员后,连接到每个主mongod并运行rs.status()来确认每个成员的健康状况和可用性。

3

Start mongos routers.启动mongos路由器。

Start mongos routers using either a configuration file or a command line parameter to specify the config servers.使用配置文件或命令行参数启动mongos路由器以指定配置服务器。

Configuration File配置文件

If using a configuration file, start the mongos specifying the --config option and the path to the configuration file.如果使用配置文件,请启动mongos,指定--config选项和配置文件的路径。

mongos --config <path-to-config>

For more information on the configuration file, see configuration options.有关配置文件的更多信息,请参阅配置选项

Command Line命令行

If using command line parameters, start the mongos and specify the --configdb, --bind_ip, and other options as appropriate to your deployment. 如果使用命令行参数,请启动mongos,并根据部署情况指定--configdb--bind_ip和其他选项。For example:例如:

Warning

Before you bind your instance to a publicly-accessible IP address, you must secure your cluster from unauthorized access. 在将实例绑定到可公开访问的IP地址之前,必须保护群集不受未经授权的访问。For a complete list of security recommendations, see Security Checklist. 有关安全建议的完整列表,请参阅安全检查表At minimum, consider enabling authentication and hardening network infrastructure.至少,考虑启用身份验证强化网络基础设施

mongos --configdb <configReplSetName>/cfg1.example.net:27019,cfg2.example.net:27019 --bind_ip localhost,<hostname(s)|ip address(es)>

Include any other options as appropriate for your deployment.包括适用于您的部署的任何其他选项。

Re-Enable the Balancer重新启用平衡器

Re-enable the balancer to resume chunk migrations.重新启用平衡器以恢复区块迁移

Connect to one of the cluster's mongos instances and run the sh.startBalancer() command: 连接到集群的一个mongos实例并运行sh.startBalancer()命令:[2]

sh.startBalancer()

To check the balancer state, issue the sh.getBalancerState() command.要检查平衡器状态,请发出sh.getBalancerState()命令。

For more information, see Enable the Balancer.有关详细信息,请参阅启用平衡器

[2] Starting in MongoDB 6.1, automatic chunk splitting is not performed. 从MongoDB 6.1开始,不执行自动区块分割。This is because of balancing policy improvements. 这是因为平衡政策的改进。Auto-splitting commands still exist, but do not perform an operation. 自动拆分命令仍然存在,但不执行操作。For details, see Balancing Policy Changes.In MongoDB versions earlier than 6.1, sh.startBalancer() also enables auto-splitting for the sharded cluster.有关详细信息,请参阅平衡策略更改。在6.1之前的MongoDB版本中,sh.startBalancer()还为分片集群启用了自动拆分。

Validate Cluster Accessibility验证群集可访问性

Connect a mongo shell to one of the cluster's mongos processes. Use sh.status() to check the overall cluster status.将mongoshell连接到集群的一个mongos进程。使用sh.status()检查集群的整体状态。

To confirm that all shards are accessible and communicating, insert test data into a temporary sharded collection. 要确认所有分片都是可访问和可通信的,请将测试数据插入到临时分片集合中。Confirm that data is being split and migrated between each shard in your cluster. 确认数据正在集群中的每个分片之间进行拆分和迁移。You can connect a mongo shell to each shard primary and use db.collection.find() to validate that the data was sharded as expected.您可以将mongoshell连接到每个分片primary,并使用db.collection.find()验证数据是否按预期进行了分片。

Important

To prevent potential data loss or reading stale data, do not start application reads and writes to the cluster until after confirming the cluster is healthy and accessible.为了防止潜在的数据丢失或读取过时的数据,在确认集群正常且可访问之前,不要启动应用程序对集群的读写操作。