0

I am using this approach to connect to remote database: (SSH tunnel is running in background)

$mongo = new Mongo('mongodb://[root:password]@localhost:27018');

However I get error like this:

Fatal error: Uncaught exception 'MongoConnectionException' with message 'Failed to connect to: localhost:27018: SASL Authentication failed on database 'admin': Authentication failed.' in /var/www/html/mongo_seed/index.php:22 Stack trace: #0 /var/www/html/mongo_seed/index.php(22): Mongo->__construct('mongodb://[root...') #1 {main} thrown in /var/www/html/mongo_seed/index.php on line 22

How do I define which database I want to connect to inside parameter string? Or else how do I do the whole thing method by method?

2 Answers 2

1

The MongoDB spec says this:

mongodb://[username:password@]host1[:port1][,host2[:port2],...[,hostN[:portN]]][/[database][?options]]

https://docs.mongodb.com/manual/reference/connection-string/

I think you just have to add /databasename at the end?

Sign up to request clarification or add additional context in comments.

1 Comment

Can you provide link for quoted text?
0

$connection = new MongoClient( "mongodb://[root:password]@localhost:27018" );

Use this.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.