On this page本页内容
Enforcing access control on a sharded cluster requires configuring:在分片群集上实施访问控制需要配置:
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.请参见访问控制。
If you are using Cloud Manager or Ops Manager to manage your deployment, see the respective Cloud Manager manual or the Ops Manager manual to enforce authentication.如果您使用Cloud Manager或Ops Manager管理部署,请参阅相应的Cloud管理器手册或Ops管理器手册以强制验证。
To avoid configuration updates due to IP address changes, use DNS hostnames instead of IP addresses. 为了避免由于IP地址更改而导致的配置更新,请使用DNS主机名而不是IP地址。It is particularly important to use a DNS hostname instead of an IP address when configuring replica set members or sharded cluster members.在配置副本集成员或分片群集成员时,使用DNS主机名而不是IP地址尤为重要。
Use hostnames instead of IP addresses to configure clusters across a split network horizon. 使用主机名而不是IP地址来跨拆分的网络范围配置群集。Starting in MongDB 5.0, nodes that are only configured with an IP address will fail startup validation and will not start.从MongDB 5.0开始,仅配置IP地址的节点将无法启动验证,无法启动。
Changed in version 3.6.在版本3.6中更改。
Starting with MongoDB 3.6, MongoDB binaries, 从MongoDB 3.6开始,MongoDB二进制文件mongod
and mongos
, bind to localhost
by default. mongod
和mongos
默认绑定到localhost
。From MongoDB versions 2.6 to 3.4, only the binaries from the official MongoDB RPM (Red Hat, CentOS, Fedora Linux, and derivatives) and DEB (Debian, Ubuntu, and derivatives) packages would bind to 从MongoDB版本2.6到3.4,默认情况下,只有来自官方MongoDB RPM(Red Hat、CentOS、Fedora Linux和衍生产品)和DEB(Debian、Ubuntu和衍生软件)包的二进制文件会绑定到localhost
by default. localhost
。To learn more about this change, see Localhost Binding Compatibility Changes.要了解有关此更改的详细信息,请参阅本地主机绑定兼容性更改。
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证书。
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. 对于用户身份验证,本教程使用默认的SCRAM身份验证机制。Challenge-response security mechanisms are best suited for testing or development environments. 挑战-响应安全机制最适合测试或开发环境。For production environments, we recommend using x.509 certificates or LDAP Proxy Authentication(available for MongoDB Enterprise only) or Kerberos Authentication(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.有关用户创建和管理的最佳做法,请参阅➤ 配置基于角色的访问控制。
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. Shard本地用户仅存在于特定的Shard中,应仅用于特定于Shard的维护和配置。You cannot connect to the 您不能使用shard本地用户连接到mongos
with shard-local users.mongos
。
This tutorial requires creating sharded cluster users, but includes optional steps for adding shard-local users.本教程要求创建分片集群用户,但包括添加分片本地用户的可选步骤。
See the Users security documentation for more information.有关详细信息,请参阅用户安全文档。
This tutorial uses the 本教程使用mongod
and mongos
programs. mongod
和mongos
程序。Windows users should use the Windows用户应改用exe
and exe
programs instead.exe
和exe
程序。
The following procedures involve creating a new sharded cluster that consists of a 以下过程涉及创建一个新的分片集群,该集群由一个mongos
, the config servers, and two shards.mongos
、配置服务器和两个分片组成。
To avoid configuration updates due to IP address changes, use DNS hostnames instead of IP addresses. 为了避免由于IP地址更改而导致的配置更新,请使用DNS主机名而不是IP地址。It is particularly important to use a DNS hostname instead of an IP address when configuring replica set members or sharded cluster members.在配置副本集成员或分片群集成员时,使用DNS主机名而不是IP地址尤为重要。
Use hostnames instead of IP addresses to configure clusters across a split network horizon. 使用主机名而不是IP地址来跨拆分的网络范围配置群集。Starting in MongDB 5.0, nodes that are only configured with an IP address will fail startup validation and will not start.从MongDB 5.0开始,仅配置IP地址的节点将无法启动验证,无法启动。
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
实例才能加入分片集群。
Starting in MongoDB 4.2, keyfiles for internal membership authentication use YAML format to allow for multiple keys in a keyfile. 从MongoDB 4.2开始,用于内部成员身份验证的密钥文件使用YAML格式,允许密钥文件中包含多个密钥。The YAML format accepts content of:YAML格式接受以下内容:
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. 密钥的长度必须介于6到1024个字符之间,并且只能包含base64集合中的字符。All members of the sharded cluster must share at least one common key.分片集群的所有成员必须至少共享一个公共密钥。
On UNIX systems, the keyfile must not have group or world permissions. 在UNIX系统上,密钥文件不得具有组或世界权限。On Windows systems, keyfile permissions are not checked.在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 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驱动器或网络连接的存储设备。
The following steps deploys a config server replica set.以下步骤部署配置服务器副本集。
For a production deployment, deploys a config server replica set with at least three members. 对于生产部署,部署至少有三个成员的配置服务器副本集。For testing purposes, you can create a single-member replica set.出于测试目的,您可以创建单个成员副本集。
Start each 启动配置服务器副本集中的每个mongod
in the config server replica set. mongod
。Include the 包括keyFile
setting. keyFile
设置。The keyFile
setting enforces both Internal/Membership Authentication and Role-Based Access Control.keyFile
设置强制执行内部/成员身份验证和基于角色的访问控制。
You can specify the 您可以通过配置文件或命令行指定mongod
settings either via a configuration file or the command line.mongod
设置。
Configuration File配置文件
If using a configuration file, set 如果使用配置文件,请将security.keyFile
to the keyfile's path, sharding.clusterRole
to configsvr
, and replication.replSetName
to the desired 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>
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
设置。For more information, see Localhost Binding Compatibility Changes.有关详细信息,请参阅本地主机绑定兼容性更改。
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
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, see Localhost Binding Compatibility Changes.有关详细信息,请参阅本地主机绑定兼容性更改。
Connect 通过localhost接口将mongosh
to one of the mongod
instances over the localhost interface. mongosh
连接到其中一个mongod
实例。You must run 您必须在与mongosh
on the same physical machine as the mongod
instance.mongod
实例相同的物理机器上运行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接口在创建第一个用户后关闭。
The rs.initiate()
method initiates the replica set and can take an optional replica set configuration document. rs.initiate()
方法启动副本集,并可以获取可选的副本集配置文档。In the replica set configuration document, include:在副本集配置文档中,包括:
_id
. _id
。_id
must match the --replSet
parameter passed to the mongod
._id
必须与传递给mongod
的--replSet
参数匹配。members
field. members
字段。members
field is an array and requires a document per each member of the replica set.members
字段是一个数组,每个副本集成员都需要一个文档。configsvr
field. configsvr
字段。configsvr
field must be set to true
for the config server replica set.configsvr
字段必须设置为true
。See Replica Set Configuration for more information on replica set configuration documents.有关副本集配置文档的更多信息,请参阅副本集配置。
Initiate the replica set using the 使用rs.initiate()
method and a configuration document:rs.initiate()
方法和配置文档启动副本集:
rs.initiate( { _id: "myReplSet", configsvr: true, members: [ { _id : 0, host : "cfg1.example.net:27019" }, { _id : 1, host : "cfg2.example.net:27019" }, { _id : 2, host : "cfg3.example.net:27019" } ] } )
Once the config server replica set (CSRS) is initiated and up, proceed to creating the shard replica sets.配置服务器副本集(CSRS)启动并启动后,继续创建分片副本集。
For a production deployment, use a replica set with at least three members. 对于生产部署,请使用至少有三个成员的副本集。For testing purposes, you can create a single-member replica set.出于测试目的,您可以创建单个成员副本集。
These steps include optional procedures for adding shard-local users. 这些步骤包括添加分片本地用户的可选过程。Executing them now ensures that there are users available for each shard to perform shard-level maintenance.现在执行它们可以确保每个分片都有用户可以执行分片级维护。
Running a 使用mongod
with the keyFile
parameter enforces both Internal/Membership Authentication and Role-Based Access Control.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, the replication.replSetName
to the desired name of the replica set, and the sharding.clusterRole
option to shardsvr
.security.keyFile
选项设置为密钥文件的路径,将replication.replSetName
设置为所需的副本集名称,将sharding.clusterRole
选项设置为shardsvr
。
security: keyFile: <path-to-keyfile> sharding: clusterRole: shardsvr replication: replSetName: <replSetName> 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
设置。For more information, see Localhost Binding Compatibility Changes.有关详细信息,请参阅本地主机绑定兼容性更改。
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 option, when starting the component, specify the 如果使用命令行选项,在启动组件时,请指定--keyFile
, replSet
, and --shardsvr
parameters, as in the following example:--keyFile
、replSet
和--shardsvr
参数,如下例所示:
mongod --keyFile <path-to-keyfile> --shardsvr --replSet <replSetName> --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, see Localhost Binding Compatibility Changes.有关详细信息,请参阅本地主机绑定兼容性更改。
Connect 通过localhost接口将mongosh
to one of the mongod
instances over the localhost interface. mongosh
连接到其中一个mongod
实例。You must run 您必须在与mongosh
on the same physical machine as the mongod
instance.mongod
实例相同的物理机器上运行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接口在创建第一个用户后关闭。
From 从mongosh
, run the rs.initiate()
method.mongosh
运行rs.initiate()
方法。
rs.initiate()
can take an optional replica set configuration document. 可以使用可选的副本集配置文档。In the replica set configuration document, include:在副本集配置文档中,包括:
_id
field set to the replica set name specified in either the replication.replSetName
or the --replSet
option._id
字段设置为replication.replSetName
或--replSet
选项中指定的副本集名称。members
array with a document per each member of the replica set.members
数组中的每个副本集成员都有一个文档。The following example initates a three member replica set.下面的示例初始化一个三成员副本集。
rs.initiate( { _id : "myReplSet", members: [ { _id : 0, host : "s1-mongo1.example.net:27018" }, { _id : 1, host : "s1-mongo2.example.net:27018" }, { _id : 2, host : "s1-mongo3.example.net:27018" } ] } )
rs.initiate()
triggers an election and elects one of the members to be the primary.rs.initiate()
触发一次选举,并选择其中一名成员作为主要成员。
Connect to the primary before continuing. 继续之前,请连接到主服务器。Use 使用rs.status()
to locate the primary member.rs.status()
查找主成员。
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 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 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
角色。
You must be connected to the primary to create users.您必须连接到primary
才能创建用户。
The following example creates the user 下面的示例在fred
with the userAdminAnyDatabase
role on the admin
database.admin
数据库上创建具有userAdminAnyDatabase
角色的用户fred
。
Passwords should be random, long, and complex to ensure system security and to prevent or delay malicious access.密码应随机、长且复杂,以确保系统安全并防止或延迟恶意访问。
Starting in version 4.2 of the 从mongoshell的4.2版开始,您可以将mongo
shell, you can use the passwordPrompt()
method in conjunction with various user authentication/management methods/commands to prompt for the password instead of specifying the password directly in the method/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" } ] } )
Enter the password when prompted. 出现提示时输入密码。See Database User Roles for a full list of built-in roles and related to database administration operations.有关内置角色和与数据库管理操作相关的完整列表,请参阅数据库用户角色。
Authenticate to the 对admin
database.admin
数据库进行身份验证。
In 在mongosh
, use db.auth()
to authenticate. mongosh
中,使用db.auth()
进行身份验证。For example, the following authenticate as the user administrator 例如,以下用户作为用户管理员fred
:fred
进行身份验证:
Starting in version 4.2 of the 从mongoshell的4.2版开始,您可以将mongo
shell, you can use the passwordPrompt()
method in conjunction with various user authentication/management methods/commands to prompt for the password instead of specifying the password directly in the method/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
Alternatively, connect a new 或者,使用mongosh
instance to the primary replica set member using the -u <username>
, -p <password>
, and the --authenticationDatabase
parameters.-u <username>
、-p <password>
和--authenticationDatabase
参数将新的mongosh
实例连接到主副本集成员。
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
会提示输入密码。
The shard-local cluster administrator user has the shard本地集群管理员用户具有clusterAdmin
role, which provides privileges that allow access to replication operations.clusterAdmin
角色,该角色提供允许访问复制操作的权限。
For a full list of roles related to replica set operations see Cluster Administration Roles.有关与副本集操作相关的角色的完整列表,请参阅群集管理角色。
Create a cluster administrator user and assign the 创建群集管理员用户,并在clusterAdmin
role in the admin
database:admin
数据库中分配clusterAdmin
角色:
Starting in version 4.2 of the 从mongoshell的4.2版开始,您可以将mongo
shell, you can use the passwordPrompt()
method in conjunction with various user authentication/management methods/commands to prompt for the password instead of specifying the password directly in the method/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" } ] } )
Enter the password when prompted.出现提示时输入密码。
See Cluster Administration Roles for a full list of built-in roles related to replica set and sharded cluster operations.有关与副本集和分片群集操作相关的内置角色的完整列表,请参阅群集管理角色。
mongos
to the Sharded Clustermongos
连接到分片群集mongos
to the clustermongos
连接到集群Start a 使用配置文件或命令行参数启动指定密钥文件的mongos
specifying the keyfile using either a configuration file or a command line parameter.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
设置。For more information, see Localhost Binding Compatibility Changes.有关详细信息,请参阅本地主机绑定兼容性更改。
Start the 启动mongos
specifying the --config
option and the path to the configuration file.mongos
,指定--config
选项和配置文件的路径。
mongos --config <path-to-config>
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
。For more information, see Localhost Binding Compatibility Changes.有关详细信息,请参阅本地主机绑定兼容性更改。
mongos
over the localhost interface.mongos
。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接口在创建第一个用户后关闭。
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 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.userAdminAnyDatabase
角色。
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
:
Starting in version 4.2 of the 从mongoshell的4.2版开始,您可以将mongo
shell, you can use the passwordPrompt()
method in conjunction with various user authentication/management methods/commands to prompt for the password instead of specifying the password directly in the method/command call. passwordPrompt()
方法与各种用户身份验证/管理方法/命令结合使用,提示输入密码,而不是直接在方法/命令调用中指定密码。However, you can still specify the password directly as you would with earlier versions of the 但是,您仍然可以像使用早期版本的mongoshell那样直接指定密码。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.有关内置角色和与数据库管理操作相关的完整列表,请参阅数据库用户角色。
Use db.auth()
to authenticate as the user administrator to create additional users:
Starting in version 4.2 of the 从mongoshell的4.2版开始,您可以将mongo
shell, you can use the passwordPrompt()
method in conjunction with various user authentication/management methods/commands to prompt for the password instead of specifying the password directly in the method/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 to connect to the 必须使用Localhost Exception连接到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
会提示输入密码。
The cluster administrator user has the 群集管理员用户具有clusterAdmin
role, which grants access to replication and sharding operations.clusterAdmin
角色,该角色授予复制和分片操作的访问权限。
Create a 在clusterAdmin
user in the admin
database.admin
数据库中创建clusterAdmin
用户。
The following example creates the user 下面的示例在ravi
on the admin
database.admin
数据库上创建用户ravi
。
Passwords should be random, long, and complex to ensure system security and to prevent or delay malicious access.密码应随机、长且复杂,以确保系统安全并防止或延迟恶意访问。
Starting in version 4.2 of the 从mongo
shell, you can use the passwordPrompt()
method in conjunction with various user authentication/management methods/commands to prompt for the password instead of specifying the password directly in the method/command call. mongo
shell的4.2版开始,您可以将passwordPrompt()
方法与各种用户身份验证/管理方法/命令结合使用,提示输入密码,而不是直接在方法/命令调用中指定密码。However, you can still specify the password directly as you would with earlier versions of the 但是,您仍然可以像使用早期版本的mongoshell那样直接指定密码。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.有关与副本集和分片群集操作相关的内置角色的完整列表,请参阅群集管理角色。
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
and readWrite
),请参阅数据库用户角色。You may also want additional administrative users. 您可能还需要其他管理用户。For more information on users, see Users.有关用户的详细信息,请参阅用户。
To create additional users, you must authenticate as a user with 要创建其他用户,您必须以userAdminAnyDatabase
or userAdmin
roles.userAdminAnyDatabase
或userAdmin
角色的用户身份进行身份验证。
To proceed, you must be connected to the 要继续,您必须连接到mongos
and authenticated as the cluster administrator user for the sharded cluster.mongos
,并作为分片集群的集群管理员用户进行身份验证。
This is the cluster administrator for the sharded cluster and not the shard-local cluster administrator.这是分片集群的集群管理员,而不是分片本地集群管理员。
To add each shard to the cluster, use the 要将每个分片添加到群集中,请使用sh.addShard()
method. sh.addShard()
方法。If the shard is a replica set, specify the name of the replica set and specify a member of the set. In production deployments, all shards should be replica sets.如果分片是副本集,请指定副本集的名称并指定该集的成员。在生产部署中,所有分片都应该是副本集。
The following operation adds a single shard replica set to the cluster:以下操作将单个分片副本集添加到集群:
sh.addShard( "<replSetName>/s1-mongo1.example.net:27017")
The following operation is an example of adding a standalone 以下操作是将独立mongod
shard to the cluster:mongod
分片添加到集群的示例:
sh.addShard( "s1-mongo1.example.net:27017")
Repeat these steps until the cluster includes all shards. 重复这些步骤,直到集群包含所有分片。At this point, the sharded cluster enforces access control for the cluster as well as for internal communications between each sharded cluster component.此时,分片集群对集群以及每个分片集群组件之间的内部通信实施访问控制。
To proceed, you must be connected to the 要继续,您必须连接到mongos
and authenticated as the cluster administrator user for the sharded cluster.mongos
,并作为分片集群的集群管理员用户进行身份验证。
This is the cluster administrator for the sharded cluster and not the shard-local cluster administrator.这是分片集群的集群管理员,而不是分片本地集群管理员。
Enabling sharding on a database makes it possible to shard collections within the database. 在数据库上启用分片可以在数据库中分片集合。Use the 使用sh.enableSharding()
method to enable sharding on the target database.sh.enableSharding()
方法在目标数据库上启用分片。
sh.enableSharding("<database>")
To proceed, you must be connected to the 要继续,您必须连接到mongos
and authenticated as the cluster administrator user for the sharded cluster.mongos
,并作为分片集群的集群管理员用户进行身份验证。
This is the cluster administrator for the sharded cluster and not the shard-local cluster administrator.这是分片集群的集群管理员,而不是分片本地集群管理员。
To shard a collection, use the 要分割集合,请使用sh.shardCollection()
method. You must specify the full namespace of the collection and a document containing the shard key.sh.shardCollection()
方法。您必须指定集合的完整名称空间和包含分片键的文档。
Your selection of shard key affects the efficiency of sharding, as well as your ability to take advantage of certain sharding features such as zones. 您选择的分片键会影响分片的效率,以及您利用某些分片功能(如分区)的能力。See the selection considerations listed in the Choose a Shard Key.请参见 选择分片键中列出的选择注意事项。
If the collection already contains data, you must create an index on the shard key using the 如果集合已经包含数据,则在使用db.collection.createIndex()
method before using shardCollection()
.shardCollection()
之前,必须使用db.collection.createIndex()
方法在分片键上创建索引。
If the collection is empty, MongoDB creates the index as part of 如果集合为空,MongoDB将创建索引作为sh.shardCollection()
.sh.shardCollection()
的一部分。
The following is an example of the 以下是sh.shardCollection()
method:sh.shardCollection()
方法的示例:
sh.shardCollection("<database>.<collection>", { <key> : <direction> } )
Create users to allow clients to connect to and interact with the sharded cluster.创建用户以允许客户端连接到分片集群并与之交互。
See Database User Roles for basic built-in roles to use in creating read-only and read-write users.有关创建只读和读写用户时使用的基本内置角色,请参阅数据库用户角色。
For details on using x.509 for internal authentication, see Use x.509 Certificate for Membership Authentication.有关将x.509用于内部身份验证的详细信息,请参阅使用x.509证书进行成员身份验证。
To upgrade from keyfile internal authentication to x.509 internal authentication, see Upgrade from Keyfile Authentication to x.509 Authentication.要从密钥文件内部身份验证升级到x.509内部身份验证,请参阅从密钥文件身份验证升级为x.509身份验证。