2017-05-03 68 views
0

Windows 7中,msys2,編譯器的MinGW無法從C++代碼

試圖連接到從C++代碼的MongoDB實例連接到MongoDB的。下一個命令運行mongod

mongod --dbpath ./dev-db --bind_ip 127.0.0.1 --port 27017 

我可以正常通過mongo和Robomongo連接到這個數據庫中。

對於C++代碼,我成功編譯並安裝了上一個穩定的Mongo C++ Driver。代碼我從官方tutorial

#include <mongocxx/client.hpp> 
#include <mongocxx/instance.hpp> 
#include <mongocxx/uri.hpp> 

mongocxx::uri uri("mongodb://127.0.0.1:27017"); 
mongocxx::client conn{uri}; 
auto db = conn["test"]; 

bsoncxx::document::value restaurant_doc = 
    document{} << "address" << open_document << "street" 
       << "2 Avenue" 
       << "zipcode" 
       << "10075" 
       << "building" 
       << "1480" 
       << "coord" << open_array << -73.9557413 << 40.7720266 << close_array 
       << close_document << "borough" 
       << "Manhattan" 
       << "cuisine" 
       << "Italian" 
       << "grades" << open_array << open_document << "date" 
       << bsoncxx::types::b_date{std::chrono::milliseconds{12323}} << "grade" 
       << "A" 
       << "score" << 11 << close_document << open_document << "date" 
       << bsoncxx::types::b_date{std::chrono::milliseconds{121212}} << "grade" 
       << "B" 
       << "score" << 17 << close_document << close_array << "name" 
       << "Vella" 
       << "restaurant_id" 
       << "41704620" << finalize; 
auto res = db["restaurants"].insert_one(std::move(restaurant_doc)) 

這提供了一個錯誤:

This application has requested the Runtime to terminate it in an unusual way. 
Please contact the application's support team for more information. 
terminate called after throwing an instance of 'mongocxx::v_noabi::bulk_write_exception' 
    what(): No suitable servers found (`serverSelectionTryOnce` set): [Failed to resolve '127.0.0.1']: generic server error 

我如何才能避免這一問題,並連接到數據庫?

回答

0

它看起來像是嘗試DNS查找127.0.0.1。試着改變它是localhost,而不是(這將解析到同一個IP)

mongocxx::uri uri("mongodb://localhost:27017"); 
+0

使用localhost不起作用。它只是顯示'[無法解析'localhost']'而是。 – Serbin