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

  • Mastering MongoDB 3.x
  • Alex Giamas
  • 226字
  • 2021-08-20 10:10:53

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.

主站蜘蛛池模板: 太仓市| 文安县| 普兰店市| 海安县| 泗阳县| 临泉县| 日照市| 巴楚县| 信丰县| 泗阳县| 平南县| 乐亭县| 安远县| 信丰县| 昌邑市| 沁源县| 江津市| 龙口市| 三河市| 天气| 防城港市| 宿州市| 北宁市| 聂拉木县| 宿州市| 黄龙县| 汝城县| 山西省| 鄱阳县| 镇康县| 农安县| 贡山| 张家界市| 山东| 乌拉特前旗| 新营市| 太仆寺旗| 大冶市| 雅江县| 达孜县| 新河县|