Background information:
Because the Linux server used for development is in a relatively closed environment, only 22 ports are accessed via SSH. So I used putty to do an SSH forwarding to map the 27018 port of this machine to the remote 27017 port. Successfully connected via MONGO localhost:27018 in CMD and operated.
Phenomenon:
In the program through the
Mongo Mongo = new Mongo ("localhost", 27018);
MongoDB on a remote server, the following error occurred while the program was running:
caused By:java.io.IOException:couldn ' t connect to [3cnl07745/172.28.220.64:27018] Bc:java.net.ConnectException: Connection Refused:connect at
Com.mongodb.dbport._open (dbport.java:206) at
Com.mongodb.DBPort.go ( dbport.java:94) at
com.mongodb.DBPort.go (dbport.java:75) at
Com.mongodb.DBPort.call (dbport.java:65)
At Com.mongodb.DBTCPConnector.call (dbtcpconnector.java:207) ...
Panax Notoginseng
As seen from the exception information, localhost is replaced with a "machine name/IP address", which is not connected in CMD.
Workaround:
In fact, the solution is quite simple: replace the localhost to become 127.0.0.1 .