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.例如,如果没有配置服务器可用,分片服务器可能会挂起。
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 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] | sh.stopBalancer() also disables auto-splitting for the sharded cluster.sh.stopBalancer() 还禁用了分片集群的自动拆分。 |
mongos
routers.mongos
路由器。Run 从每个db.shutdownServer()
from the admin
database on each mongos
router:mongos
路由器上的admin
数据库运行db.shutdownServer()
:
use admin db.shutdownServer()
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.在关闭每个副本集中的主副本之前,请先关闭所有辅助成员。
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.关闭主成员之前,请关闭所有辅助成员。
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
和其他适合您的部署的选项启动mongod
。For 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成员的健康状况和可用性。
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
选项以及适合您的部署的其他选项启动mongod
。For 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()
以确认每个成员的健康状况和可用性。
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:例如
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 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] | sh.startBalancer() also enables auto-splitting for the sharded cluster.sh.startBalancer() 还支持自动分割分片集群。 |
Connect a 将mongo shell连接到集群的mongo
shell to one of the cluster's mongos
processes. 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()
验证数据是否按预期进行了分片。
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.为防止潜在的数据丢失或读取陈旧数据,请在确认集群正常且可访问之前,不要启动应用程序对集群的读取和写入。