PHP Velho Oeste 2024

接続の作成

データベースサーバーに接続するには、次のいずれかの方法を使います。

<?php
$connection 
= new MongoClient(); // localhost:27017 に接続します
$connection = new MongoClient"mongodb://example.com" ); // リモートホスト (デフォルトのポート: 27017) に接続します
$connection = new MongoClient"mongodb://example.com:65432" ); // リモートホストの指定したポートに接続します
?>

明示的にデータベースとの接続を切断する必要はありません。 ドライバは持続的接続を利用し、確立済みの接続を再利用します。

参考

接続のページで、 さまざまな形式の接続を扱っています。

MongoClient クラスや MongoClient::__construct() の API ドキュメントには、 利用可能なすべてのオプションや多くのサンプルがあります。

add a note add a note

User Contributed Notes 2 notes

up
3
Max Muster
4 years ago
The Class has changed again :

$m = new MongoDB\Driver\Manager("mongodb://alex:mypassword@10.111.0.2:27017/");
up
-92
Anonymous
7 years ago
Another way to connect, that works for me:

require 'vendor/autoload.php';//composer require "mongodb/mongodb=^1.0.0.0"

$m = new MongoDB\Client("mongodb://alex:mypassword@10.111.0.2:27017/");
echo "Connection to database successfull. <br>";
To Top