Kunok
Kunok

Reputation: 8759

MongoDB PHP define database connection

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?

Upvotes: 0

Views: 121

Answers (2)

Sitethief
Sitethief

Reputation: 770

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?

Upvotes: 1

Ajitej Kaushik
Ajitej Kaushik

Reputation: 944

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

Use this.

Upvotes: 0

Related Questions