0

我正在尝试在我的本地电脑上运行我的应用程序。云上的一切看起来都很好,它可以工作并使用以下代码连接到 MongoDB:

    $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"];

但是由于我无法从本地 pc 获取 vcap_services,因此我使用常量值进行连接,以将我的应用程序连接到 appfog 上的远程 mongo 服务器:

    // 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'] = "***";

但是当我尝试通过 执行应用程序时http://localhost/my-app,它给了我这个错误:

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

可能是什么问题?

*出于隐私考虑,已删除值。

4

1 回答 1

1

您是否尝试在没有 VCAP 变量的情况下连接,而不是在本地主机上而是在实际服务器上?如果没有 VCAP 变量,AppFog 可能无法授权进行连接,或者您手动获取的值可能有问题。

我相信这是一个授权问题。如果您无法处理,请尝试其他云服务。这不是唯一的解决方案。

于 2013-10-03T14:33:45.823 回答