Docs 菜单
Docs 主页
/ / /
PHP 库手册

保护您的数据

MongoDB支持多种保护数据库连接的机制。本页包含演示每种机制的代码示例。

提示

要学习;了解有关此页面上显示的任何机制的更多信息,请参阅每个部分中提供的链接。

要使用本页中的示例,请将代码示例复制到 示例应用程序 或您自己的应用程序中。确保将代码示例中的所有占位符(例如 <hostname>)替换为MongoDB 部署的相关值。

您可以使用以下示例应用程序来测试本页上的代码示例。 要使用示例应用程序,请执行以下步骤:

  1. 确保您已在项目中安装MongoDB PHP库。 要学习;了解有关安装MongoDB PHP库的更多信息,请参阅下载和安装指南。

  2. 复制以下代码并将其粘贴到新的.php文件中。

  3. 从此页面复制代码示例,并将其粘贴到文件中的指定行。

1<?php
2
3require __DIR__ . '/../vendor/autoload.php';
4
5// Start example code here
6
7// End example code here
8
9try {
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身份验证机制进行身份验证:

$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身份验证

以下代码演示如何创建连接 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 。 当您使用 MONGODB-AWS 机制时, MongoDB PHP库会尝试按列出的顺序从以下源检索您的Amazon Web Services凭证:

  1. 传递给 MongoDB\Client 构造函数的选项,可以作为连接string的一部分,也可以作为 $uriOptions大量参数

  2. 环境变量

  3. Amazon Web Services EKS AssumeRoleWithWebIdentity请求

  4. ECS容器元数据

  5. EC 2实例元数据

每个部分展示了在从传递给客户端或备用外部源的选项中检索Amazon Web Services档案时,如何使用 MONGODB-AWS 进行凭证验证。

要学习;了解有关使用 Amazon Web Services 进行身份验证的更多信息,请参阅身份验证指南中的 Amazon Web Services IAM 身份验证

以下代码展示了如何将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进行身份验证的更多信息,请参阅身份验证指南中的以下部分:

以下代码展示了如何为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文件的路径以连接到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 端点:

$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 验证服务器的证书:

$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);

重要

<password>替换连接 URI 中的 占位符时,请确保对值进行百分号编码

要学习;了解有关提供密钥文件密码的更多信息,请参阅 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

后退

日志记录

在此页面上