Search code examples
phpmongodbcloudcontrol

Cloudcontrol no MongoDB access


I recently discovered cloudcontrol and wanted to write a little mongoDB app to store some images. I enabled the free addon mongolab.free and followed the documentation to set up a connection.

$credfile = file_get_contents($_ENV['CRED_FILE'], false);
$credentials = json_decode($credfile, true);
$uri = $credentials["MONGOLAB"]["MONGOLAB_URI"];
$m = new Mongo($uri);
$db = $m->selectDB('test');

The problem is that the following line doesn't work because I "Need to login"

$db = $m->selectDB('test');

If I try to list all available DBs via listDBs() I get the same error "Need to login"

Even when connection via the terminal, the connection establishes but with the same "need to login" warning.

I've been trying to fix this problem since three days now but I cannot find the solution :(

Hope someone can help me


Solution

  • The name of your database is defined in MONGOLAB_URI as well (it has the form cloudcontrol_<deployment_id>). So you don't have to explicitely define another database name (and can't).

    You can extract the database name from the uri with

    preg_match("#/([^/\?]+)(\?|$)#", $uri, $match);
    database = $match[1];