Overview概述
Enforcing access control on a replica set requires configuring:对副本集实施访问控制需要配置:
Security between members of the replica set using Internal Authentication, and使用内部身份验证的副本集成员之间的安全性,以及Security between connecting clients and the replica set using Role-Based Access Control in Self-Managed Deployments.在自我管理部署中使用基于角色的访问控制连接客户端和副本集之间的安全性。
For this tutorial, each member of the replica set uses the same internal authentication mechanism and settings.对于本教程,副本集的每个成员都使用相同的内部身份验证机制和设置。
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 Users and Authentication Mechanisms.请参阅用户和身份验证机制。
Cloud Manager and Ops Manager云经理和运维经理
If you are currently using or are planning to use Cloud Manager or Ops Manager, see the Cloud Manager manual or the Ops Manager manual for enforcing access control.如果您当前正在使用或计划使用Cloud Manager或Ops Manager,请参阅《云管理器手册》或《Ops管理器手册》以实施访问控制。
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绑定
mongod and mongos bind to localhost by default. If the members of your deployment are run on different hosts or if you wish remote clients to connect to your deployment, you must specify --bind_ip or net.bindIp.mongod和mongos默认绑定到localhost。如果部署的成员在不同的主机上运行,或者希望远程客户端连接到部署,则必须指定--bind_ip或net.bindIp。
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键文件安全
We recommend keyfiles only for testing and development environments, due to their limitations in manageability and cryptographic strength. 我们建议键文件仅用于测试和开发环境,因为它们在可管理性和加密强度方面存在局限性。For production environments, we strongly advise using X.509 certificates. 对于生产环境,我们强烈建议使用X.509证书。While keyfiles can be secure in specific, controlled scenarios, they present scalability and management challenges in complex deployments. 虽然键文件在特定的受控场景中是安全的,但在复杂的部署中,它们带来了可扩展性和管理挑战。X.509 certificates offer more robust security features, enable better key management, support individual authentication, and adhere to industry standards for sensitive data protection.X.509证书提供更强大的安全功能,实现更好的键管理,支持个人身份验证,并遵守敏感数据保护的行业标准。
Users and Authentication Mechanisms用户和身份验证机制
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.有关用户创建和管理的最佳实践,请参阅配置基于角色的访问控制。
Deploy New Replica Set with Keyfile Access Control使用键文件访问控制部署新副本集
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地址的节点无法启动验证,也无法启动。
Create a keyfile.创建键文件。
With keyfile authentication, each 通过键文件身份验证,副本集中的每个mongod instances in the replica set uses the contents of the keyfile as the shared password for authenticating other members in the deployment. mongod实例都使用键文件的内容作为共享密码,用于对部署中的其他成员进行身份验证。Only 只有具有正确键文件的mongod instances with the correct keyfile can join the replica set.mongod实例才能加入副本集。
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 replica set 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. It then uses chmod to change file permissions to provide read permissions for the file owner only:openssl生成一个复杂的伪随机1024字符串,用作共享密码。然后,它使用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 replica set member.将键文件复制到每个副本集成员。
Copy the keyfile to each server hosting the replica set members. Ensure that the user running the 将键文件复制到承载副本集成员的每个服务器。确保运行mongod instances is the owner of the file and can access the keyfile.mongod实例的用户是文件的所有者,并且可以访问键文件。
Avoid storing the keyfile on storage mediums that can be easily disconnected from the hardware hosting the 避免将键文件存储在容易与托管mongod instances, such as a USB drive or a network attached storage device.mongod实例的硬件断开连接的存储介质上,例如USB驱动器或网络连接的存储设备。
Start each member of the replica set with access control enabled.在启用访问控制的情况下启动副本集的每个成员。
For each member in the replica set, start the 对于副本集中的每个成员,使用mongod with either the security.keyFile configuration file setting or the --keyFile command-line option. security.keyFile配置文件设置或--keyFile命令行选项启动mongod。Running 使用mongod with the --keyFile command-line option or the security.keyFile configuration file setting enforces both Self-Managed Internal/Membership Authentication and Role-Based Access Control in Self-Managed Deployments.--keyFile命令行选项或security.keyFile配置文件设置运行mongod,可在自我管理部署中强制执行自我管理内部/成员身份验证和基于角色的访问控制。
Configuration File配置文件
If using a configuration file, set如果使用配置文件,请设置
security.keyFileto the keyfile's path, and到键文件的路径,以及replication.replSetNameto the replica set name.复制副本集名称。
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设置。
security:
keyFile: <path-to-keyfile>
replication:
replSetName: <replicaSetName>
net:
bindIp: localhost,<hostname(s)|ip address(es)>Start the 使用配置文件启动mongod using the configuration file:mongod:
mongod --config <path-to-config-file>
For more information on the configuration file, see configuration options.有关配置文件的更多信息,请参阅配置选项。
Command Line命令行
If using the command line options, start the 如果使用命令行选项,请使用以下选项启动mongod with the following options:mongod:
--keyFileset to the keyfile's path, and设置为键文件的路径,以及--replSetset to the replica set name.设置为副本集名称。
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。
mongod --keyFile <path-to-keyfile> --replSet <replicaSetName> --bind_ip localhost,<hostname(s)|ip address(es)>
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地址的节点无法启动验证,也无法启动。
For more information on command-line options, see the 有关命令行选项的更多信息,请参阅mongod reference page.mongod参考页面。
Connect to a member of the replica set over the localhost interface.通过localhost接口连接到副本集的成员。
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接口关闭。
Initiate the replica set.启动副本集。
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:在副本集配置文档中,包括:
The_idfield set to the replica set name specified in either thereplication.replSetNameor the--replSetoption._id字段设置为replication.replSetName或--replSet选项中指定的副本集名称。Themembersarray with a document per each member of the replica set.members数组,每个副本集的成员都有一个文档。
The following example initates a three member replica set.以下示例初始化一个由三个成员组成的副本集。
Important
Run 仅对副本集的一个rs.initiate() on only one mongod instance for the replica set.mongod实例运行rs.initiate()。
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地址的节点无法启动验证,也无法启动。
rs.initiate(
{
_id : "myReplSet",
members: [
{ _id : 0, host : "mongo1.example.net:27017" },
{ _id : 1, host : "mongo2.example.net:27017" },
{ _id : 2, host : "mongo3.example.net:27017" }
]
}
)
rs.initiate() triggers an election and elects one of the members to be the primary.触发选举,并选举其中一名成员为初选。
Connect to the primary before continuing. Use 请先连接到主服务器,然后再继续。使用rs.status() to locate the primary member.rs.status()查找主要成员。
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. This ensures that you can create additional users after the Localhost Exception in Self-Managed Deployments closes.userAdminAnyDatabase的用户。这可确保您可以在自我管理部署中的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。
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" } ]
}
)
Enter the password when prompted. See Database User Roles for a full list of built-in roles related to database administration operations.出现提示时输入密码。有关与数据库管理操作相关的内置角色的完整列表,请参阅数据库用户角色。
Authenticate as the user administrator.以用户管理员身份进行身份验证。
Authenticate to the 对admin database.admin数据库进行身份验证。
In 在mongosh, use db.auth() to authenticate. For example, the following authenticate as the user administrator fred:mongosh中,使用db.auth()进行身份验证。例如,以下人员以用户管理员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一样直接指定密码。
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会提示输入密码。
Create the cluster administrator.创建群集管理员。
The clusterAdmin role grants access to replication operations, such as configuring the replica set.clusterAdmin角色授予对复制操作的访问权限,例如配置副本集。
Create a cluster administrator user and assign the 创建一个集群管理员用户,并在clusterAdmin role in the admin database:admin数据库中分配clusterAdmin角色:
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" } ]
}
)
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.有关与副本集和分片群集操作相关的内置角色的完整列表,请参阅群集管理角色。
Create additional users (Optional).创建其他用户(可选)。
Create users to allow clients to connect and interact with the replica set. See Database User Roles for basic built-in roles to use in creating read-only and read-write users.创建用户以允许客户端连接副本集并与之交互。有关创建只读和读写用户时使用的基本内置角色,请参阅数据库用户角色。
You may also want additional administrative users. For more information on users, see Users in Self-Managed Deployments.您可能还需要其他管理用户。有关用户的更多信息,请参阅自我管理部署中的用户。
X.509 Internal AuthenticationX.509内部身份验证
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身份验证。