http://uk3.php.net/manual/en/mongoclient.construct.php you can pass the username and password as an options array
new MongoClient("mongodb://localhost:27017", array("username" => "joe", "password" => "test"));
Вопрос
I've been trying to experiment with MongoDb, and when I run the script to connect, it can't because the password uses the @
symbol.
To connect to MongoDb
mongodb://username:password@localhost
But as my password to connect to MongoDb uses the @
symbol, it can't connect due to the @
symbol.
For example;
new MongoClient("mongodb://mongo:p@assword@localhost);
Will throw the following error;
PHP Fatal error: Uncaught exception 'MongoConnectionException' with message 'Failed to connect to: assword!@localhost:27017: Couldn't get host info for assword!@localhost'
As you can see, it thinks the first @
in the password is the seperator for password and host.
Is there a way to connect, and allow the @
symbol in the password when using MongoClient?
Решение
http://uk3.php.net/manual/en/mongoclient.construct.php you can pass the username and password as an options array
new MongoClient("mongodb://localhost:27017", array("username" => "joe", "password" => "test"));
Другие советы
You can try by this code. Just pass your input as per dynamic value.
try {
$m = new MongoClient("mongodb://HOST:" . MNG_PORT, array(
"username" => MNG_USER,
"password" => MNG_PWD
)); // connect local Mongo
$db = $m->selectDB("DBNAME"); // select DB
$collection = $db->collections; // connect to collection
} catch ( MongoConnectionException $e ) {
echo 'Couldn\'t connect to mongodb, is the "mongo" process running?';
exit();
}