Overview概述
To secure against unauthorized access, enforce authentication for your deployments. Authentication for replica sets consists of internal authentication among the replica set members, and user access control for clients connecting to the replica set.为了防止未经授权的访问,请对部署强制进行身份验证。副本集的身份验证包括副本集成员之间的内部身份验证,以及连接到副本集的客户端的用户访问控制。
If your deployment does not currently enforce authentication, you can use the 如果部署当前没有强制身份验证,则可以使用--transitionToAuth option to enforce authentication without downtime.--transitionToAuth选项强制身份验证而不会停机。
This tutorial uses the keyfile internal authentication mechanism for internal security, and SCRAM-based role-based access controls for client connections.本教程使用键文件内部身份验证机制来实现内部安全,并使用基于SCRAM的基于角色的访问控制来实现客户端连接。
Cloud Manager and Ops Manager云经理和运维经理
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 Manager手册或Ops Manager手册以强制执行身份验证。
Architecture建筑
This tutorial assumes that your replica set can elect a new primary after stepping down the existing primary replica set member. This requires:本教程假设副本集可以在关闭现有主副本集成员后选择新的primary。这需要:
Transition State过渡态
A 运行mongod running with --transitionToAuth accepts both authenticated and non-authenticated connections. --transitionToAuth的mongod接受经过身份验证和未经身份验证的连接。Clients connected to the 在此转换状态期间连接到mongod during this transition state can perform read, write, and administrative operations on any database.mongod的客户端可以对任何数据库执行读取、写入和管理操作。
Client Access客户端访问
At the end of the following procedure, the replica set rejects any client attempting to make a non-authenticated connection. The procedure creates users for client applications to use when connecting to the replica set.在以下过程结束时,副本集会拒绝任何试图进行未经身份验证的连接的客户端。该过程为客户端应用程序创建用户,以便在连接到副本集时使用。
See ➤ Configure Role-Based Access Control for user creation and management best practices.有关用户创建和管理的最佳实践,请参阅➤ 配置基于角色的访问控制。
IP BindingIP绑定
MongoDB binaries, MongoDB二进制文件mongod and mongos, bind to localhost by default.mongod和mongos默认绑定到localhost。
Passwords密码
Important
Passwords should be random, long, and complex to ensure system security and to prevent or delay malicious access.密码应该是随机的、长的和复杂的,以确保系统安全,防止或延迟恶意访问。
Enforce Keyfile Access Control on Existing Replica Set对现有副本集实施键文件访问控制
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 the user administrator.创建用户管理员。
Connect to the primary to create a user with 连接到主服务器以创建具有userAdminAnyDatabase role. userAdminAnyDatabase角色的用户。The userAdminAnyDatabase role grants access to user creation on any database in the deployment.userAdminAnyDatabase角色授予对部署中任何数据库上的用户创建的访问权限。
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" } ]
}
)
At the completion of this procedure, any client that administers users in the replica set must authenticate as this user, or a user with similar permissions.完成此过程后,管理副本集中用户的任何客户端都必须以该用户或具有类似权限的用户身份进行身份验证。
See Database User Roles for a full list of built-in roles and related to database administration operations.有关内置角色和与数据库管理操作相关的完整列表,请参阅数据库用户角色。
Create the cluster administrator.创建群集管理员。
Connect to the primary to create a user with 连接到主服务器以创建具有clusterAdmin role. clusterAdmin角色的用户。The clusterAdmin role grants access to replication operations, such as configuring the replica set.clusterAdmin角色授予对复制操作的访问权限,例如配置副本集。
The following example creates the user 以下示例在ravi with the clusterAdmin role on the admin database.admin数据库上创建了具有clusterAdmin角色的用户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" } ]
}
)
At the completion of this procedure, any client that administrates or maintains the replica set must authenticate as this user, or a user with similar permissions.完成此过程后,任何管理或维护副本集的客户端都必须以该用户或具有类似权限的用户身份进行身份验证。
See Cluster Administration Roles for a full list of built-in roles related to replica set operations.有关与副本集操作相关的内置角色的完整列表,请参阅群集管理角色。
Create users for client applications.为客户端应用程序创建用户。
Create users to allow client application to connect and interact with the replica set. At the completion of this tutorial, clients must authenticate as a configured user to connect to the replica set.创建用户以允许客户端应用程序连接副本集并与之交互。完成本教程后,客户端必须作为配置用户进行身份验证才能连接到副本集。
See Database User Roles for basic built-in roles to use in creating read-only and read-write users.有关创建只读和读写用户时使用的基本内置角色,请参阅数据库用户角色。
The following creates a user with read and write permissions on the 下面创建一个对foo database.foo数据库具有读写权限的用户。
Important
Passwords should be random, long, and complex to ensure system security and to prevent or delay malicious access.密码应该是随机的、长的和复杂的,以确保系统安全,防止或延迟恶意访问。
Create a user with the 在readWrite role in the foo database.foo数据库中创建一个具有readWrite角色的用户。
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("foo").createUser(
{
"user" : "joe",
"pwd" : passwordPrompt(), // or cleartext password
roles: [ { "role" : "readWrite", "db" : "foo" } ]
}
)
Clients authenticating as this user can perform read and write operations against the 以该用户身份进行身份验证的客户端可以对foo database. foo数据库执行读写操作。See Authenticate a User with Self-Managed Deployments for more on creating an authenticated connection to the replica set.有关创建到副本集的经过身份验证的连接的更多信息,请参阅使用自我管理部署对用户进行身份验证。
See the Add Users tutorial for more information on adding users. 有关添加用户的更多信息,请参阅添加用户教程。Consider security best practices when adding new users.添加新用户时考虑安全最佳实践。
Update Client Applications更新客户端应用程序
At this point in the procedure, the replica set does not enforce authentication. However, client applications can still specify auth credentials and connect to the replica set.在该过程的这一点上,副本集不强制进行身份验证。但是,客户端应用程序仍然可以指定身份验证凭据并连接到副本集。
Update client applications to authenticate to the replica set using a configured user. Authenticated connections require a username, password, and the authentication database. 更新客户端应用程序,使用配置的用户对副本集进行身份验证。经过身份验证的连接需要用户名、密码和身份验证数据库。See Authenticate a User with Self-Managed Deployments.请参阅使用自我管理部署对用户进行身份验证。
For example, the following connects to a replica set named 例如,以下内容连接到名为mongoRepl and authenticates as the user joe.mongoRepl的副本集,并验证为用户joe。
mongosh -u joe -password -authenticationDatabase foo --host mongoRepl/mongo1.example.net:27017, mongo2.example.net:27017, mongo3.example.net:27017
If you do not specify the password to the 如果不在-p command-line option, mongosh prompts for the password.-p命令行选项中指定密码,mongosh会提示输入密码。
If your application uses a MongoDB driver, see the associated driver documentation for instructions on creating an authenticated connection.如果应用程序使用MongoDB驱动程序,请参阅相关的驱动程序文档,了解创建经过身份验证的连接的说明。
At the completion of this tutorial, the replica set rejects non-authenticated client connections. Performing this step now ensures clients can connect to the replica set before and after the transition.完成本教程后,副本集将拒绝未经身份验证的客户端连接。现在执行此步骤可确保客户端可以在转换前后连接到副本集。
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. 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 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驱动器或网络连接的存储设备。
Restart each secondary or arbiter member of the replica set with transitionToAuth.使用transitionToAuth重新启动副本集的每个辅助成员或仲裁器成员。
transitionToAuth.Restart each secondary or arbiter member in the replica set, including in the configuration:重新启动副本集中的每个辅助或仲裁器成员,包括配置中的成员:
Thesecurity.transitionToAuthsetting.security.transitionToAuth设置。Starting the将mongodwithsecurity.transitionToAuthset totrueplaces the instance in a transition state where it can accept and create both authenticated and non-authenticated connections.security.transitionToAuth设置为true启动mongod会将实例置于转换状态,在该状态下,它可以接受和创建经过身份验证和未经身份验证的连接。An internal authentication mechanism such as一种内部身份验证机制,如security.keyFile.security.keyFile。
You must restart each member one at a time to ensure a majority of members in the replica set remain online.您必须一次重新启动每个成员,以确保副本集中的大多数成员保持在线。
Shut down the secondary or arbiter members.关闭辅助成员或仲裁器成员。
From a 从连接到辅助或仲裁器的mongosh session that is connected to the secondary or arbiter, issue the db.shutdownServer() against the admin database.mongosh会话中,对admin数据库发出db.shutdownServer()。
admin = db.getSiblingDB("admin")
admin.shutdownServer()Restart the secondary or arbiter members with transitionToAuth使用transitionToAuth重新启动辅助成员或仲裁器成员
transitionToAuthSpecify the following settings in your configuration file.在配置文件中指定以下设置。
security.keyFile, with the path to the keyfile.,以及键文件的路径。replication.replSetNameto the original replica set name.更改为原始副本集名称。security.transitionToAuthto为true.truetrue。
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 the net.bindIp setting.mongod和mongos默认绑定到localhost。如果部署的成员在不同的主机上运行,或者希望远程客户端连接到部署,则必须指定net.bindIp设置。
security:
keyFile: <path-to-keyfile>
transitionToAuth: true
replication:
replSetName: <replicaSetName>Specify the 启动--config option with the path to the configuration file when starting the mongod.mongod时,使用配置文件的路径指定--config选项。
mongod --config <path-to-config-file>
For more information on the configuration file, see configuration options.有关配置文件的更多信息,请参阅配置选项。
Alternatively, you can use the equivalent 或者,您可以在启动mongod command-line options (e.g. --transitionToAuth and --keyFile) when starting your mongod. mongod时使用等效的mongod命令行选项(例如--transitionToAuth和--keyFile)。See the 有关完整的选项列表,请参阅mongod reference page for a complete list of options.mongod参考页面。
Include additional settings as appropriate to your deployment.根据部署情况包括其他设置。
At the end of this step, all secondaries and arbiters should be up and running with 在此步骤结束时,所有次级服务器和仲裁器都应该启动并运行,并将security.transitionToAuth set to true.security.transitionToAuth设置为true。
Step down the primary member of the replica set and restart it with --transitionToAuth.关闭副本集的主要成员,并使用--transitionToAuth重新启动它。
--transitionToAuth.Step down the primary member in the replica set and restart the member, including in its configuration:关闭副本集中的primary成员并重新启动该成员,包括在其配置中:
Thesecurity.transitionToAuthsetting.security.transitionToAuth设置。Starting the将mongodwithsecurity.transitionToAuthset totrueplaces the instance in a transition state where it can accept and create both authenticated and non-authenticated connections.security.transitionToAuth设置为true启动mongod会将实例置于转换状态,在该状态下,它可以接受和创建经过身份验证和未经身份验证的连接。An internal authentication mechanism such as一种内部身份验证机制,如security.keyFile.security.keyFile。
Step down the primary replica set member关闭主副本集成员
Connect to the primary using 使用mongosh and step down the primary using the rs.stepDown() method.mongosh连接到主服务器,并使用rs.stepDown()方法降级主服务器。
rs.stepDown()Shut down the old primary关闭旧primary
Once the primary steps down and the replica set elects a new primary, shut down the old primary 一旦primary关闭,副本集选择了一个新的主服务器,关闭旧的主服务器mongod.mongod。
From a 从连接到旧主服务器的mongosh session that is connected to the old primary, issue the db.shutdownServer() on the admin database.mongosh会话中,在admin数据库上发出db.shutdownServer()。
admin = db.getSiblingDB("admin")
admin.shutdownServer()Restart the old primary with transitionToAuth使用transitionToAuth重新启动旧主服务器
transitionToAuthSpecify the following settings in your configuration file.在配置文件中指定以下设置。
security.keyFile, with the path to the keyfile.,以及键文件的路径。replication.replSetNameto the original replica set name.为原始副本集名称。security.transitionToAuthtotrue.
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>
transitionToAuth: true
replication:
replSetName: <replicaSetName>
Start the 使用配置文件启动mongod using the configuration file.mongod。
mongod --config <path-to-config-file>
For more information on the configuration file, see configuration options.有关配置文件的更多信息,请参阅配置选项。
Alternatively, you can use the equivalent 或者,您可以在启动mongod command-line options (e.g. --transitionToAuth and --keyFile) when starting your mongod. mongod时使用等效的mongod命令行选项(例如--transitionToAuth和--keyFile)。See the 有关完整的选项列表,请参阅mongod reference page for a complete list of options.mongod参考页面。
Include additional settings as appropriate to your deployment.根据部署情况包括其他设置。
At the end of this step, all members of the replica set should be up and running with 在此步骤结束时,副本集的所有成员都应启动并运行,security.transitionToAuth set to true and security.keyFile set to the keyfile path.security.transitionToAuth设置为true,security.keyFile设置为键文件路径。
Restart secondaries and arbiters without 重新启动二级服务器和仲裁器,而无需--transitionToAuth
Restart each secondary or arbiter member in the replica set, removing the 重新启动副本集中的每个辅助成员或仲裁器成员,在重新启动时删除security.transitionToAuth option on restart. security.transitionToAuth选项。You must do this one at a time to ensure a majority of members in the replica set remain online.您必须一次执行一个操作,以确保副本集中的大多数成员保持在线。
If the majority of replica set members are offline at the same time, the replica set may go into read-only mode.如果大多数副本集成员同时脱机,则副本集可能会进入只读模式。
Shut down the secondary or arbiter members关闭辅助或仲裁器成员
Connect 将mongosh to the secondary or arbiter, and issue the db.shutdownServer() on the admin database.mongosh连接到辅助或仲裁器,并在admin数据库上发出db.shutdownServer()。
admin = db.getSiblingDB("admin")
admin.shutdownServer()Restart the secondary or arbiter members without 重新启动辅助成员或仲裁器成员,而无需transitionToAuth
Restart the 重新启动mongod, this time without the security.transitionToAuth option but with internal authentication mechanism such as security.keyFile.mongod,这次不使用security.transitionToAuth选项,但使用security.keyFile等内部身份验证机制。
Specify the following settings in your configuration file.在配置文件中指定以下设置。
security.keyFile, with the path to the keyfile.,以及键文件的路径。replication.replSetNameto the original 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>Start the 使用配置文件启动mongod using the configuration file:mongod:
mongod --config <path-to-config-file>
For more information on the configuration file, see configuration options.有关配置文件的更多信息,请参阅配置选项。
You can also use the equivalent 启动mongod options when starting your mongod. mongod时,您还可以使用等效的mongod选项。See the 有关完整的选项列表,请参阅mongod reference page for a complete list of options.mongod参考页面。
Include additional settings as appropriate to your deployment.根据部署情况包括其他设置。
At the end of this step, all secondaries and arbiters should be up and running with internal authentication configured, but without 在此步骤结束时,所有二级服务器和仲裁器都应已启动并运行,并配置了内部身份验证,但没有security.transitionToAuth. security.transitionToAuth。Clients can only connect to these 客户端只能使用配置的客户端身份验证机制连接到这些mongod instances by using the configured client authentication mechanism.mongod实例。
Step down and restart the primary replica set member without --transitionToAuth.在不使用--transitionToAuth的情况下关闭并重新启动主副本集成员。
--transitionToAuth.Step down the primary member in the replica set, then restart it without the 关闭副本集中的primary成员,然后在不使用security.transitionToAuth option.security.transitionToAuth选项的情况下重新启动它。
Important
At the end of this step, clients not connecting with auth cannot connect to the replica set. Update clients to connect with authentication before completing this step to avoid loss of connectivity.在此步骤结束时,未使用身份验证连接的客户端无法连接到副本集。在完成此步骤之前,更新客户端以使用身份验证进行连接,以避免连接丢失。
Step down the primary replica set member关闭主副本集成员
Connect to the primary using 使用mongosh and step down the primary using the rs.stepDown() method.mongosh连接到主服务器,并使用rs.stepDown()方法降级主服务器。
rs.stepDown()Shut down the old primary关闭旧primary
Once the primary steps down and the replica set elects a new primary, shut down the old primary 一旦主服务器关闭,副本集选择了一个新的主服务器,关闭旧的主服务器mongod.mongod。
From a 从连接到旧主服务器的mongosh session that is connected to the old primary, issue the db.shutdownServer() on the admin database.mongosh会话中,在管理数据库上发出db.shutdownServer()。
admin = db.getSiblingDB("admin")
admin.shutdownServer()Restart the old primary without 重新启动旧主服务器,而无需transitionToAuth
Restart the 重新启动mongod, this time without the security.transitionToAuth option but with the internal authentication mechanism such as security.keyFile.mongod,这次不使用security.transitionToAuth选项,但使用security.keyFile等内部身份验证机制。
Specify the following settings in your configuration file.在配置文件中指定以下设置。
security.keyFile, with the path to the keyfile.,以及键文件的路径。replication.replSetNameto the original replica set name.更改为原始副本集名称。
security:
keyFile: <path-to-keyfile>
replication:
replSetName: <replicaSetName>
Start the 使用配置文件启动mongod using the configuration file:mongod:
mongod --config <path-to-config-file>
For more information on the configuration file, see configuration options.有关配置文件的更多信息,请参阅配置选项。
You can also use the equivalent 启动mongod options when starting your mongod. See the mongod reference page for a complete list of options.mongod时,您还可以使用等效的mongod选项。有关完整的选项列表,请参阅mongod参考页面。
Include additional settings as appropriate to your deployment.根据部署情况包括其他设置。
At the end of this step, all members of the replica set should be up and running with authentication enforced. Clients can only connect to these 在此步骤结束时,副本集的所有成员都应启动并运行,并强制执行身份验证。客户端只能使用配置的客户端身份验证机制连接到这些mongod instances by using the configured client authentication mechanism.mongod实例。
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身份验证。