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

On this page本页内容

The tutorial is specific to MongoDB 5.0. 本教程特定于MongoDB 5.0。For earlier versions of MongoDB, refer to the corresponding version of the MongoDB Manual.有关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.例如,如果没有配置服务器可用,分片服务器可能会挂起。

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 4.2, sh.stopBalancer() also disables auto-splitting for the sharded cluster.从MongoDB 4.2开始,sh.stopBalancer()还禁用了分片集群的自动拆分。

Stop Sharded Cluster停止分片群集

1

Stop mongos routers.停止mongos路由器。

Run db.shutdownServer() from the admin database on each mongos router:从每个mongos路由器上的admin数据库运行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. 从每个分片副本集成员的admin数据库运行db.shutdownServer(),以关闭其mongod进程。Shutdown all secondary members before shutting down the primary in each replica set.在关闭每个副本集中的主副本之前,请先关闭所有辅助成员。

3

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

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

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 binding to a non-localhost (e.g. publicly accessible) IP address, ensure you have secured 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 4.2, sh.startBalancer() also enables auto-splitting for the sharded cluster.从MongoDB 4.2开始,sh.startBalancer()还支持自动分割分片集群。

Validate Cluster Accessibility验证群集可访问性

Connect a mongo shell to one of the cluster's mongos processes. 将mongo shell连接到集群的mongos进程之一。Use sh.status() to check the overall cluster status.使用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.您可以将mongo shell连接到每个分片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.为防止潜在的数据丢失或读取陈旧数据,请在确认集群正常且可访问之前,不要启动应用程序对集群的读取和写入。

←  View Cluster ConfigurationMigrate a Sharded Cluster to Different Hardware →