[英]Can't connect from EC2 PHP to cloud MongoDB
I am trying to connect from my PHP running on EC2 to newly created MongoDB free cloud cluster.我正在尝试从运行在 EC2 上的 PHP 连接到新创建的 MongoDB 免费云集群。 MongoDB installed:
MongoDB安装:
sudo pecl install mongodb
And extension appear in php.ini:扩展名出现在 php.ini 中:
My code:我的代码:
<?php
echo "start";
echo(MONGODB_VERSION);
try
{
$client = new MongoDB\Driver\Manager(
'mongodb://user:password@cluster0-shard-00-00-tmugt.mongodb.net:27017,cluster0-shard-00-01-tmugt.mongodb.net:27017,cluster0-shard-00-02-tmugt.mongodb.net:27017/testDB?ssl=true&replicaSet=Cluster0-shard-0&authSource=admin&retryWrites=true&w=majority');
echo("<br>Client created");
}
catch (\Exception $e)
{
echo "***Client failed*** " . $e->getMessage();
exit();
}
echo("<br>");
var_dump($client);
echo("<br>");
var_dump(bin2hex(\MongoDB\BSON\fromPHP(['x'=>1])));
var_dump(class_exists('MongoDB\Driver\Manager'));
try{
$db = $client->selectDB("testDB");
echo("<db>Database selected");
}
catch(MongoConnectionException $e)
{
echo '<p>Couldn\'t connect to mongodb, is the "mongo" process running?</p>';
exit();
}
echo("<br>");
var_dump($db);
My output:我的输出:
start1.7.4
Client created
object(MongoDB\Driver\Manager)#1 (2) { ["uri"]=> string(249) "mongodb://user:password@cluster0-shard-00-00-tmugt.mongodb.net:27017,cluster0-shard-00-01-tmugt.mongodb.net:27017,cluster0-shard-00-02-tmugt.mongodb.net:27017/testDB?ssl=true&replicaSet=Cluster0-shard-0&authSource=admin&retryWrites=true&w=majority" ["cluster"]=> array(0) { } }
string(24) "0c0000001078000100000000" bool(true)
What I am missing?我缺少什么? Why I can't select database?
为什么我不能选择数据库?
Here is a correct way: https://www.php.net/manual/en/class.mongodb-driver-bulkwrite.php这是一个正确的方法: https : //www.php.net/manual/en/class.mongodb-driver-bulkwrite.php
<?php
//errror_reporting(E_ALL);
ini_set('display_errors', 1);
echo "start";
echo(MONGODB_VERSION);
try
{
$client = new MongoDB\Driver\Manager(
'mongodb://user:password@cluster0-shard-00-00-tmugt.mongodb.net:27017,cluster0-shard-00-01-tmugt.mongodb.net:27017,cluster0-shard-00-02-tmugt.mongodb.net:27017/testDB?ssl=true&replicaSet=Cluster0-shard-0&authSource=admin&retryWrites=true&w=majority'
);
$writeConcern = new MongoDB\Driver\WriteConcern(MongoDB\Driver\WriteConcern::MAJORITY, 1000);
echo("<br>Client created");
}
catch (\Exception $e)
{
echo "***Client failed*** " . $e->getMessage();
exit();
}
echo("<br>");
//var_dump( $client->getConnections() );
echo("<br>Client: ");
var_dump($client);
echo("<br>");
var_dump(bin2hex(\MongoDB\BSON\fromPHP(['x'=>1])));
var_dump(class_exists('MongoDB\Driver\Manager'));
$bulk = new MongoDB\Driver\BulkWrite(['ordered' => true]);
$bulk->delete([]);
$bulk->insert(['_id' => 1]);
$bulk->insert(['_id' => 2]);
$bulk->insert(['_id' => 3, 'hello' => 'world']);
$bulk->update(['_id' => 3], ['$set' => ['hello' => 'earth']]);
$bulk->insert(['_id' => 4, 'hello' => 'pluto']);
$bulk->update(['_id' => 4], ['$set' => ['hello' => 'moon']]);
$bulk->insert(['_id' => 3]);
$bulk->insert(['_id' => 4]);
$bulk->insert(['_id' => 5]);
try {
$result = $client->executeBulkWrite('db.collection', $bulk, $writeConcern);
}
catch (MongoDB\Driver\Exception\BulkWriteException $e) {
$result = $e->getWriteResult();
// Check if the write concern could not be fulfilled
if ($writeConcernError = $result->getWriteConcernError()) {
printf("%s (%d): %s\n",
$writeConcernError->getMessage(),
$writeConcernError->getCode(),
var_export($writeConcernError->getInfo(), true)
);
}
// Check if any write operations did not complete at all
foreach ($result->getWriteErrors() as $writeError) {
printf("Operation#%d: %s (%d)\n",
$writeError->getIndex(),
$writeError->getMessage(),
$writeError->getCode()
);
}
}
catch (MongoDB\Driver\Exception\Exception $e) {
printf("Other error: %s\n", $e->getMessage());
exit;
}
?>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.