title: MongoDB java driver - com.mongodb.MongoSocketReadTimeoutException: Timeout while receiving message
body: <p>Having some issues when trying to connect to a local instance of MongoDB. Stack trace below:</p>
  
  <pre><code>    Feb 07, 2018 5:29:06 PM com.mongodb.diagnostics.logging.JULLogger log
  INFO: Cluster created with settings {hosts=[127.0.0.1:27017], mode=SINGLE, requiredClusterType=UNKNOWN, serverSelectionTimeout='30000 ms', maxWaitQueueSize=500}
  Feb 07, 2018 5:29:07 PM com.mongodb.diagnostics.logging.JULLogger log
  INFO: Cluster description not yet available. Waiting for 30000 ms before timing out
  Feb 07, 2018 5:29:27 PM com.mongodb.diagnostics.logging.JULLogger log
  INFO: Exception in monitor thread while connecting to server 127.0.0.1:27017
  com.mongodb.MongoSocketReadTimeoutException: Timeout while receiving message
      at com.mongodb.connection.InternalStreamConnection.translateReadException(InternalStreamConnection.java:530)
      at com.mongodb.connection.InternalStreamConnection.receiveMessage(InternalStreamConnection.java:421)
      at com.mongodb.connection.InternalStreamConnection.receiveCommandMessageResponse(InternalStreamConnection.java:290)
      at com.mongodb.connection.InternalStreamConnection.sendAndReceive(InternalStreamConnection.java:255)
      at com.mongodb.connection.CommandHelper.sendAndReceive(CommandHelper.java:84)
      at com.mongodb.connection.CommandHelper.executeCommand(CommandHelper.java:34)
      at com.mongodb.connection.InternalStreamConnectionInitializer.initializeConnectionDescription(InternalStreamConnectionInitializer.java:91)
      at com.mongodb.connection.InternalStreamConnectionInitializer.initialize(InternalStreamConnectionInitializer.java:51)
      at com.mongodb.connection.InternalStreamConnection.open(InternalStreamConnection.java:127)
      at com.mongodb.connection.DefaultServerMonitor$ServerMonitorRunnable.run(DefaultServerMonitor.java:114)
      at java.lang.Thread.run(Thread.java:745)
  Caused by: java.net.SocketTimeoutException: Read timed out
      at java.net.SocketInputStream.socketRead0(Native Method)
      at java.net.SocketInputStream.socketRead(SocketInputStream.java:116)
      at java.net.SocketInputStream.read(SocketInputStream.java:170)
      at java.net.SocketInputStream.read(SocketInputStream.java:141)
      at com.mongodb.connection.SocketStream.read(SocketStream.java:84)
      at com.mongodb.connection.InternalStreamConnection.receiveResponseBuffers(InternalStreamConnection.java:547)
      at com.mongodb.connection.InternalStreamConnection.receiveMessage(InternalStreamConnection.java:418)
      ... 9 more
  
  com.mongodb.MongoTimeoutException: Timed out after 30000 ms while waiting to connect. Client view of cluster state is {type=UNKNOWN, servers=[{address=127.0.0.1:27017, type=UNKNOWN, state=CONNECTING, exception={com.mongodb.MongoSocketReadTimeoutException: Timeout while receiving message}, caused by {java.net.SocketTimeoutException: Read timed out}}]
      at com.mongodb.connection.BaseCluster.getDescription(BaseCluster.java:167)
      at com.mongodb.Mongo.getConnectedClusterDescription(Mongo.java:881)
      at com.mongodb.Mongo.createClientSession(Mongo.java:873)
      at com.mongodb.Mongo$3.getClientSession(Mongo.java:862)
      at com.mongodb.Mongo$3.execute(Mongo.java:830)
      at com.mongodb.MongoCollectionImpl.executeSingleWriteRequest(MongoCollectionImpl.java:1025)
      at com.mongodb.MongoCollectionImpl.executeInsertOne(MongoCollectionImpl.java:513)
      at com.mongodb.MongoCollectionImpl.insertOne(MongoCollectionImpl.java:493)
      at com.mongodb.MongoCollectionImpl.insertOne(MongoCollectionImpl.java:487)
      at database.Setup.insertAll(Setup.java:46)
      at database.Setup.main(Setup.java:64)
  </code></pre>
  
  <p>I'm using windows 10, downloaded and installed MongoDB, created the data directory and started mongo from the command line.</p>
  
  <p>I can connect using the mongo shell and list details:</p>
  
  <pre><code> db.hostInfo();
  {
          "system" : {
                  "currentTime" : ISODate("2018-02-07T04:32:08.428Z"),
                  "hostname" : "DESKTOP-KH7KS0F",
                  "cpuAddrSize" : 64,
                  "memSizeMB" : 16294,
                  "numCores" : 12,
                  "cpuArch" : "x86_64",
                  "numaEnabled" : false
          },
          "os" : {
                  "type" : "Windows",
                  "name" : "Microsoft Windows 10",
                  "version" : "10.0 (build 16299)"
          },
          "extra" : {
                  "pageSize" : NumberLong(4096)
          },
          "ok" : 1
  }
  &gt; db.stats()
  {
          "db" : "test",
          "collections" : 0,
          "views" : 0,
          "objects" : 0,
          "avgObjSize" : 0,
          "dataSize" : 0,
          "storageSize" : 0,
          "numExtents" : 0,
          "indexes" : 0,
          "indexSize" : 0,
          "fileSize" : 0,
          "fsUsedSize" : 0,
          "fsTotalSize" : 0,
          "ok" : 1
  }
  &gt; db.version()
  3.6.2
  </code></pre>
  
  <p>I'm using the synchronous driver and added it as a maven dependency:</p>
  
  <pre><code>    &lt;dependency&gt;
          &lt;groupId&gt;org.mongodb&lt;/groupId&gt;
          &lt;artifactId&gt;mongodb-driver&lt;/artifactId&gt;
          &lt;version&gt;3.6.2&lt;/version&gt;
      &lt;/dependency&gt;
  </code></pre>
  
  <p>This is the output of running Mongo from command line:</p>
  
  <pre><code>2018-02-06T19:59:53.808-0700 I CONTROL  [initandlisten] MongoDB starting : pid=14308 port=27017 dbpath=C:\data\db\ 64-bit host=DESKTOP-KH7KS0F
  2018-02-06T19:59:53.808-0700 I CONTROL  [initandlisten] targetMinOS: Windows 7/Windows Server 2008 R2
  2018-02-06T19:59:53.808-0700 I CONTROL  [initandlisten] db version v3.6.2
  2018-02-06T19:59:53.808-0700 I CONTROL  [initandlisten] git version: 489d177dbd0f0420a8ca04d39fd78d0a2c539420
  2018-02-06T19:59:53.809-0700 I CONTROL  [initandlisten] OpenSSL version: OpenSSL 1.0.1u-fips  22 Sep 2016
  2018-02-06T19:59:53.809-0700 I CONTROL  [initandlisten] allocator: tcmalloc
  2018-02-06T19:59:53.810-0700 I CONTROL  [initandlisten] modules: none
  2018-02-06T19:59:53.810-0700 I CONTROL  [initandlisten] build environment:
  2018-02-06T19:59:53.810-0700 I CONTROL  [initandlisten]     distmod: 2008plus-ssl
  2018-02-06T19:59:53.810-0700 I CONTROL  [initandlisten]     distarch: x86_64
  2018-02-06T19:59:53.811-0700 I CONTROL  [initandlisten]     target_arch: x86_64
  2018-02-06T19:59:53.811-0700 I CONTROL  [initandlisten] options: {}
  2018-02-06T19:59:53.814-0700 I STORAGE  [initandlisten] wiredtiger_open config: create,cache_size=7635M,session_max=20000,eviction=(threads_min=4,threads_max=4),config_base=false,statistics=(fast),log=(enabled=true,archive=true,path=journal,compressor=snappy),file_manager=(close_idle_time=100000),statistics_log=(wait=0),verbose=(recovery_progress),
  2018-02-06T19:59:53.875-0700 I CONTROL  [initandlisten]
  2018-02-06T19:59:53.875-0700 I CONTROL  [initandlisten] ** WARNING: Access control is not enabled for the database.
  2018-02-06T19:59:53.877-0700 I CONTROL  [initandlisten] **          Read and write access to data and configuration is unrestricted.
  2018-02-06T19:59:53.878-0700 I CONTROL  [initandlisten]
  2018-02-06T19:59:53.878-0700 I CONTROL  [initandlisten] ** WARNING: This server is bound to localhost.
  2018-02-06T19:59:53.878-0700 I CONTROL  [initandlisten] **          Remote systems will be unable to connect to this server.
  2018-02-06T19:59:53.878-0700 I CONTROL  [initandlisten] **          Start the server with --bind_ip &lt;address&gt; to specify which IP
  2018-02-06T19:59:53.879-0700 I CONTROL  [initandlisten] **          addresses it should serve responses from, or with --bind_ip_all to
  2018-02-06T19:59:53.879-0700 I CONTROL  [initandlisten] **          bind to all interfaces. If this behavior is desired, start the
  2018-02-06T19:59:53.879-0700 I CONTROL  [initandlisten] **          server with --bind_ip 127.0.0.1 to disable this warning.
  2018-02-06T19:59:53.880-0700 I CONTROL  [initandlisten]
  2018-02-06T19:59:53.880-0700 I CONTROL  [initandlisten]
  2018-02-06T19:59:53.880-0700 I CONTROL  [initandlisten] ** WARNING: The file system cache of this machine is configured to be greater than 40% of the total memory. This can lead to increased memory pressure and poor performance.
  2018-02-06T19:59:53.880-0700 I CONTROL  [initandlisten] See http://dochub.mongodb.org/core/wt-windows-system-file-cache
  2018-02-06T19:59:53.881-0700 I CONTROL  [initandlisten]
  2018-02-07T15:59:53.882+1300 I STORAGE  [initandlisten] createCollection: admin.system.version with provided UUID: c91b385a-579d-400f-92f6-1442d6ef5887
  2018-02-07T15:59:53.892+1300 I COMMAND  [initandlisten] setting featureCompatibilityVersion to 3.6
  2018-02-07T15:59:53.895+1300 I STORAGE  [initandlisten] createCollection: local.startup_log with generated UUID: ccbcb30a-7460-463b-9334-c5d7d531e473
  2018-02-07T15:59:54.081+1300 I FTDC     [initandlisten] Initializing full-time diagnostic data capture with directory 'C:/data/db/diagnostic.data'
  2018-02-07T15:59:54.083+1300 I NETWORK  [initandlisten] waiting for connections on port 27017
  2018-02-07T16:01:11.179+1300 I NETWORK  [listener] connection accepted from 127.0.0.1:61738 #1 (1 connection now open)
  </code></pre>
  
  <p>Lastly the java code:</p>
  
  <pre><code>   public static void init() {
          try {
              Database.mongoClient = new MongoClient(DatabaseConfig.DB_HOST, DatabaseConfig.DB_PORT);
          } catch (Exception e) {
              Database.logger.log(Level.INFO, String.format("Unable to connect to database using connection : %s %d", DatabaseConfig.DB_HOST, DatabaseConfig.DB_PORT));
              e.printStackTrace();
              Database.mongoClient.close();
              throw e;
          }
      }
  </code></pre>