<?php
/**
* Connects to MongoDB server.
* Stops code execution on`enter code here` connection error.
*
* You do not need to use this file, just any place to globally assign a MongoDB instance to $db.
*
* Define constants in your config or here
*/
define('MDB_USERNAME', 'root');
define('MDB_PASSWORD', 'asd');
define('MDB_HOST', 'localhost:80');
define('MDB_NAME', 'abc');
if (!class_exists('Mongo')) {
die("Mongo class not existing. Did you install the PHP MongoDB extension?");
}
try {
$conn = new MongoClient("mongodb://".MDB_USERNAME.":".MDB_PASSWORD."@".MDB_HOST."/".MDB_NAME);
$conn->authenticate('root','gynadfehurbo');
$db = $conn->selectDB(MDB_NAME);
} catch (MongoConnectionException $e) {
die($e->getMessage()); // In production you might want to turn this off.
}
Mongodb连接问题。
当我试图连接mongodb与php得到身份验证问题。通过端口,上面的错误被替换为连接被拒绝。然后我更改了错误已被替换的端口
连接失败:localhost:80:读取0字节后读取超时,等待60.000000秒
答案 0 :(得分:0)
$server = "mongodb://localhost:27017/dbname"; // Connecting to server
$c = new MongoClient($server );
if($c->connected)
echo "Connected successfully";
else
echo "Connection failed";