官术网_书友最值得收藏!

Connecting using PHP

The MongoDB PHP driver was rewritten from scratch two years ago to support the PHP 5, PHP 7, and HHVM architectures. The current architecture is shown in the following diagram:

Currently we have official drivers for all three architectures with full support for the underlying functionality.

Installation is a two-step process. First we need to install the MongoDB extension. This extension is dependent on the version of PHP (or HHVM) that we have installed and can be done using brew in Mac. For example with PHP 7.0:

brew install php70-mongodb

Then, using composer (a widely used dependency manager for PHP):

composer require mongodb/mongodb

Connecting to the database can then be done by using the connection string URI or by passing an array of options.

Using the connection string URI we have:

$client = new MongoDB\Client($uri = 'mongodb://127.0.0.1/', array $uriOptions = [], array $driverOptions = [])

For example, to connect to a replica set using SSL authentication:

$client = new MongoDB\Client('mongodb://myUsername:myPassword@rs1.example.com,rs2.example.com/?ssl=true&replicaSet=myReplicaSet&authSource=admin');

Or we can use the $uriOptions parameter to pass in parameters without using the connection string URL, like this:

$client = new MongoDB\Client(
'mongodb://rs1.example.com,rs2.example.com/'
[
'username' => 'myUsername',
'password' => 'myPassword',
'ssl' => true,
'replicaSet' => 'myReplicaSet',
'authSource' => 'admin',
],
);

The set of $uriOptions and the connection string URL options available are analogous to the ones used for Ruby and Python.

主站蜘蛛池模板: 泾阳县| 荥经县| 海口市| 普安县| 太湖县| 行唐县| 图木舒克市| 永年县| 彝良县| 平阴县| 曲周县| 焦作市| 石柱| 渝中区| 通化县| 丹巴县| 保康县| 义乌市| 新疆| 东阳市| 青神县| 南投市| 二连浩特市| 交口县| 江都市| 清丰县| 武强县| 渭南市| 康定县| 西充县| 太保市| 银川市| 吉木乃县| 鸡泽县| 庆阳市| 六枝特区| 东乌| 府谷县| 巴塘县| 九寨沟县| 佛坪县|