Deploy Replica Set With Keyfile Authentication使用密钥文件身份验证部署副本集

On this page本页内容

Overview概述

Enforcing access control on a replica set requires configuring:副本集强制实施访问控制需要配置:

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 the mongo shell need to use a user account.要连接到副本集,像mongoshell这样的客户端需要使用用户帐户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,请参阅Cloud管理器手册Ops管理器手册以强制访问控制。

Considerations注意事项

Tip

When possible, use a logical DNS hostname instead of an ip address, particularly when configuring replica set members or sharded cluster members.如果可能,请使用逻辑DNS主机名而不是ip地址,特别是在配置副本集成员或分片群集成员时。The use of logical DNS hostnames avoids configuration changes due to ip address changes.使用逻辑DNS主机名可以避免由于ip地址更改而导致的配置更改。

IP BindingIP绑定

Changed in version 3.6.在版本3.6中更改。

Starting in MongoDB 3.6, mongod and mongos bind to localhost by default.从MongoDB 3.6开始,mongodmongos默认绑定到localhost。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.如果部署的成员在不同的主机上运行,或者希望远程客户端连接到部署,则必须指定--bind_ipnet.bindIpFor more information, see Localhost Binding Compatibility Changes.有关更多信息,请参阅Localhost绑定兼容性更改

Operating System操作系统

This tutorial primarily refers to the mongod process.本教程主要介绍mongod过程。Windows users should use the mongod.exe program instead.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证书

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.对于用户身份验证,本教程使用默认的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企业版)或Kerberos身份验证(仅适用于MongoDB企业版)。

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部署具有密钥文件访问控制的新副本集

Tip

When possible, use a logical DNS hostname instead of an ip address, particularly when configuring replica set members or sharded cluster members.如果可能,请使用逻辑DNS主机名而不是ip地址,特别是在配置副本集成员或分片群集成员时。The use of logical DNS hostnames avoids configuration changes due to ip address changes.使用逻辑DNS主机名可以避免由于ip地址更改而导致的配置更改。

1

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.对于keyfile身份验证,副本集中的每个mongod实例都使用keyfile的内容作为共享密码,用于对部署中的其他成员进行身份验证。Only mongod instances with the correct keyfile can join the replica set.只有具有正确密钥文件的mongod实例才能加入副本集。

Note

Starting in MongoDB 4.2, keyfiles for internal membership authentication use YAML format to allow for multiple keys in a keyfile.从MongoDB 4.2开始,用于内部成员身份验证的keyfiles使用YAML格式来允许一个keyfile中有多个密钥。The YAML format accepts content of:YAML格式接受以下内容:

  • a single key string (same as in earlier versions),单个键字符串(与早期版本相同),
  • multiple key strings (each string must be enclosed in quotes), or多个键字符串(每个字符串必须用引号括起来),或
  • 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.键的长度必须介于6到1024个字符之间,并且只能包含base64集合中的字符。All members of the replica set must share at least one common key.副本集的所有成员必须至少共享一个公共密钥。

Note

On UNIX systems, the keyfile must not have group or world permissions.在UNIX系统上,密钥文件不能具有组或世界权限。On Windows systems, keyfile permissions are not checked.在Windows系统上,不检查keyfile权限。

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.有关使用密钥文件的其他详细信息和要求,请参见密钥文件

2

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驱动器或网络连接存储设备。

3

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命令行选项启动mongodRunning mongod with the --keyFile command-line option or the security.keyFile configuration file setting enforces both Internal/Membership Authentication and Role-Based Access Control.配合--keyFile命令行选项或security.keyFile配置文件设置运行mongod同时强制内部/成员身份验证基于角色的访问控制

Configuration File配置文件

If using a configuration file, set如果使用配置文件,请设置

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.有关更多信息,请参阅Localhost绑定兼容性更改

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

  • --keyFile set to the keyfile’s path, and设置到密钥文件的路径,以及
  • --replSet set 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_ipFor more information, see Localhost Binding Compatibility Changes.有关更多信息,请参阅Localhost绑定兼容性更改

mongod --keyFile <path-to-keyfile> --replSet <replicaSetName> --bind_ip localhost,<hostname(s)|ip address(es)>

Tip

When possible, use a logical DNS hostname instead of an ip address, particularly when configuring replica set members or sharded cluster members.如果可能,请使用逻辑DNS主机名而不是ip地址,特别是在配置副本集成员或分片群集成员时。The use of logical DNS hostnames avoids configuration changes due to ip address changes.使用逻辑DNS主机名可以避免由于ip地址更改而导致的配置更改。

