0

I have the following code that intends to insert a document into a local instance of a mongodb database:

(let [conn (mg/connect)
      db   (mg/get-db conn "monger-test")]
  (pprint (mc/insert-and-return db "documents" {:name "John" :age 30}))

  )

But upon running this, I get the following error:

INFO: Cluster created with settings {hosts=[127.0.0.1:27017], mode=SINGLE, requiredClusterType=UNKNOWN, serverSelectionTimeout='30000 ms', maxWaitQueueSize=500}
Feb 01, 2020 8:20:34 AM com.mongodb.diagnostics.logging.JULLogger log
INFO: Exception in monitor thread while connecting to server 127.0.0.1:27017
com.mongodb.MongoSocketOpenException: Exception opening socket
    at com.mongodb.connection.SocketStream.open(SocketStream.java:63)
    at com.mongodb.connection.InternalStreamConnection.open(InternalStreamConnection.java:115)
    at com.mongodb.connection.DefaultServerMonitor$ServerMonitorRunnable.run(DefaultServerMonitor.java:116)
    at java.base/java.lang.Thread.run(Thread.java:834)
Caused by: java.net.ConnectException: Connection refused (Connection refused)
    at java.base/java.net.PlainSocketImpl.socketConnect(Native Method)
    at java.base/java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:399)
    at java.base/java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:242)
    at java.base/java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:224)
    at java.base/java.net.SocksSocketImpl.connect(SocksSocketImpl.java:403)
    at java.base/java.net.Socket.connect(Socket.java:591)
    at com.mongodb.connection.SocketStreamHelper.initialize(SocketStreamHelper.java:50)
    at com.mongodb.connection.SocketStream.open(SocketStream.java:58)
    ... 3 more

Feb 01, 2020 8:20:34 AM com.mongodb.diagnostics.logging.JULLogger log
INFO: No server chosen by WritableServerSelector from cluster description ClusterDescription{type=UNKNOWN, connectionMode=SINGLE, serverDescriptions=[ServerDescription{address=127.0.0.1:27017, type=UNKNOWN, state=CONNECTING, exception={com.mongodb.MongoSocketOpenException: Exception opening socket}, caused by {java.net.ConnectException: Connection refused (Connection refused)}}]}. Waiting for 30000 ms before timing out
:error-while-loading server.core

Error refreshing environment: Syntax error compiling at (server/core.clj:29:1). com.mongodb.MongoTimeoutException: Timed out after 30000 ms while waiting for a server that matches WritableServerSelector. Client view of cluster state is {type=UNKNOWN, servers=[{address=127.0.0.1:27017, type=UNKNOWN, state=CONNECTING, exception={com.mongodb.MongoSocketOpenException: Exception opening socket}, caused by {java.net.ConnectException: Connection refused (Connection refused)}}]
Finished at 08:21:04.671 (run time: 30.322s)

How do I fix this?

zengod
  • 1,114
  • 13
  • 26
  • The relevant error appears to be `java.net.ConnectException: Connection refused (Connection refused)`. Do you have a MongoDB process running on `127.0.0.1`? Try connecting with the `mongo` shell to confirm. – Stennie Feb 02 '20 at 01:18

2 Answers2

0

It seems like your local mongo is corrupted, that happened to me and i couldn't even log into mongo from console. Try loggin into mongo from youy terminal, if that action returns that error too you have to uninstall your mongodb instance and install it from zero again.

  • The error only suggests an inability to connect to MongoDB with the provided connection string. Reinstalling MongoDB would be an extreme (and unnecessary) solution. It is more likely that a MongoDB instance either isn't running, or is running on a different IP/port combination. – Stennie Feb 02 '20 at 01:26
0

You have to make sure that your mongo shell service is up and running:

brew services list

You should be able to see mongodb-community with status started.

Reference: Read-only file system when attempting mkdir /data/db on Mac

Malakai
  • 3,011
  • 9
  • 35
  • 49
sFritsch09
  • 424
  • 4
  • 10