[CSHARP-255] SocketException on MongoServer.Connect() Created: 24/Jun/11  Updated: 02/Apr/15  Resolved: 04/Jul/11

Status: Closed
Project: C# Driver
Component/s: None
Affects Version/s: 1.1
Fix Version/s: 1.1, 1.2

Type: Bug Priority: Major - P3
Reporter: John Woakes Assignee: Robert Stam
Resolution: Done Votes: 0
Labels: connection
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified
Environment:

Running on Azure connecting to a local VM running 3 mongod.exe as replica sets



 Description   

This mongo url fails with the 1.1 c# driver and works with the 1.0 driver

mongodb://xxxx:40000,xxxx:40001,xxxx:40002/?ipv6=true;replicaSet=xq;safe=true

System.Net.Sockets.SocketException: No connection could be made
because the target machine actively refused it [::1]:40000
at System.Net.Sockets.Socket.DoConnect(EndPoint endPointSnapshot,
SocketAddress socketAddress)
at System.Net.Sockets.Socket.Connect(EndPoint remoteEP)
at System.Net.Sockets.TcpClient.Connect(IPEndPoint remoteEP)
at MongoDB.Driver.Internal.MongoConnection.Open() in
C:\work\10gen\mongodb\mongo-csharp-driver\Driver\Internal\MongoConnection.cs:line
266
at MongoDB.Driver.Internal.MongoConnection.GetNetworkStream() in
C:\work\10gen\mongodb\mongo-csharp-driver\Driver\Internal\MongoConnection.cs:line
409
at MongoDB.Driver.Internal.MongoConnection.SendMessage(MongoRequestMessage
message, SafeMode safeMode) in
C:\work\10gen\mongodb\mongo-csharp-driver\Driver\Internal\MongoConnection.cs:line
403
at MongoDB.Driver.Internal.MongoConnection.RunCommand(String
collectionName, QueryFlags queryFlags, CommandDocument command) in
C:\work\10gen\mongodb\mongo-csharp-driver\Driver\Internal\MongoConnection.cs:line
296
at MongoDB.Driver.MongoServerInstance.Connect(Boolean slaveOk) in
C:\work\10gen\mongodb\mongo-csharp-driver\Driver\Core\MongoServerInstance.cs:line
254
at MongoDB.Driver.Internal.ReplicaSetConnector.ConnectWorkItem(Object
argsObject) in C:\work\10gen\mongodb\mongo-csharp-driver\Driver\Internal\ReplicaSetConnector.cs:line
100
— End of inner exception stack trace —
at MongoDB.Driver.Internal.ReplicaSetConnector.Connect(TimeSpan
timeout) in C:\work\10gen\mongodb\mongo-csharp-driver\Driver\Internal\ReplicaSetConnector.cs:line
87
at MongoDB.Driver.MongoServer.Connect(TimeSpan timeout) in
C:\work\10gen\mongodb\mongo-csharp-driver\Driver\Core\MongoServer.cs:line
435
at MongoDB.Driver.MongoServer.Connect() in
C:\work\10gen\mongodb\mongo-csharp-driver\Driver\Core\MongoServer.cs:line
415
at IQ.IR.Data.Configuration.GetMongoDB(String connection) in
C:\Dev\XQ.Service\IQ.IR.Data\Configuration.cs:line 52

This is the code...

static MongoDatabase GetMongoDB(string connection)
{
const string testBox = "xxxx";
var addr1 = new MongoServerAddress(testBox, 40000);
var addr2 = new MongoServerAddress(testBox, 40001);
var addr3 = new MongoServerAddress(testBox, 40002);
MongoUrlBuilder url = new MongoUrlBuilder
{
IPv6 = true,
SafeMode = SafeMode.True,
Servers = new List<MongoServerAddress>

{ addr1, addr2, addr3 }

,
ConnectionMode = ConnectionMode.ReplicaSet,
ReplicaSetName = "xq"
};

try

{ var mongo = MongoServer.Create(url.ToMongoUrl()); mongo.Connect(); return mongo.GetDatabase("xq"); }

catch (Exception ex)
{
throw new Exception(string.Format("<<

{0}

>>

{1}

",
url.ToMongoUrl(), ex));
}
}

