Overview概述
Enforcing access control on a sharded cluster requires configuring:在分片集群上实施访问控制需要配置:
Security between components of the cluster using Internal Authentication.使用内部身份验证的群集组件之间的安全性。Security between connecting clients and the cluster using Role-Based Access Control in Self-Managed Deployments.在自我管理部署中使用基于角色的访问控制连接客户端和集群之间的安全性。
For this tutorial, each member of the sharded cluster must use the same internal authentication mechanism and settings. 对于本教程,分片集群的每个成员都必须使用相同的内部身份验证机制和设置。This means enforcing internal authentication on each 这意味着对集群中的每个mongos and mongod in the cluster.mongos和mongod强制执行内部身份验证。
The following tutorial uses a keyfile to enable internal authentication.以下教程使用键文件启用内部身份验证。
Enforcing internal authentication also enforces user access control. To connect to the replica set, clients like 强制内部身份验证也会强制用户访问控制。要连接到副本集,像mongosh need to use a user account. mongosh这样的客户端需要使用用户帐户。See Access Control.请参阅访问控制。
CloudManager and 和OpsManager
If Cloud Manager or Ops Manager is managing your deployment, internal authentication is automatically enforced.如果Cloud Manager或Ops Manager正在管理部署,则会自动强制执行内部身份验证。
To configure Access Control on a managed deployment, see: 要在托管部署上配置访问控制,请参阅Cloud Manager手册或Ops Manager手册中的:为MongoDB部署配置访问控制。Configure Access Control for MongoDB Deployments in the Cloud Manager manual or in the Ops Manager manual.
Considerations注意事项
Important
To avoid configuration updates due to IP address changes, use DNS hostnames instead of IP addresses. It is particularly important to use a DNS hostname instead of an IP address when configuring replica set members or sharded cluster members.为避免因IP地址更改而进行配置更新,请使用DNS主机名而不是IP地址。在配置副本集成员或分片集群成员时,使用DNS主机名而不是IP地址尤为重要。
Use hostnames instead of IP addresses to configure clusters across a split network horizon. Starting in MongoDB 5.0, nodes that are only configured with an IP address fail startup validation and do not start.使用主机名而不是IP地址来配置跨拆分网络范围的集群。从MongoDB 5.0开始,仅配置了IP地址的节点无法启动验证,也无法启动。
IP BindingIP绑定
MongoDB binaries, MongoDB二进制文件mongod and mongos, bind to localhost by default.mongod和mongos默认绑定到localhost。
Operating System操作系统
This tutorial primarily refers to the 本教程主要介绍mongod process. Windows users should use the mongod.exe program instead.mongod过程。Windows用户应该使用mongod.exe程序。
Keyfile Security键文件安全
Keyfiles are bare-minimum forms of security and are best suited for testing or development environments. For production environments we recommend using X.509 certificates.键文件是最低限度的安全形式,最适合测试或开发环境。对于生产环境,我们建议使用X.509证书。
Access Control访问控制
This tutorial covers creating the minimum number of administrative users on the 本教程仅介绍在admin database only. admin数据库上创建最小数量的管理用户。For the user authentication, the tutorial uses the default SCRAM authentication mechanism. Challenge-response security mechanisms are best suited for testing or development environments. 对于用户身份验证,本教程使用默认的SCRAM身份验证机制。挑战响应安全机制最适合测试或开发环境。For production environments, we recommend using X.509 certificates or Self-Managed LDAP Proxy Authentication (available for MongoDB Enterprise only) or Kerberos Authentication on Self-Managed Deployments (available for MongoDB Enterprise only).对于生产环境,我们建议在自我管理部署上使用X.509证书或自我管理LDAP代理身份验证(仅适用于MongoDB Enterprise)或Kerberos身份验证(仅供MongoDB Enterprise)。
For details on creating users for specific authentication mechanism, refer to the specific authentication mechanism pages.有关为特定身份验证机制创建用户的详细信息,请参阅特定的身份验证机制页面。
See ➤ Configure Role-Based Access Control for best practices for user creation and management.有关用户创建和管理的最佳实践,请参阅➤ 配置基于角色的访问控制。
Users用户
In general, to create users for a sharded clusters, connect to the 一般来说,要为分片集群创建用户,请连接到mongos and add the sharded cluster users.mongos并添加分片集群用户。
However, some maintenance operations require direct connections to specific shards in a sharded cluster. To perform these operations, you must connect directly to the shard and authenticate as a shard-local administrative user.然而,一些维护操作需要直接连接到分片集群中的特定分片。要执行这些操作,您必须直接连接到分片,并作为分片本地管理用户进行身份验证。
Shard-local users exist only in the specific shard and should only be used for shard-specific maintenance and configuration. You cannot connect to the 分片本地用户只存在于特定的分片中,只应用于分片特定的维护和配置。您无法使用分片本地用户连接到mongos with shard-local users.mongos。
See the Users in Self-Managed Deployments security documentation for more information.有关更多信息,请参阅自我管理部署中的用户安全文档。
Downtime停机时间
Upgrading a sharded cluster to enforce access control requires downtime.升级分片集群以实施访问控制需要停机。
Before You Begin开始之前
Starting in MongoDB 8.0, you can use the 从MongoDB 8.0开始,您可以使用directShardOperations role to perform maintenance operations that require you to execute commands directly against a shard.directShardOperations角色执行维护操作,这些操作要求您直接对分片执行命令。
Warning
Running commands using the 使用directShardOperations role can cause your cluster to stop working correctly and may cause data corruption. directShardOperations角色运行命令可能会导致集群停止正常工作,并可能导致数据损坏。Only use the 仅将directShardOperations role for maintenance purposes or under the guidance of MongoDB support. directShardOperations角色用于维护目的或在MongoDB支持的指导下使用。Once you are done performing maintenance operations, stop using the 完成维护操作后,停止使用directShardOperations role.directShardOperations角色。
Procedures过程
Enforce Keyfile Internal Authentication on Existing Sharded Cluster Deployment在现有分片群集部署上强制键文件内部身份验证
Create a keyfile.创建键文件。
With keyfile authentication, each 通过键文件身份验证,分片集群中的每个mongod or mongos instances in the sharded cluster uses the contents of the keyfile as the shared password for authenticating other members in the deployment. mongod或mongos实例都使用键文件的内容作为共享密码,用于对部署中的其他成员进行身份验证。Only 只有具有正确键文件的mongod or mongos instances with the correct keyfile can join the sharded cluster.mongod或mongos实例才能加入分片集群。
Note
Keyfiles for internal membership authentication用于内部成员身份验证的键文件 use YAML format to allow for multiple keys in a keyfile. The YAML format accepts either:使用YAML格式允许键文件中有多个键。YAML格式接受以下任一格式:
A single key string (same as in earlier versions)单个键字符串(与早期版本相同)A sequence of key strings一串键串
The YAML format is compatible with the existing single-key keyfiles that use the text file format.YAML格式与使用文本文件格式的现有单键键文件兼容。
A key's length must be between 6 and 1024 characters and may only contain characters in the base64 set. All members of the sharded cluster must share at least one common key.键的长度必须在6到1024个字符之间,并且只能包含base64集合中的字符。分片集群的所有成员必须至少共享一个公共键。
Note
On UNIX systems, the keyfile must not have group or world permissions. On Windows systems, keyfile permissions are not checked.在UNIX系统上,键文件不得具有组或世界权限。在Windows系统上,不检查键文件权限。
You can generate a keyfile using any method you choose. For example, the following operation uses 您可以使用您选择的任何方法生成键文件。例如,以下操作使用openssl to generate a complex pseudo-random 1024 character string to use as a shared password. openssl生成一个复杂的伪随机1024字符串,用作共享密码。It then uses 然后,它使用chmod to change file permissions to provide read permissions for the file owner only:chmod更改文件权限,仅为文件所有者提供读取权限:
openssl rand -base64 756 > <path-to-keyfile>
chmod 400 <path-to-keyfile>
See Keyfiles for additional details and requirements for using keyfiles.有关使用键文件的其他详细信息和要求,请参阅键文件。
Copy the keyfile to each component in the sharded cluster.将键文件复制到分片集群中的每个组件。
Every server hosting a 托管分片集群的mongod or mongos component of the sharded cluster must contain a copy of the keyfile.mongod或mongos组件的每台服务器都必须包含键文件的副本。
Copy the keyfile to each server hosting the sharded cluster members. 将键文件复制到托管分片集群成员的每个服务器。Ensure that the user running the 确保运行mongod or mongos instances is the owner of the file and can access the keyfile.mongod或mongos实例的用户是文件的所有者,并且可以访问键文件。
Avoid storing the keyfile on storage mediums that can be easily disconnected from the hardware hosting the 避免将键文件存储在容易与托管mongod or mongos instances, such as a USB drive or a network attached storage device.mongod或mongos实例的硬件断开连接的存储介质上,例如USB驱动器或网络连接的存储设备。
Disable the Balancer.禁用平衡器。
Connect 把mongosh to a mongos.mongosh和mongos联系起来。
sh.stopBalancer()
The balancer may not stop immediately if a migration is in progress. The 如果正在进行迁移,平衡器可能不会立即停止。sh.stopBalancer() method blocks the shell until the balancer stops.sh.stopBalancer()方法会阻塞shell,直到平衡器停止。
Starting in MongoDB 6.0.3, automatic chunk splitting is not performed. This is because of balancing policy improvements. Auto-splitting commands still exist, but do not perform an operation.从MongoDB 6.0.3开始,不执行自动块分割。这是因为平衡了政策的改进。自动拆分命令仍然存在,但不执行操作。
In MongoDB versions earlier than 6.0.3, 在早于6.0.3的MongoDB版本中,sh.stopBalancer() also disables auto-splitting for the sharded cluster.sh.stopBalancer()还禁用了分片集群的自动拆分。
Use 使用sh.getBalancerState() to verify that the balancer has stopped.sh.getBalancerState()验证平衡器是否已停止。
sh.getBalancerState()
Important
Do not proceed until the balancer has stopped running.在平衡器停止运行之前,不要继续。
See Manage Sharded Cluster Balancer for tutorials on configuring sharded cluster balancer behavior.有关配置分片集群平衡器行为的教程,请参阅管理分片集群均衡器。
Shut down all mongos instances for the sharded cluster.关闭分片集群的所有mongos实例。
mongos instances for the sharded cluster.Connect 将mongosh to each mongos and shut them down.mongosh连接到每个mongos并关闭它们。
Use the 在db.shutdownServer() method on the admin database to safely shut down the mongos:admin数据库上使用db.shutdownServer()方法安全关闭mongos:
db.getSiblingDB("admin").shutdownServer()
Repeat until all 重复此操作,直到集群中的所有mongos instances in the cluster are offline.mongos实例都脱机。
Once this step is complete, all 完成此步骤后,集群中的所有mongos instances in the cluster should be offline.mongos实例都应该脱机。
Shut down config server mongod instances.关闭配置服务器mongod实例。
mongod instances.Connect 将mongosh to each mongod in the config server deployment and shut them down.mongosh连接到配置服务器部署中的每个mongod并关闭它们。
For replica set config server deployments, shut down the primary member last.对于副本集配置服务器部署,请最后关闭primary成员。
Use the 在db.shutdownServer() method on the admin database to safely shut down the mongod:admin数据库上使用db.shutdownServer()方法安全关闭mongod:
db.getSiblingDB("admin").shutdownServer()
Repeat until all config servers are offline.重复此操作,直到所有配置服务器都脱机。
Shut down shard replica set mongod instances.关闭分片副本集mongod实例。
mongod instances.For each shard replica set, connect 对于每个分片副本集,将mongosh to each mongod member in the replica set and shut them down. mongosh连接到副本集中的每个mongod成员并关闭它们。Shut down the primary member last.最后关闭primary成员。
Use the 在db.shutdownServer() method on the admin database to safely shut down the mongod:admin数据库上使用db.shutdownServer()方法安全关闭mongod:
db.getSiblingDB("admin").shutdownServer()
Repeat this step for each shard replica set until all 对每个分片副本集重复此步骤,直到所有分片副本集中的所有mongod instances in all shard replica sets are offline.mongod实例都脱机。
Once this step is complete, the entire sharded cluster should be offline.完成此步骤后,整个分片集群应处于脱机状态。
Enforce Access Control on the Config Servers.对配置服务器实施访问控制。
Start each 启动配置服务器副本集中的每个mongod in the config server replica set. mongod。Include the 包括keyFile setting. keyFile设置。The keyFile setting enforces both Self-Managed Internal/Membership Authentication and Role-Based Access Control in Self-Managed Deployments.keyFile设置在自我管理部署中强制执行自我管理内部/成员身份验证和基于角色的访问控制。
You can specify the 您可以通过配置文件或命令行指定mongod settings either via a configuration file or the command line.mongod设置。
Configuration File配置文件
If using a configuration file, for a config server replica set, set 如果使用配置文件,对于配置服务器副本集,请将security.keyFile to the keyfile's path, sharding.clusterRole to configsvr, and replication.replSetName to the name of the config server replica set.security.keyFile设置为键文件的路径,将sharding.clusterRole设置为configsvr,并将replication.replSetName设置为配置服务器副本集中的名称。
security:
keyFile: <path-to-keyfile>
sharding:
clusterRole: configsvr
replication:
replSetName: <setname>
storage:
dbpath: <path>
Include additional options as required for your configuration. 根据配置要求,包括其他选项。For instance, if you wish remote clients to connect to your deployment or your deployment members are run on different hosts, specify the 例如,如果您希望远程客户端连接到部署,或者部署成员在不同的主机上运行,请指定net.bindIp setting.net.bindIp设置。
Start the 启动mongod specifying the --config option and the path to the configuration file.mongod,指定--config选项和配置文件的路径。
mongod --config <path-to-config>
Command Line命令行
If using the command line parameters, for a config server replica set, start the 如果使用命令行参数,对于配置服务器副本集,请使用mongod with the -keyFile, --configsvr, and --replSet parameters.-keyFile、--configsvr和--replSet参数启动mongod。
mongod --keyFile <path-to-keyfile> --configsvr --replSet <setname> --dbpath <path>
Include additional options as required for your configuration. For instance, if you wish remote clients to connect to your deployment or your deployment members are run on different hosts, specify the 根据配置要求,包括其他选项。例如,如果您希望远程客户端连接到部署,或者部署成员在不同的主机上运行,请指定--bind_ip.--bind_ip。
For more information on command line options, see the 有关命令行选项的更多信息,请参阅mongod reference page.mongod参考页面。
Make sure to use the original replica set name when restarting each member. You cannot change the name of a replica set.重新启动每个成员时,请确保使用原始副本集名称。您无法更改副本集的名称。
Enforce Access Control for each Shard in the Sharded Cluster.对分片集群中的每个分片实施访问控制。
Running a 使用mongod with the keyFile parameter enforces both Self-Managed Internal/Membership Authentication and Role-Based Access Control in Self-Managed Deployments.keyFile参数运行mongod会在自我管理部署中强制执行自我管理内部/成员身份验证和基于角色的访问控制。
Start each 使用配置文件或命令行启动副本集中的每个mongod in the replica set using either a configuration file or the command line.mongod。
Configuration File配置文件
If using a configuration file, set the 如果使用配置文件,请将security.keyFile option to the keyfile's path and the replication.replSetName option to the original name of the replica set.security.keyFile选项设置为键文件的路径,将replication.replSetName选项设置为副本集的原始名称。
security:
keyFile: <path-to-keyfile>
replication:
replSetName: <setname>
storage:
dbPath: <path>
Include additional options as required for your configuration. For instance, if you wish remote clients to connect to your deployment or your deployment members are run on different hosts, specify the 根据配置要求,包括其他选项。例如,如果您希望远程客户端连接到部署,或者部署成员在不同的主机上运行,请指定net.bindIp setting.net.bindIp设置。
Start the 启动mongod specifying the --config option and the path to the configuration file.mongod,指定--config选项和配置文件的路径。
mongod --config <path-to-config-file>
Command Line
If using the command line parameters, start the 如果使用命令行参数,请启动mongod and specify the --keyFile and --replSet parameters.mongod并指定--keyFile和--replSet参数。
mongod --keyfile <path-to-keyfile> --replSet <setname> --dbpath <path>
Include additional options as required for your configuration. For instance, if you wish remote clients to connect to your deployment or your deployment members are run on different hosts, specify the 根据配置要求,包括其他选项。例如,如果您希望远程客户端连接到部署,或者部署成员在不同的主机上运行,请指定--bind_ip.--bind_ip。
For more information on startup parameters, see the 有关启动参数的更多信息,请参阅mongod reference page.mongod参考页面。
Make sure to use the original replica set name when restarting each member. You cannot change the name of a replica set.重新启动每个成员时,请确保使用原始副本集名称。您无法更改副本集的名称。
Repeat this step until all shards in the cluster are online.重复此步骤,直到集群中的所有分片都联机。
Create a Shard-Local User Administrator (Optional).创建分片本地用户管理员(可选)。
Important
The Localhost Exception in Self-Managed Deployments allows clients connected over the localhost interface to create users on a 自我管理部署中的Localhost异常允许通过Localhost接口连接的客户端在mongod enforcing access control. mongod上创建用户,以实施访问控制。After creating the first user, the Localhost Exception in Self-Managed Deployments closes.创建第一个用户后,自我管理部署中的Localhost异常将关闭。
The first user must have privileges to create other users, such as a user with the 第一个用户必须具有创建其他用户的权限,例如具有userAdminAnyDatabase. userAdminAnyDatabase的用户。This ensures that you can create additional users after the Localhost Exception in Self-Managed Deployments closes.这可确保您可以在自我管理部署中的Localhost异常关闭后创建其他用户。
If at least one user does not have privileges to create users, once the localhost exception closes you may be unable to create or modify users with new privileges, and therefore unable to access certain functions or operations.如果至少有一个用户没有创建用户的权限,一旦localhost异常关闭,您可能无法创建或修改具有新权限的用户,因此无法访问某些功能或操作。
For each shard replica set in the cluster, connect 对于集群中的每个分片副本集,通过localhost接口将mongosh to the primary member over the localhost interface. mongosh连接到主成员。You must run 您必须在与目标mongosh on the same machine as the target mongod to use the localhost interface.mongod相同的机器上运行mongosh才能使用localhost接口。
Create a user with the 在userAdminAnyDatabase role on the admin database. admin数据库上创建一个具有userAdminAnyDatabase角色的用户。This user can create additional users for the shard replica set as necessary. 此用户可以根据需要为分片副本集创建其他用户。Creating this user also closes the Localhost Exception in Self-Managed Deployments.创建此用户也会关闭自我管理部署中的Localhost异常。
The following example creates the shard-local user 以下示例在fred on the admin database.admin数据库上创建分片本地用户fred。
Important
Passwords should be random, long, and complex to ensure system security and to prevent or delay malicious access.密码应该是随机的、长的和复杂的,以确保系统安全,防止或延迟恶意访问。
Tip
You can use the 您可以将passwordPrompt() method in conjunction with various user authentication management methods and commands to prompt for the password instead of specifying the password directly in the method or command call. passwordPrompt()方法与各种用户身份验证管理方法和命令结合使用,以提示输入密码,而不是直接在方法或命令调用中指定密码。However, you can still specify the password directly as you would with earlier versions of the 但是,您仍然可以像使用早期版本的mongo shell.mongo shell一样直接指定密码。
admin = db.getSiblingDB("admin")
admin.createUser(
{
user: "fred",
pwd: passwordPrompt(), // or cleartext password
roles: [ { role: "userAdminAnyDatabase", db: "admin" } ]
}
)Enforce Access Control for the mongos servers.对mongos服务器实施访问控制。
mongos servers.Running a 使用mongod with the keyFile parameter enforces both Self-Managed Internal/Membership Authentication and Role-Based Access Control in Self-Managed Deployments.keyFile参数运行mongod会在自我管理部署中强制执行自我管理内部/成员身份验证和基于角色的访问控制。
Start each 使用配置文件或命令行启动副本集中的每个mongos in the replica set using either a configuration file or the command line.mongos。
Configuration File配置文件
If using a configuration file, set the 如果使用配置文件,请将security.keyFile to the keyfile`s path and the sharding.configDB to the replica set name and at least one member of the replica set in <replSetName>/<host:port> format.security.keyFile设置为键文件的路径,将sharding.configDB设置为副本集名称,并以<replSetName>/<host:port>格式设置副本集的至少一个成员。
security:
keyFile: <path-to-keyfile>
sharding:
configDB: <configReplSetName>/cfg1.example.net:27019,cfg2.example.net:27019,...
Include additional options as required for your configuration. For instance, if you wish remote clients to connect to your deployment or your deployment members are run on different hosts, specify the 根据配置要求,包括其他选项。例如,如果您希望远程客户端连接到部署,或者部署成员在不同的主机上运行,请指定net.bindIp setting.net.bindIp设置。
Start the 启动mongos specifying the --config option and the path to the configuration file.mongos,指定--config选项和配置文件的路径。
mongos --config <path-to-config-file>
Command Line命令行
If using command line parameters start the 如果使用命令行参数,请启动mongos and specify the --keyFile and --configdb parameters.mongos并指定--keyFile和--configdb参数。
mongos --keyFile <path-to-keyfile> --configdb <configReplSetName>/cfg1.example.net:27019,cfg2.example.net:27019,...
Include additional options as required for your configuration. For instance, if you wish remote clients to connect to your deployment or your deployment members are run on different hosts, specify the 根据配置要求,包括其他选项。例如,如果您希望远程客户端连接到部署,或者部署成员在不同的主机上运行,请指定--bind_ip.--bind_ip。
At this point, the entire sharded cluster is back online and can communicate internally using the keyfile specified. However, external programs like 此时,整个分片集群重新联机,可以使用指定的键文件进行内部通信。但是,像mongosh need to use a correctly provisioned user in order to read or write to the cluster.mongosh这样的外部程序需要使用正确配置的用户才能读取或写入集群。
Connect to the mongos instance over the localhost interface.通过localhost接口连接到mongos实例。
mongos instance over the localhost interface.Connect 通过localhost接口将mongosh to one of the mongos instances over the localhost interface. mongosh连接到mongos实例之一。You must run 您必须在与mongosh on the same physical machine as the mongos instance.mongos实例相同的物理机器上运行mongosh。
The localhost interface is only available since no users have been created for the deployment. 由于没有为部署创建用户,因此只能使用localhost接口。The localhost interface closes after the creation of the first user.创建第一个用户后,localhost接口关闭。
Create the user administrator.
Important
After you create the first user, the localhost exception is no longer available.创建第一个用户后,localhost异常不再可用。
The first user must have privileges to create other users, such as a user with the 第一个用户必须具有创建其他用户的权限,例如具有userAdminAnyDatabase. userAdminAnyDatabase的用户。This ensures that you can create additional users after the Localhost Exception in Self-Managed Deployments closes.这可确保您可以在自我管理部署中的Localhost异常关闭后创建其他用户。
If at least one user does not have privileges to create users, once the localhost exception closes you cannot create or modify users, and therefore may be unable to perform necessary operations.如果至少有一个用户没有创建用户的权限,则一旦localhost异常关闭,您就无法创建或修改用户,因此可能无法执行必要的操作。
Add a user using the 使用db.createUser() method. db.createUser()方法添加用户。The user should have at minimum the 用户在userAdminAnyDatabase role on the admin database.admin数据库上至少应具有userAdminAnyDatabase角色。
Important
Passwords should be random, long, and complex to ensure system security and to prevent or delay malicious access.密码应该是随机的、长的和复杂的,以确保系统安全,防止或延迟恶意访问。
The following example creates the user 以下示例在fred on the admin database:admin数据库上创建用户fred:
Tip
You can use the 您可以将passwordPrompt() method in conjunction with various user authentication management methods and commands to prompt for the password instead of specifying the password directly in the method or command call. passwordPrompt()方法与各种用户身份验证管理方法和命令结合使用,以提示输入密码,而不是直接在方法或命令调用中指定密码。However, you can still specify the password directly as you would with earlier versions of the 但是,您仍然可以像使用早期版本的mongo shell.mongo shell一样直接指定密码。
admin = db.getSiblingDB("admin")
admin.createUser(
{
user: "fred",
pwd: passwordPrompt(), // or cleartext password
roles: [ { role: "userAdminAnyDatabase", db: "admin" } ]
}
)
See Database User Roles for a full list of built-in roles and related to database administration operations.有关内置角色和与数据库管理操作相关的完整列表,请参阅数据库用户角色。
Authenticate as the user administrator.以用户管理员身份进行身份验证。
Use 使用db.auth() to authenticate as the user administrator to create additional users:db.auth()以用户管理员身份进行身份验证,以创建其他用户:
Tip
You can use the 您可以将passwordPrompt() method in conjunction with various user authentication management methods and commands to prompt for the password instead of specifying the password directly in the method or command call. passwordPrompt()方法与各种用户身份验证管理方法和命令结合使用,以提示输入密码,而不是直接在方法或命令调用中指定密码。However, you can still specify the password directly as you would with earlier versions of the 但是,您仍然可以像使用早期版本的mongo shell.mongo shell一样直接指定密码。
db.getSiblingDB("admin").auth("fred", passwordPrompt()) // or cleartext password
Enter the password when prompted.出现提示时输入密码。
Alternatively, connect a new 或者,使用mongosh session to the target replica set member using the -u <username>, -p <password>, and the --authenticationDatabase "admin" parameters. -u <username>、-p <password>和--authenticationDatabase "admin"参数将新的mongosh会话连接到目标副本集成员。You must use the Localhost Exception in Self-Managed Deployments to connect to the 您必须在自我管理部署中使用Localhost异常来连接到mongos.mongos。
mongosh -u "fred" -p --authenticationDatabase "admin"
If you do not specify the password to the 如果不在-p command-line option, mongosh prompts for the password.-p命令行选项中指定密码,mongosh会提示输入密码。
Create Administrative User for Cluster Management创建群集管理的管理用户
The cluster administrator user has the 集群管理员用户具有分片集群的clusterAdmin role for the sharded cluster and not the shard-local cluster administrator.clusterAdmin角色,而不是分片本地集群管理员。
The following example creates the user 以下示例在ravi on the admin database.admin数据库上创建用户ravi。
Important
Passwords should be random, long, and complex to ensure system security and to prevent or delay malicious access.密码应该是随机的、长的和复杂的,以确保系统安全,防止或延迟恶意访问。
Tip
You can use the 您可以将passwordPrompt() method in conjunction with various user authentication management methods and commands to prompt for the password instead of specifying the password directly in the method or command call. passwordPrompt()方法与各种用户身份验证管理方法和命令结合使用,以提示输入密码,而不是直接在方法或命令调用中指定密码。However, you can still specify the password directly as you would with earlier versions of the 但是,您仍然可以像使用早期版本的mongo shell.mongo shell一样直接指定密码。
db.getSiblingDB("admin").createUser(
{
"user" : "ravi",
"pwd" : passwordPrompt(), // or cleartext password
roles: [ { "role" : "clusterAdmin", "db" : "admin" } ]
}
)
See Cluster Administration Roles for a full list of built-in roles related to replica set and sharded cluster operations.有关与副本集和分片群集操作相关的内置角色的完整列表,请参阅群集管理角色。
Authenticate as cluster admin.以群集管理员身份进行身份验证。
To perform sharding operations, authenticate as a 要执行分片操作,请使用clusterAdmin user with either the db.auth() method or a new mongosh session with the username, password, and authenticationDatabase parameters.db.auth()方法或使用username、password和authenticationDatabase参数的新mongosh会话作为clusterAdmin用户进行身份验证。
Note
This is the cluster administrator for the sharded cluster and not the shard-local cluster administrator.这是分片集群的集群管理员,而不是分片本地集群管理员。
Start the balancer.启动平衡器。
Start the balancer.启动平衡器。
sh.startBalancer()
Starting in MongoDB 6.0.3, automatic chunk splitting is not performed. This is because of balancing policy improvements. Auto-splitting commands still exist, but do not perform an operation.从MongoDB 6.0.3开始,不执行自动块分割。这是因为平衡了政策的改进。自动拆分命令仍然存在,但不执行操作。
In MongoDB versions earlier than 6.0.3, 在早于6.0.3的MongoDB版本中,sh.startBalancer() also enables auto-splitting for the sharded cluster.sh.startBalancer()还启用了分片集群的自动拆分。
Use the 使用sh.getBalancerState() to verify the balancer has started.sh.getBalancerState()验证平衡器是否已启动。
See Manage Sharded Cluster Balancer for tutorials on the sharded cluster balancer.有关分片集群平衡器的教程,请参阅管理分片集群均衡器。
Create additional users (Optional).创建其他用户(可选)。
Create users to allow clients to connect and access the sharded cluster. 创建用户以允许客户端连接和访问分片集群。See Database User Roles for available built-in roles, such as 有关可用的内置角色(如read and readWrite. read和readWrite),请参阅数据库用户角色。You may also want additional administrative users. For more information on users, see Users in Self-Managed Deployments.您可能还需要其他管理用户。有关用户的更多信息,请参阅自我管理部署中的用户。
To create additional users, you must authenticate as a user with 要创建其他用户,您必须以userAdminAnyDatabase or userAdmin roles.userAdminAnyDatabase或userAdmin角色的用户身份进行身份验证。
X.509 Internal Authentication内部认证
For details on using X.509 for internal authentication, see Use X.509 Certificates for Membership Authentication with Self-Managed MongoDB.有关使用X.509进行内部身份验证的详细信息,请参阅使用X.509证书进行自我管理MongoDB的成员身份验证。
To upgrade from keyfile internal authentication to X.509 internal authentication, see Upgrade Self-Managed MongoDB from Keyfile Authentication to X.509 Authentication.要从键文件内部身份验证升级到X.509内部身份验证,请参阅将自管理MongoDB从键文件身份验证升级为X.509身份验证。