Connect to MongoDB on Appfog from localhost

248 Views Asked by At

I'm trying to run my application on my local pc. Everything looks perfectly fine on the cloud, it works and connects to MongoDB with the code below:

    $services_json = json_decode(getenv("VCAP_SERVICES"),true);
$mongo_config = $services_json["mongodb-1.8"][0]["credentials"];

// Generally will be localhost if you're querying from the machine that Mongo is installed on
$config['mongo_host'] = $mongo_config["hostname"];

// Generally will be 27017 unless you've configured Mongo otherwise
$config['mongo_port'] = $mongo_config["port"];

// The database you want to work from (required)
$config['mongo_db'] = $mongo_config["db"];

// Leave blank if Mongo is not running in auth mode
$config['mongo_user'] = $mongo_config["username"];
$config['mongo_pass'] = $mongo_config["password"];

But since I cannot get the vcap_services from my local pc, I'm using constant values for the connection to connect my app to the remote mongo server on appfog:

    // Generally will be localhost if you're querying from the machine that Mongo is installed on
$config['mongo_host'] = "***";

// Generally will be 27017 unless you've configured Mongo otherwise
$config['mongo_port'] = "***";

// The database you want to work from (required)
$config['mongo_db'] = "db";

// Leave blank if Mongo is not running in auth mode
$config['mongo_user'] = "***";
$config['mongo_pass'] = "***";

But when I try to execute the app through http://localhost/my-app, it gave me this error:

Unable to connect to MongoDB: Failed to connect to: ***: Connection timed out

What can be the problem?

* values are deleted for the privacy.

1

There are 1 best solutions below

1
On BEST ANSWER

Did you try connecting without the VCAP variables not on localhost but the actual server? AppFog may not give authorization to connect without VCAP variables or something may be wrong on the values you manually get.

I believe this is an authorization problem. Try another cloud service if you cannot handle with it. This is not the only solution.