connection attempt failed : connect@src/mongo/shell/mongo.js:251:13 @(connect):1:6 exception: connect failed

1,498 views
Skip to first unread message

Monika Shah

unread,
Feb 25, 2018, 11:51:22 PM2/25/18
to mongod...@googlegroups.com
I am using mongoshard.bat file to start mongodb shard server. It was working yesterday, but giving connection error for last few attempt

Getting connection error while executing following batch file.

Error message
============
"Error: couldn't connect to server 127.0.0.1:37017, connection attempt failed :
connect@src/mongo/shell/mongo.js:251:13 @(connect):1:6 exception: connect failed"


mongoshard.bat file
================
set mongo_home_path=C:/Program Files/MongoDB/Server/3.6.2/bin
set root_path=e:/mongodb
set data_path=d:/MongoDB/shard

echo Start 3 mongod for replicatSet #
start /b "s1-a" "%mongo_home_path%/mongod" --replSet s1 --logappend --logpath "%data_path%/s1/a/s1_a.log" --dbpath "%data_path%/s1/a" --port 37017 --oplogSize 200  --shardsvr
start /b "s1-b" "%mongo_home_path%/mongod" --replSet s1 --logappend --logpath "%data_path%/s1/b/s1_b.log" --dbpath "%data_path%/s1/b" --port 37018 --oplogSize 200  --shardsvr
start /b "s1-c" "%mongo_home_path%/mongod" --replSet s1 --logappend --logpath "%data_path%/s1/c/s1_c.log" --dbpath "%data_path%/s1/c" --port 37019 --oplogSize 200  --shardsvr
timeout /t 5

echo Start 3 mongod for replicatSet #
start /b "s2-a" "%mongo_home_path%/mongod" --replSet s2 --logappend --logpath "%data_path%/s2/a/s2_a.log" --dbpath "%data_path%/s2/a" --port 47017 --oplogSize 200  --shardsvr
start /b "s2-b" "%mongo_home_path%/mongod" --replSet s2 --logappend --logpath "%data_path%/s2/b/s2_b.log" --dbpath "%data_path%/s2/b" --port 47018 --oplogSize 200  --shardsvr
start /b "s2-c" "%mongo_home_path%/mongod" --replSet s2 --logappend --logpath "%data_path%/s2/c/s2_c.log" --dbpath "%data_path%/s2/c" --port 47019 --oplogSize 200  --shardsvr
timeout /t 5

echo Start 3 config servers
start /b "cfg-a" "%mongo_home_path%/mongod" --logappend  --logpath "%data_path%/cfg-a/cfg_a.log" --dbpath "%data_path%/cfg-a" --replSet conf --port 57017 --configsvr
start /b "cfg-b" "%mongo_home_path%/mongod" --logappend --logpath "%data_path%/cfg-b/cfg_b.log" --dbpath "%data_path%/cfg-b" --replSet conf  --port 57018 --configsvr
start /b "cfg-c" "%mongo_home_path%/mongod" --logappend --logpath "%data_path%/cfg-c/cfg_c.log" --dbpath "%data_path%/cfg-c" --replSet conf --port 57019 --configsvr
timeout /t 20

echo Configure shard #1
start /b "configure S1" "%mongo_home_path%/mongo" --port 37017 "%root_path%/configS1.js"

echo Configure shard #2
start /b "configure S2" "%mongo_home_path%/mongo" --port 47017 "%root_path%/configS2.js"

start /b "configure shard" "%mongo_home_path%/mongo" --port 57017 "%root_path%/configS.js"

start /b "mongos" "%mongo_home_path%/mongos" --port 61017 --logappend --logpath "%data_path%/mongos.log" --configdb "conf/localhost:57017,localhost:57018,localhost:57019"
start /b "configure shard" "%mongo_home_path%/mongo" --shell --port 61017 "%root_path%/configShard_alt.js"
===================================================================



