Overview
MongoDB支持多种保护数据库连接的机制。本页包含演示每种机制的代码示例。
提示
要学习;了解有关此页面上显示的任何机制的更多信息,请参阅每个部分中提供的链接。
To use an example from this page, copy the code example into the sample application or your own application. Make sure to replace all placeholders in the code examples, such as <hostname>, with the relevant values for your MongoDB deployment.
示例应用程序
您可以使用以下示例应用程序来测试本页上的代码示例。 要使用示例应用程序,请执行以下步骤:
确保您已在项目中安装MongoDB PHP库。 要学习;了解有关安装MongoDB PHP库的更多信息,请参阅下载和安装指南。
复制以下代码并将其粘贴到新的
.php文件中。从此页面复制代码示例,并将其粘贴到文件中的指定行。
提示
The require statement in the sample application loads the MongoDB PHP Library and assumes that the vendor directory is in the same directory as your application file. If your application file is located elsewhere, adjust the path to vendor/autoload.php accordingly.
1 2 3 require __DIR__ . '/vendor/autoload.php'; 4 5 // Start example code here 6 7 // End example code here 8 9 try { 10 $client->test->command(['ping' => 1]); 11 echo 'Successfully pinged the MongoDB server.', PHP_EOL; 12 } catch (MongoDB\Driver\Exception\RuntimeException $e) { 13 printf("Failed to ping the MongoDB server: %s\n", $e->getMessage()); 14 }
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-256身份验证的更多信息,请参阅身份验证指南中的SCRAM身份验证。
MONGODB X.509
以下代码演示如何创建连接 URI,以使用X.509身份验证机制进行身份验证:
$uriOptions = [ 'tls' => true, 'tlsCertificateKeyFile' => '<file path>', 'authMechanism' => 'MONGODB-X509', ]; $client = new MongoDB\Client( 'mongodb://<hostname>:<port>', $uriOptions, );
$uri = 'mongodb://<hostname>:<port>/?tls=true&tlsCertificateKeyFile=<file path>&authMechanism=MONGODB-X509'; $client = new MongoDB\Client($uri);
要学习;了解有关 X.509 身份验证的更多信息,请参阅身份验证指南中的 X.509 身份验证。
MONGODB-AWS
以下部分介绍如何使用MONGODB-AWS身份验证机制连接到MongoDB 。 当您使用 MONGODB-AWS 机制时, MongoDB PHP库会尝试按列出的顺序从以下源检索您的Amazon Web Services凭证:
传递给
MongoDB\Client构造函数的选项,可以作为连接string的一部分,也可以作为$uriOptions大量参数环境变量
Amazon Web Services EKS
AssumeRoleWithWebIdentity请求ECS容器元数据
EC 2实例元数据
每个部分展示了在从传递给客户端或备用外部源的选项中检索Amazon Web Services档案时,如何使用 MONGODB-AWS 进行凭证验证。
要学习;了解有关使用 Amazon Web Services 进行身份验证的更多信息,请参阅身份验证指南中的 Amazon Web Services IAM 身份验证。
MongoDB\Client Credentials
以下代码展示了如何将Amazon Web Services凭证传递给 MongoDB\Client 构造函数,以便使用 MONGODB-AWS 进行身份验证:
$uriOptions = [ 'username' => '<AWS IAM access key ID>', 'password' => '<AWS IAM secret access key>', 'authMechanism' => 'MONGODB-AWS', ]; $client = new MongoDB\Client( 'mongodb://<hostname>:<port>', $uriOptions, );
$uri = 'mongodb://<AWS IAM access key ID>:<AWS IAM secret access key>@<hostname>:<port>/?authMechanism=MONGODB-AWS'; $client = new MongoDB\Client($uri);
要学习;了解通过检索 MongoDB\Client 档案来使用Amazon Web Services进行凭证验证的更多信息,请参阅身份验证指南中的MongoDB \Client 档案。
外部档案
以下代码展示了从环境变量、 AssumeRoleWithWebIdentity请求、ECS元数据或 EC 2实例元数据获取档案时如何使用MONGODB-AWS进行凭证验证:
$client = new MongoDB\Client( 'mongodb://<hostname>:<port>', ['authMechanism' => 'MONGODB-AWS'], );
$uri = 'mongodb://<hostname>:<port>/?authMechanism=MONGODB-AWS'; $client = new MongoDB\Client($uri);
要学习;了解有关通过获取外部凭证来使用Amazon Web Services进行身份验证的更多信息,请参阅身份验证指南中的以下部分:
传输层安全性 (TLS)
启用 TLS
以下代码展示了如何为MongoDB实例的连接启用TLS:
$client = new MongoDB\Client( 'mongodb://<hostname>:<port>/', ['tls' => true], );
$uri = 'mongodb://<hostname>:<port>/?tls=true'; $client = new MongoDB\Client($uri);
要学习;了解有关启用 TLS 的更多信息,请参阅 TLS 配置指南中的启用 TLS 。
指定证书颁发机构 (CA) 文件
以下代码演示如何指定 CA文件的路径以连接到MongoDB实例:
$client = new MongoDB\Client( 'mongodb://<hostname>:<port>/', ['tls' => true, 'tlsCAFile' => '/path/to/ca.pem'], );
$uri = 'mongodb://<hostname>:<port>/?tls=true&tlsCAFile=/path/to/ca.pem'; $client = new MongoDB\Client($uri);
要学习;了解有关指定 CA文件的更多信息,请参阅 TLS 配置指南中的指定 CA 文件。
禁用 OCSP 检查
以下代码演示如何阻止驱动程序联系 OCSP 终结点:
$client = new MongoDB\Client( 'mongodb://<hostname>:<port>/', ['tls' => true, 'tlsDisableOCSPEndpointCheck' => true], );
$uri = 'mongodb://<hostname>:<port>/?tls=true&tlsDisableOCSPEndpointCheck=true'; $client = new MongoDB\Client($uri);
要学习;了解有关禁用 OCSP 检查的更多信息,请参阅 TLS 配置指南中的OCSP 。
指定证书撤销列表 (CRL)
以下代码演示如何指示驱动程序根据 CRL 验证服务器的证书:
$client = new MongoDB\Client( 'mongodb://<hostname>:<port>/', ['tls' => true], ['crl_file' => '/path/to/file.pem'], );
要了解有关指定 CRL 的更多信息,请参阅 TLS 配置指南中的证书吊销列表。
出示客户端证书
以下代码显示如何指定驱动程序向 MongoDB 部署提供的客户端证书:
$client = new MongoDB\Client( 'mongodb://<hostname>:<port>/', ['tls' => true, 'tlsCertificateKeyFile' => '/path/to/client.pem'], );
$uri = 'mongodb://<hostname>:<port>/?tls=true&tlsCertificateKeyFile=/path/to/client.pem'; $client = new MongoDB\Client($uri);
要学习;了解有关指定客户端证书的更多信息,请参阅 TLS 配置指南中的提供客户端证书。
提供证书密钥文件密码
以下代码演示如何指定客户端证书的密码:
$client = new MongoDB\Client( 'mongodb://<hostname>:<port>/', [ 'tls' => true, 'tlsCertificateKeyFile' => '/path/to/client.pem', 'tlsCertificateKeyFilePassword' => '<password>', ], );
$uri = 'mongodb://<hostname>:<port>/?tls=true&tlsCertificateKeyFile=/path/to/client.pem&tlsCertificateKeyFilePassword=<password>'; $client = new MongoDB\Client($uri);
重要
替换连接 URI 中的 <password> 占位符时,请确保对该值进行百分比编码。
要学习;了解有关提供密钥文件密码的更多信息,请参阅 TLS 配置指南中的提供密钥密码。
允许不安全的 TLS
以下代码展示了如何放宽 TLS 约束,这与同时禁用证书验证和主机名验证具有相同的效果:
$client = new MongoDB\Client( 'mongodb://<hostname>:<port>/', ['tls' => true, 'tlsInsecure' => true], );
$uri = 'mongodb://<hostname>:<port>/?tls=true&tlsInsecure=true'; $client = new MongoDB\Client($uri);
要学习;了解有关允许不安全 TLS 的更多信息,请参阅 TLS 配置指南中的允许不安全 TLS 。
警告
将tlsInsecure选项设置为true可能会使您的应用程序面临安全风险。 启用此选项会使您的应用程序不安全,并且可能容易受到过期证书和冒充有效客户端实例的外部进程的攻击。
禁用证书验证
以下代码显示如何禁用证书验证:
$client = new MongoDB\Client( 'mongodb://<hostname>:<port>/', ['tls' => true, 'tlsAllowInvalidCertificates' => true], );
$uri = 'mongodb://<hostname>:<port>/?tls=true&tlsAllowInvalidCertificates=true'; $client = new MongoDB\Client($uri);
要学习;了解有关禁用证书验证的更多信息,请参阅 TLS 配置指南中的允许不安全的 TLS 。
禁用主机名验证
以下代码展示了如何禁用主机名验证:
$client = new MongoDB\Client( 'mongodb://<hostname>:<port>/', ['tls' => true, 'tlsAllowInvalidHostnames' => true], );
$uri = 'mongodb://<hostname>:<port>/?tls=true&tlsAllowInvalidHostnames=true'; $client = new MongoDB\Client($uri);
要学习;了解有关禁用主机名验证的更多信息,请参阅 TLS 配置指南中的允许不安全的 TLS 。