0

I'm having troubles connecting to replica set from my c++ application. Everything is fine if connecting to single mongo instance. But the application crashes if trying to connect to replica set. "Crashes" means that the process just disappears after entering into ScopedDbConnection::getScopedDbConnection.

Below is my code. It was compiled on EC2 instance running Amazon linux with g++ compiler. I come from Windows world and don't know how to extract more information regarding the crash (for example, stack).

void run()
{
    syslog(LOG_INFO, "Before connection");

    // scoped_ptr<ScopedDbConnection> conn( ScopedDbConnection::getScopedDbConnection("54.83.49.200")); // works just fine

    // next line causes a crash
    scoped_ptr<ScopedDbConnection> conn( ScopedDbConnection::getScopedDbConnection("myreplset/54.83.49.200,54.83.53.241,54.83.52.158"));

    DBClientBase * client = conn->get();

    syslog(LOG_INFO, "After connection"); // newer happens if connecting to replica set 

    do_something(client);

    conn->done();

}

MongoDB servers installed on Amazon EC2 from pre-configured images (AMI) provided by 10gen and have version 2.4.9 The only change was setting up the replica set.

C++ driver compiled from MongoDB source version 2.4.9. Boost version is 1.53.

Replica set configuration:

myreplset:PRIMARY> rs.status()
{
        "set" : "myreplset",
        "date" : ISODate("2014-03-26T22:15:11Z"),
        "myState" : 1,
        "members" : [
                {
                        "_id" : 0,
                        "name" : "54.83.49.200:27017",
                        "health" : 1,
                        "state" : 1,
                        "stateStr" : "PRIMARY",
                        "uptime" : 595,
                        "optime" : Timestamp(1395872014, 1),
                        "optimeDate" : ISODate("2014-03-26T22:13:34Z"),
                        "self" : true
                },
                {
                        "_id" : 1,
                        "name" : "54.83.53.241:27017",
                        "health" : 1,
                        "state" : 2,
                        "stateStr" : "SECONDARY",
                        "uptime" : 95,
                        "optime" : Timestamp(1395872014, 1),
                        "optimeDate" : ISODate("2014-03-26T22:13:34Z"),
                        "lastHeartbeat" : ISODate("2014-03-26T22:15:10Z"),
                        "lastHeartbeatRecv" : ISODate("2014-03-26T22:15:11Z"),
                        "pingMs" : 1,
                        "syncingTo" : "54.83.49.200:27017"
                },
                {
                        "_id" : 2,
                        "name" : "54.83.52.158:27017",
                        "health" : 1,
                        "state" : 7,
                        "stateStr" : "ARBITER",
                        "uptime" : 93,
                        "lastHeartbeat" : ISODate("2014-03-26T22:15:10Z"),
                        "lastHeartbeatRecv" : ISODate("2014-03-26T22:15:10Z"),
                        "pingMs" : 1
                }
        ],
        "ok" : 1
}

Firewall seems to be tuned correctly.

Any help is very much appreciated.

4

1 回答 1

-1

您可以添加端口信息并尝试:

 scoped_ptr<ScopedDbConnection> conn( ScopedDbConnection::getScopedDbConnection("myreplset/54.83.49.200:27017,54.83.53.241:27017,54.83.52.158:27017")); 

参考client/dbclientinterface.h:ConnectionString 处理解析连接mongo的不同方式并确定方法foo/server:port,server:port SET

于 2014-10-10T09:43:18.350 回答