Java cannot connect to MongoDB

Source: Internet
Author: User

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

  1. Mongo mongo =NewMongo ("Localhost",27018);
When connecting to MongoDB on a remote server, the following error occurs during the program running:
  1. Caused by: java. io. IOException: couldn't connect to [3CNL07745/172.28.220.64:27018] Bc: java.net. ConnectException: Connection refused: connect
  2. At com. mongodb. DBPort. _ open (DBPort. java:206)
  3. At com. mongodb. DBPort. go (DBPort. java:94)
  4. At com. mongodb. DBPort. go (DBPort. java:75)
  5. At com. mongodb. DBPort. call (DBPort. java:65)
  6. At com. mongodb. DBTCPConnector. call (dbtcpconne. java:207)
  7. ...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.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.