Mongodb authentication failed in php7
Hy anyone,
I create new connection to mongodb from php7 but I get error : "authectication fail"
this script create new connection :
$conn = new Client("mongodb://user:password@host:port"); $db = $conn->selectDatabase("DATABASE_NAME"); $collection = $db->selectCollection("COLLECTION_NAME"); $data = $collection->find();
Is there a solution to my problem ? Is there anybody who can help me ?
We can discuss via email, this my email [email protected]
Thank you,
A few things to check would be:
- If the username or password contains special characters (e.g. something other than alpha-numeric characters), you should encode each string with
rawurlencode()as discussed in theMongoDB\Driver\Manager::__construct()andMongoDB\Client::__construct()documentation. Alternatively, you can specify the username and password in the URI options array (second parameter) in lieu of the URI string, which will not require any encoding. - Since you are not specifying an authentication source, the driver defaults to authenticating against the "admin" database. Depending how the user is configured, you may need to specify a difference database via the
authSourceURI option. - By default, the
authMechanismdefaults toSCRAM-SHA-1for MongoDB 3.0+ andMONGODB-CRfor earlier versions. Ensure that this is consistent with your database configuration.
Lastly, I'd suggest attempting to authenticate with identical options and credentials via the mongo shell to rule out any issues with the PHP driver.
Closing this out due to inactivity. Feel free to followup if the issue persists.