Where, configS1.js contains
===========================
configS1 = {
     "_id" : "s1",
     "members" : [
        {
           "_id" : 1,
           "host" : "localhost:37017"
        },
        {
           "_id" : 2,
           "host" : "localhost:37018"
        },
        {
           "_id" : 3,
           "host" : "localhost:37019"
 }
     ]
};
rs.initiate(configS1);
===========================
configS.js
======================
rs.initiate(
  {
    _id: "conf",
    configsvr: true,
    members: [
      { _id : 0, host : "localhost:57017" },
      { _id : 1, host : "localhost:57018" },
      { _id : 2, host : "localhost:57019" }
    ]
  }
)
==================================

--
Monika Shah

Kevin Adistambha

unread,
Mar 2, 2018, 12:28:07 AM3/2/18
to mongodb-user

Hi

If you’re looking to automate starting up a MongoDB deployment for testing purposes (i.e. all mongod runs on localhost), then I would encourage you to take a look at mtools, especially the tool mlaunch.

From my understanding, the deployment you posted could be started by mlaunch using the following command:

mlaunch init --sharded 2 --replicaset --config 3

That is, initialize a sharded cluster with 2 shards, where each shard contains a 3-node replica set, and use a 3-node config server.

The sh.status() output of the result is:

mongos> sh.status()
--- Sharding Status ---
  sharding version: {
        "_id" : 1,
        "minCompatibleVersion" : 5,
        "currentVersion" : 6,
        "clusterId" : ObjectId("5a98de77fc15b1d0705111b5")
}
  shards:
        {  "_id" : "shard01",  "host" : "shard01/localhost:27018,localhost:27019,localhost:27020",  "state" : 1 }
        {  "_id" : "shard02",  "host" : "shard02/localhost:27021,localhost:27022,localhost:27023",  "state" : 1 }
  active mongoses:
        "3.4.9" : 1
 autosplit:
        Currently enabled: yes
  balancer:
        Currently enabled:  yes
        Currently running:  no
                Balancer lock taken at Thu Mar 01 2018 21:17:45 GMT-0800 (Pacific Standard Time) by ConfigServer:Balancer
        Failed balancer rounds in last 5 attempts:  0
        Migration Results for the last 24 hours:
                No recent migrations
  databases:

Please refer to the installation instruction to install mtools and the mlaunch manual for operating mlaunch.

Best regards
Kevin

Monika Shah

unread,
Mar 29, 2018, 1:41:49 PM3/29/18
to mongodb-user
Thank you

Monika Shah

unread,
Apr 5, 2018, 1:08:50 PM4/5/18
to mongodb-user

Thank you, but can you help me to find solution of this error

On Friday, March 2, 2018 at 10:58:07 AM UTC+5:30, Kevin Adistambha wrote:

Monika Shah

unread,
Apr 5, 2018, 1:36:41 PM4/5/18
to mongodb-user
It was working fine for few days. suddenly it started giving this error.
mongos log shows message
"Unable to reach primary for set" conf


I am surprised to see such uncertain behavior. It works fine sometime , and give error sometime.
What is cause and solution of this error?


On Friday, March 2, 2018 at 10:58:07 AM UTC+5:30, Kevin Adistambha wrote:

Kevin Adistambha

unread,
Apr 17, 2018, 1:18:15 AM4/17/18
to mongodb-user

Hi

“Unable to reach primary for set” conf

This means that the mongos process cannot connect to the primary of the shard replica set.

The full message in your mongos log generally look like this:

2018-04-17T15:13:41.328+1000 W NETWORK  [conn6] Unable to reach primary for set shard01
2018-04-17T15:13:41.831+1000 W NETWORK  [conn6] Failed to connect to 127.0.0.1:27018, in(checking socket for error after poll), reason: Connection refused

In the example above, the message Connection refused was due to the mongod process of the shard server is not running. Your actual message might be different, and would provide a hint of why mongos cannot connect to the relevant server.

Typically there are two possible causes:

  1. The primary node of the shard is actually offline
  2. The machine running the mongos process cannot reach the shard due to some network issues

Could you confirm that all the mongod process (shard servers, config servers) are up and running, and there is no network connectivity issues between the nodes in your cluster?

Best regards
Kevin

Reply all
Reply to author
Forward
0 new messages