[CXX-676] Cannot connect to replica set using C++ driver Created: 24/Sep/15  Updated: 29/Oct/15  Resolved: 16/Oct/15

Status: Closed
Project: C++ Driver
Component/s: Release
Affects Version/s: legacy-1.0.5
Fix Version/s: None

Type: Bug Priority: Major - P3
Reporter: Peter Assignee: Adam Midvidy
Resolution: Cannot Reproduce Votes: 0
Labels: legacy-cxx
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Attachments: File mongod.conf     File mongod.log     Text File rsStatus.txt    

 Description   

When using legacy C++ driver, cannot connect to replica set servers. Although I can connect to every member separately with no problem. When multiple servers are specified in connection string, empty pointer is returned from cs.connect().

Code snippet:
#include <mongo/client/dbclient.h>
#include <mongo/client/options.h>
#include <mongo/client/init.h>
#include <mongo/bson/bson.h>
#include <iostream>
#include <memory>
#include <iostream>

using namespace mongo;
using namespace std;

int main() {
string rsName = "myProductionSet";
string err;
bool master;

client::Options opt;
client::initialize(opt);
ConnectionString cs = ConnectionString::parse("mongodb://[host1]:27017,[host2]:27117/?replicaSet=myProductionSet",err);

DBClientReplicaSet* client = static_cast<DBClientReplicaSet*>(cs.connect(err));
if(!client)

{ cout << "Empty pointer" << endl; }

if(client->isMaster(master))
std::cout << "OK" << std::endl;
else
std::cout << "ERR" << std::endl;

return 0;
}



 Comments   
Comment by Adam Midvidy [ 29/Oct/15 ]

Hi Peter,

Did you check that the client machine (running the C++ driver) was able to resolve the hostnames of the replica set nodes? This can be a problem when running on AWS, as the seedlist returned by the server to the driver will return the internal (AWS only) DNS address, which will cause the driver to fail to connect.

In the replica set documentation, we remind users to ensure that "each member of a replica set is accessible by way of resolvable DNS or hostnames." (https://docs.mongodb.org/manual/tutorial/deploy-replica-set/). Nonetheless, as this seems like a MongoDB issue (as opposed to a C++ driver issue) I would recommend that you follow up on mongodb-user if you have further questions.

Adam

Comment by Peter [ 27/Oct/15 ]

Looks, like I found the problem - it is described here:
https://www.amido.com/mongodb-unable-to-connect-to-a-member-of-the-replica-set-matching-the-read-preference-primary/

I replicated the steps - namely reconfigured members of replica set not to be identified by hostnames (although DNS was setup and machines were able to communicate using these names), but now containig full host identification: [my service].cloudapp.net:[port] and now the source code works.

Would that be possible to add this information to docs somewhere?

Thanks for your time.

Comment by Peter [ 27/Oct/15 ]

Hi Adam,
sorry for late answer. I have new information : upgraded server to version 3.0.7, updated and recompiled C++ driver version 1.0.6.

  • gcc version 4.8.1 (Ubuntu 4.8.1-2ubuntu1~12.04), using -std=c++11 and -g flags for source compilation
  • scons --dbg=on --c++11=on --sharedclient=SHAREDCLIENT install
  • source code mentioned above, when specified 3 servers in connection string, connect returns empty pointer
  • 3 mongod servers running as replica set with 1 primary
  • replica set is normally running, when inserting data to primary, data is replicated to secondary nodes as expected
  • when connecting only to one node (no replica set in connection string specified), connection works as expected

I also attached configuration and log from mongo server.

Comment by Adam Midvidy [ 16/Oct/15 ]

Hi Peter,

I am going to close this issue out. If you are still having trouble, please re-open it.

Comment by Adam Midvidy [ 02/Oct/15 ]

Also, I noticed you are building the driver with C+11. Are you building your test executable with C+11 as well?

Comment by Adam Midvidy [ 02/Oct/15 ]

Hi Peter,

That error would be hit if none of the replica set nodes are reachable at the time the replica set connection his created. Can you elaborate on the configuration of your replica set? What is the # of servers and what version of MongoDB are they running?

Also, can you attach MongoDB logs for when this connection failure occurs?

Best,
Adam

Comment by Peter [ 29/Sep/15 ]

Hi, the string says: "connect failed to replica set [replica set name]/[host1]:[port1],[host2]:[port2],[host3]:[port3]"
Maybe I also should add, that virtual machines running mongo instances are deployed in Azure.

Comment by Adam Midvidy [ 28/Sep/15 ]

Also, what are the contents of the string 'err' after connection fails?

Comment by Peter [ 28/Sep/15 ]

Authentication is not enabled, I'm waiting for this basic scenario to work first.

Comment by Adam Midvidy [ 25/Sep/15 ]

Hi peto2468@hotmail.com, are you running with auth enabled on your cluster?

Generated at Wed Feb 07 21:59:57 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.