Background:
Because the Linux server used for development is in a relatively closed environment, only port 22 is accessed through SSH. Therefore, putty is used to create an SSH forwarding, which maps the port 27018 of the local machine to the remote port 27017. Mongo localhost: 27018 is successfully connected in cmd and operations are performed.
Symptom:
In the program
- Mongo mongo =NewMongo ("Localhost",27018);
When connecting to MongoDB on a remote server, the following error occurs during the program 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 (dbtcpconne. java:207)
- ...37More
From the exception information, we can see that localhost is replaced with the form of "machine name/IP Address", and this combination cannot be connected in cmd.