Overview
Salted 挑战响应身份验证机制 (SCRAM) 是一系列身份验证机制,它们使用质询-响应机制对用户进行身份验证。SCRAM-SHA-256(使用 SHA-256算法对密码进行哈希处理)是MongoDB Server 4.0 及更高版本中的默认身份验证机制。 SCRAM-SHA-1 改用 SHA-1算法,是早于 4.0 的MongoDB Server版本中的默认身份验证机制。
您可以使用SCRAM对MongoDB Atlas、 MongoDB Enterprise Advanced和MongoDB Community Edition进行身份验证。
SCRAM-SHA-256
SCRAM-SHA-256(如RFC 7677所定义)是MongoDB部署的默认身份验证机制。
要使用此机制进行身份验证,请设置以下连接选项:
username:要进行身份验证的用户名。 在将其包含在连接 URI 中之前,对该值进行百分比编码。password:用于身份验证的密码。 在将其包含在连接 URI 中之前,对该值进行百分比编码。authSource:要进行身份验证的MongoDB 数据库。 默认, MongoDB PHP库根据连接 URI 中的数据库进行身份验证(如果您包含数据库)。 如果没有,则会根据admin数据库进行身份验证。
您可以通过两种方式设立这些选项:将选项大量传递给 MongoDB\Client 构造函数,或通过连接 URI 中的参数。选择 MongoDB\Client 或 Connection URI标签页以查看相应的代码:
$uriOptions = [ 'username' => '<username>', 'password' => '<password>', 'authSource' => '<authentication database>', ]; $client = new MongoDB\Client( 'mongodb://<hostname>:<port>', $uriOptions, );
$uri = 'mongodb://<username>:<password>@<hostname>:<port>/?authSource=admin'; $client = new MongoDB\Client($uri);
您还可以通过将 authMechanism 连接选项设置为 'SCRAM-SHA-256' 来显式指定 SCRAM-SHA-256身份验证机制,如以下示例所示:
$uriOptions = [ 'username' => '<username>', 'password' => '<password>', 'authSource' => '<authentication database>', 'authMechanism' => 'SCRAM-SHA-256', ]; $client = new MongoDB\Client( 'mongodb://<hostname>:<port>', $uriOptions, );
$uri = 'mongodb://<username>:<password>@<hostname>:<port>/?authSource=admin&authMechanism=SCRAM-SHA-256'; $client = new MongoDB\Client($uri);
SCRAM-SHA-1
SCRAM-SHA-1, 如 RFC 5802 所定义,是一种 Salted 挑战响应身份验证机制 (SCRAM ),它使用通过 SHA-1算法加密的用户名和密码来对用户进行身份验证。
要使用此机制进行身份验证,请设置以下连接选项:
username:要进行身份验证的用户名。 在将其包含在连接 URI 中之前,对该值进行百分比编码。password:用于身份验证的密码。 在将其包含在连接 URI 中之前,对该值进行百分比编码。authSource:要进行身份验证的MongoDB 数据库。 默认, MongoDB PHP库根据连接 URI 中的数据库进行身份验证(如果您包含数据库)。 如果没有,则会根据admin数据库进行身份验证。authMechanism:设置为'SCRAM-SHA-1'。
您可以通过两种方式设立这些选项:将选项大量传递给 MongoDB\Client 构造函数,或通过连接 URI 中的参数。选择MongoDB\Client或Connection URI标签页以查看相应的代码:
$uriOptions = [ 'username' => '<username>', 'password' => '<password>', 'authSource' => '<authentication database>', 'authMechanism' => 'SCRAM-SHA-1', ]; $client = new MongoDB\Client( 'mongodb://<hostname>:<port>', $uriOptions, );
$uri = 'mongodb://<username>:<password>@<hostname>:<port>/?authSource=admin&authMechanism=SCRAM-SHA-1'; $client = new MongoDB\Client($uri);
更多信息
要学习;了解有关在MongoDB PHP库中创建MongoDB\Client对象的更多信息,请参阅创建MongoDB客户端指南。
要学习;了解有关连接选项的更多信息,请参阅指定连接选项指南。