For more information on command-line options, see the mongod reference page.有关命令行选项的更多信息,请参阅mongod参考页。

4

Connect to a member of the replica set over the localhost interface.通过localhost接口连接到副本集的成员。

Connect a mongo shell to one of the mongod instances over the localhost interface.通过localhost接口mongo shell连接到其中一个mongod实例。You must run the mongo shell on the same physical machine as the mongod instance.必须在与mongod实例相同的物理计算机上运行mongoshell。

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接口关闭。

5

Initiate the replica set.启动复制集。

From the mongo shell, run the rs.initiate() method.mongo shell运行rs.initiate()方法。

rs.initiate() can take an optional replica set configuration document.rs.initiate()可以获取可选的副本集配置文档In the replica set configuration document, include:副本集配置文档中,包括:

  • The _id field set to the replica set name specified in either the replication.replSetName or the --replSet option._id字段设置为replication.replSetName--replSet选项中指定的副本集名称。
  • The members array 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 just one and only one mongod instance for the replica set.在且仅在副本集的一个mongod实例上运行rs.initiate()

Tip

When possible, use a logical DNS hostname instead of an ip address, particularly when configuring replica set members or sharded cluster members.如果可能,请使用逻辑DNS主机名而不是ip地址,特别是在配置副本集成员或分片群集成员时。The use of logical DNS hostnames avoids configuration changes due to ip address changes.使用逻辑DNS主机名可以避免由于ip地址更改而导致的配置更改。

rs.initiate(
  {
    _id : <replicaSetName>,
    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.rs.initiate()触发选举并选出其中一名成员作为主要成员

Connect to the primary before continuing.继续之前先连接到主服务器。Use rs.status() to locate the primary member.使用rs.status()以定位主要成员。

6

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 Exceptioncloses.这样可以确保在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.您必须连接到主服务器才能创建用户。

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

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.mongoshell的4.2版开始,您可以将passwordPrompt()方法与各种用户身份验证/管理方法/命令结合使用来提示输入密码,而不是直接在方法/命令调用中指定密码。However, you can still specify the password directly as you would with earlier versions of the mongo shell.但是,您仍然可以像使用早期版本的mongoshell一样直接指定密码。

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.有关内置角色和与数据库管理操作相关的完整列表,请参见数据库用户角色

7

Authenticate as the user administrator.作为用户管理员进行身份验证。

Authenticate to the admin database.admin数据库进行身份验证。

In the mongo shell, use db.auth() to authenticate.mongo shell中,使用db.auth()进行身份验证。For example, the following authenticate as the user administrator fred:例如,以下用户将以用户管理员fred身份进行身份验证:

Tip

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.mongoshell的4.2版开始,您可以将passwordPrompt()方法与各种用户身份验证/管理方法/命令结合使用来提示输入密码,而不是直接在方法/命令调用中指定密码。However, you can still specify the password directly as you would with earlier versions of the mongo shell.但是,您仍然可以像使用早期版本的mongoshell一样直接指定密码。

db.getSiblingDB("admin").auth("fred", passwordPrompt()) // or cleartext password

Alternatively, connect a new mongo shell to the primary replica set member using the -u <username>, -p <password>, and the --authenticationDatabase parameters.或者,使用-u<username>-p<password>--authenticationDatabase参数将新mongo shell连接到主副本集成员。

mongo -u "fred" -p  --authenticationDatabase "admin"

If you do not specify the password to the -p command-line option, the mongo shell prompts for the password.如果没有为-p命令行选项指定密码,mongo shell将提示输入密码。

8

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

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.mongoshell的4.2版开始,您可以将passwordPrompt()方法与各种用户身份验证/管理方法/命令结合使用来提示输入密码,而不是直接在方法/命令调用中指定密码。However, you can still specify the password directly as you would with earlier versions of the mongo shell.但是,您仍然可以像使用早期版本的mongoshell一样直接指定密码。

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.有关与副本集和分片群集操作相关的内置角色的完整列表,请参阅群集管理角色

9

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.有关用户的详细信息,请参阅用户

x.509 Internal Authentication内部身份验证

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.要从keyfile内部身份验证升级到x.509内部身份验证,请参阅从keyfile身份验证升级到x.509身份验证