php mongodb 连接 x509

php mongodb connect with x509

我安装了旧版 mongo php 1.6.10 驱动程序和支持的 1.2.5 mongodb php 驱动程序。 php Debian 8 上的版本是 5.6.29。

旧版驱动程序和受支持的驱动程序都可以使用基本凭据进行连接。

只有旧版驱动程序可以使用 x509 证书进行连接。

尝试对集合执行简单的 findOne 时,受支持的驱动程序会导致以下异常。

PHP Fatal error:  Uncaught exception 'MongoDB\Driver\Exception\RuntimeException' with message 'SCRAM Failure: invalid salt length of 0 in sasl step2'

我正在为 mongodb 驱动程序使用 Mongodb 客户端库 http://php.net/manual/en/set.mongodb.php

这是我正在使用的解释代码

<?php
$server = 'mongodb://uat-a:27017,uat-b:27017,uat-c:27017';
$options = [
    'replicaSet' => 'rs-uat',
    'username' => 'CN=my-user,OU=user,O=NA,L=Place,ST=State,C=GB',
    'authMechanism' => 'MONGODB-X509',
    'authSource' => '$external',
    'ssl' => true,
    'connect' => true,
];
$driverOptions = [
    'context' => stream_context_create(
        [
            'ssl' => [
                'local_cert' => '/etc/local-cert.pem',
                'cafile' => '/etc/cafile.pem',
            ],
        ]
    ),
];
$database = 'uatdata';

$client = new MongoDB\Client($server, $options, $driverOptions);
$db = $client->selectDatabase($database);

$doc = $db->selectCollection('errors')->findOne([], ['projection' => ['timestamp' => 1, 'uri' => 1]]);

答案是在 URI 字符串中传递 authMechanism 选项。例如

mongodb://uat-a:27017,uat-b:27017,uat-c:27017/?authMechanism=MONGODB-X509

可以在这里找到更详细的解释https://jira.mongodb.org/browse/PHPC-914