Cannot connect to mongodb database user using php

I have mongod running with auth=true on my server.

If I log in to my administrator (from the admin database), there is no problem getting the data.

 <?php $connection = new Mongo("mongodb://admin: adminpass@127.0.0.1 "); $db = $connection->selectDB( "mydb" ); $collection = $db->selectCollection( "user" ); var_dump($collection->findOne()); ?> 

but if I replaced the first line with

 $connection = new Mongo("mongodb://mydbadmin: dbadminpass@127.0.0.1 :27017"); 

It cannot connect and receive an error message:

 Fatal error: Uncaught exception 'MongoConnectionException' with message 'Couldn't authenticate with database admin: username [mydbadmin]' in ..... 

So the problem is that new Mongo() trying to connect my user to the admin database instead of the "mydb" database. How to choose the database that I want to connect?

EDIT:

according to http://php.net/manual/fr/mongo.construct.php
I tried this

 $login = array("username" => "mydbadmin", "password" => "dbadminpass", "db" => "mydb", "connect" => true ); $connection = new Mongo("mongodb://localhost", $login); 

but

 Couldn't authenticate with database mydb: username [mydbadmin]' in ..... 
+4
source share
2 answers

Well, if you do not specify db in the constructor, it uses the admin database by default (as described in Mongo::__construct -reference). Try the following:

 $connection = new Mongo("mongodb://admin: adminpass@127.0.0.1 /mydb"); 

pay attention to /mydb after the host part, which allows PHP-api to connect to the required database.

+3
source

check php ur driver version and mongod version, both versions must be compatible .. Example: for mine .. im using php 5.3 (wamp) and mongod 3.0 in mlab.com .. my dll version is php_mongo-1.6.6-5.3 -ts-vc9-x86

works well for me

link to this site for mongo driver information

refer to the site https://docs.mongodb.com/ecosystem/drivers/php/ below

link to this site to download mongod drivers https://pecl.php.net/package/mongo/1.6.6/windows

thanks ... sureshapn.m@gmail.com

-1
source

Source: https://habr.com/ru/post/1435458/


All Articles