I put a test in to see if the Host value resolves to an IP and it does (IPv6).



 Comments   
Comment by John Woakes [ 05/Jul/11 ]

In my case IPv6 is the only option. I had ipv6=true and used ipv6 addresses everywhere and it didn't work. But I am actually happier using machine names as they are less likely to change.

Comment by Robert Stam [ 05/Jul/11 ]

I think you will have to be consistent and use either IPv4 or IPv6 addresses throughout. And your driver connection string should have ipv6=true if you are using IPv6 addresses.

Comment by John Woakes [ 04/Jul/11 ]

I guess the question is should it work with IP addresses? I only tested IPv6 but I wonder if a regular IP address would work...

Comment by Robert Stam [ 04/Jul/11 ]

Thanks for letting us know, and I'm glad it's working for you now.

Comment by John Woakes [ 04/Jul/11 ]

It is fully working now. When initializing the replica sets you have to use host names, not localhost or IPv6 addresses.

Comment by John Woakes [ 28/Jun/11 ]

I think I know what is happening here. When I set up the test server running mongod I set up 3 instances on the same machine and configured replication using localhost:port. If I look at rs.status() I see the names in members are localhost:port. I think the driver picks these up and tries to use them. I am in the process of trying to fix this or start afresh. I will let you know how I get on.

Comment by John Woakes [ 27/Jun/11 ]

I added a call to this just before the failing connect

private static void OutputServerDetails(MongoServer mongo)
{
var result = string.Empty;
foreach (var address in mongo.Settings.Servers)
{
result += string.Format("

{0}

{1}

", address.Host, address.Port);
try

{ result += address.ToIPEndPoint(AddressFamily.InterNetworkV6); }

catch (Exception ex)

{ result += ex.Message; }

result += "; ";
}
throw new Exception(result);
}

and got

System.Exception: IQ-xxxxx002.iQmetrixHO.local 40000 [2a01:111:f102:122:cc28:398f:262c:3333]:40000; IQ-xxxxx002.iQmetrixHO.local 40001 [2a01:111:f102:122:cc28:398f:262c:3333]:40001; IQ-xxxxx002.iQmetrixHO.local 40002 [2a01:111:f102:122:cc28:398f:262c:3333]:40002;

so it looks like the host name resolves...

Comment by John Woakes [ 27/Jun/11 ]

I had it working with v1.0...

We are running the client in Microsoft's Azure cloud platform and the mongod is running in our local data center. I will continue digging to see if I can provide more information.

Comment by Robert Stam [ 27/Jun/11 ]

Odd then that the error message had "[::1]:40000" as the address...

If the client and server are in different data centers, could it be a firewall issue?

Comment by John Woakes [ 27/Jun/11 ]

I am not running the client and mongod on the same machine. The xxxx machine is in a different data center. Using localhost would not work. If I do a System.Net.Dns.GetHostAddresses("xxxx") from the client I get one valid IPv6 address (not localhost).

I am running my mongod with the --ipv6 flag.

Comment by Robert Stam [ 24/Jun/11 ]

I also get the same results with either the 1.0 or the 1.1 version of the driver.

Comment by Robert Stam [ 24/Jun/11 ]

FYI, I set up a 3 member replica set and was able to connect to it using essentially the same connection string as yours:

var server = MongoServer.Create("mongodb://localhost:10001,localhost:10002,localhost:10003/?safe=true;replicaset=myset;ipv6=true");

Again, I could only connect if I had used --ipv6 when starting mongod.

Comment by Robert Stam [ 24/Jun/11 ]

Not that it necessarily explains your exception, but when I launch mongod without -ipv6 and then put "ipv6=true" in the client connection string I do get the same exception you are reporting.

Comment by Robert Stam [ 24/Jun/11 ]

The IPv6 address [::1] is localhost, so I'm assuming that even though you are using the host name "xxxx" your client and mongod servers are all running on the same box.

A connection refused exception implies that the host name was resolved correctly, but that there was no server listening on that host/port combination.

Did you launch mongod with the --ipv6 command line parameter? mongod won't listen on IPv6 addresses unless you ask it to.

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