我試圖在本地PC上運行我的應用程序。一切看起來都在雲上完全沒有問題,它的工作原理,並連接到MongoDB的用下面的代碼:從本地主機連接到Appfog上的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遠程服務器蒙哥:
// 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
可能是什麼問題?
*值的隱私被刪除。
你能連接到'http:// localhost:28017 /'嗎? –
是的,但主要問題不在於localhost。我有另一個主機,例如10。*。*。*,我需要在本地appfog上連接。 – gzg