connect failed to replica set

886 views
Skip to first unread message

Mahesh

unread,
Jan 29, 2016, 2:45:17 PM1/29/16
to mongodb-user
Hi , 

I am trying to understand replica set in MongoDB. I am running  example C++ Programs given at mongo-cxx-driver-legacy-1.1.0_source\src\mongo\client\examples\rs.cpp

1) After compilation, I ran the Program but  I got error -  connect failed to replica set foo//127.0.0.1:27017

Ex source code is:

----------------------  ------  -------------- -------
mongo::client::GlobalInstance instance;
if (!instance.initialized()) {
std::cout << "failed to initialize the client driver: " << instance.status() << endl;
return EXIT_FAILURE;
}

string errmsg;
ConnectionString cs = ConnectionString::parse("mongodb://127.0.0.1/?replicaSet=foo", errmsg);

if (!cs.isValid()) {
cout << "error parsing url: " << errmsg << endl;
return EXIT_FAILURE;
}

DBClientReplicaSet* conn =
static_cast<DBClientReplicaSet*>(cs.connect(errmsg, testTimeout ? 10 : 0));
if (!conn) {
cout << "error connecting: " << errmsg << endl;
return EXIT_FAILURE;
}

-----------------------------------

1)Can you Please help me on this? 

other info:

mongodb running on localhost:27017.

Is there any online documentation to understand replica set using C++ Driver/C++ Application?

Thanks.
Mahesh


Wan Bachtiar

unread,
Jan 30, 2016, 8:28:20 AM1/30/16
to mongodb-user

Hi Mahesh,

I have just tested the example source code, and it successfully connected to a replica set called ‘foo’ to a primary node running on localhost.

Could you connect to the replica set via the Mongo Shell, run rs.status() and post the output of it ? This is to confirm that you have a replica set foo running.

Also, which MongoDB version are you running ?

You may also find these useful:

Regards,

Wan.

张友东

unread,
Feb 1, 2016, 12:46:14 AM2/1/16
to mongodb-user
Hi, Mahesh

what's the error message of you code?

can you successfully connected to  "127.0.0.1:27017" using mongo shell?

Maybe your mongod have not bind with 127.0.0.1,you need to use host ip address to connect.

在 2016年1月30日星期六 UTC+8上午3:45:17,Mahesh写道:

Mahesh

unread,
Feb 1, 2016, 1:28:58 PM2/1/16
to mongodb-user
Hi ,

I am running 3.2 version. installed @ C:\Program Files\MongoDB\Server\3.2\bin\mongod.exe  in my system. I just tried connecting to replica set through mongo shell but it seems it thorows some errors. PFA screen shot for output I got after running,

rs.initiate(), rs.conf() and rs.status(). 


2) I am using VS2013 for compilation and output . program compiling successfully but after running it I got the below error as output.

. error connecting:connect failed to replica set foo//127.0.0.1:27017

-----
rs_error.PNG

Wan Bachtiar

unread,
Feb 1, 2016, 5:47:03 PM2/1/16
to mongodb-user

Hi Mahesh,

Based on the error message on the screenshot, it shows that you are not running with --replSet. This is why the rs.initiate() command failed to initiate a replica set. See Deploy a Replica Set for steps and guides on how to deploy a replica set.


I am using VS2013 for compilation and output . program compiling successfully but after running it I got the below error as output.

I would suggest to focus on getting a replica set configured properly first. As currently the issue is there is no replica set called foo running on localhost:27017. 

Also, note that you need to run rs.status() with parentheses () to actually execute on mongo shell.

You may also find Replication Introduction useful.

Kind regards,

Wan.

Mahesh

unread,
Feb 24, 2016, 11:38:34 AM2/24/16
to mongodb-user
Hi Wan,

  I resolved the issue by running --replSet option. now everything okay. Thank's for your previous responses. 
  I have got stuck at adding replica set members into already existing replica set. let me explain what I did,

1) I have installed MongoDB in Windows and created RepSet(rs0). it is working fine. I see rs.status(), rs.config(). this is Primary now.
2) I have installed MongoDb, created Replica set with same name(rs0) in CentOS , which is iso image running on VMWare Player on my local windows .
3) for both Windows and CentOS, the bindIP is 127.0.0.1 and port is 27017.both servers running individually and they are good.
4) Now, I want to add CentOS replica set member as secondary into my primary replica set running in Windows. I tried adding rs.add("IP address of centos:portno'). but I could not able to connect.

it seems I need to change some network configuration in CentOS. I am not sure how to connect/add . 

my CentOS  /etc/hosts file looks like this,

127.0.0.1  localhost.localdoman localhost
10.137.101.XXX centoshost  # I added this IP and this is the IP address of centOS server.

the problem is, I can't connect/add centOS replica set from Windows replica set. I hope you understand my problem. please let me know if you need any info. Thank you.


-MAhesh

Wan Bachtiar

unread,
Feb 24, 2016, 7:53:18 PM2/24/16
to mongodb-user

Hi Mahesh,

You need to verify connections in both “directions”, as networking topologies and firewall configurations can prevent connectivity.

I would suggest to try to ping the guest (CentOS) from the host (Windows), and vice versa. If the pings are not successful then you would have to configure your VMware settings for the network connection.

You may also find these VMware knowledge base useful:

Or perhaps the answer for this question StackExchange: CentOS no network interface after installation in virtual box  may be useful as well.

Note that this is not really related to MongoDB itself, and you may get a wider audience by posting a question on ServerFault, SuperUser or similar sites.

Kind regards,
Wan.

fryj00

unread,
Feb 25, 2016, 5:00:58 PM2/25/16
to mongodb-user
I believe you need to specify the replica set name and all members and their ports in your connection string, though, I don't know how to express that in C++ terms. My php driver looks like this:

$MONGO["servers"][$i]["mongo_options"] = array('replicaSet' => 'rs1');//mongo server name
$MONGO["servers"][$i]["mongo_name"] = "localhost";//mongo server name
$MONGO["servers"][$i]["mongo_host"] = "127.0.0.1:27017,127.0.0.1:27018";//mongo host
$MONGO["servers"][$i]["mongo_port"] = false;//mongo port


FYI, I was using multiple instances running on the same box with different ports. "rs1" is my replica set name
Reply all
Reply to author
Forward
0 new messages