简体   繁体   English

java.net.ConnectException:连接被拒绝

[英]java.net.ConnectException: Connection refused

I am trying to connect to a mysql database on remote server and whenever I try to run the code it is giving me connection refused exception. 我试图连接到远程服务器上的mysql数据库,并且每当我尝试运行代码时,它都会给我拒绝连接的异常。

 Connection con = null;
          String driver = "com.mysql.jdbc.Driver";
          String url1="jdbc:mysql://IPADDRESS:3306/";
          String db = "hola";
          String dbUser = "root";
          String dbPasswd = "root";
          Class.forName(driver).newInstance();
          con = DriverManager.getConnection(url1+db, dbUser, dbPasswd);
          System.out.println("Database Connection Established");

Also, when I telnet with that IP on a port 3306 it is giving me connection refused. 另外,当我使用端口3306上的IP进行Telnet时,连接被拒绝。 How can I make sure that my server listens to connection over port 3306? 如何确保服务器侦听端口3306上的连接?

java.net.ConnectException: Connection refused意味着您输入的IP地址或端口号不正确!

There is nothing listening at that IP:port. 在该IP:port上没有监听。 Check that the IP:port is correct, that MySQL is installed there, on that port, and that it is running. 检查IP:port是否正确,在该端口上是否已安装MySQL,以及它是否正在运行。

Contrary to other answers here, it has nothing to do with the user name, password, or permissions, and almost certainly nothing to do with the firewall either unless you have an antique. 与这里的其他答案相反,它与用户名,密码或权限无关,并且几乎可以肯定与防火墙无关,除非您有古董。

In general, when you are getting java.net.ConnectException: Connection refused often it is because of the following reasons: 通常,当您获取java.net.ConnectException: Connection refused经常java.net.ConnectException: Connection refused是由于以下原因:

  1. The server is not started. 服务器未启动。 What happens when you ping the host or try to access the host from you browser? 当您对主机执行ping操作或尝试从浏览器访问主机时会发生什么? Maybe there is a spelling error in the hostname of your project. 项目的主机名中可能存在拼写错误。
  2. Your firewall is blocking access. 您的防火墙阻止了访问。 Disable you firewall and try again 禁用防火墙,然后重试
  3. You are using the wrong port. 您使用了错误的端口。 If you have access to the server see what port it is configured with and that your client connection params match. 如果您有权访问服务器,请查看服务器配置了哪个端口,以及客户端连接参数是否匹配。
  4. You have not configured SSL. 您尚未配置SSL。 Is the endpoint using https? 端点是否使用https? This is more specific to connecting web services rather than databases, but if you are not handling certs correctly you will get a connection refused error. 这是更特定于连接Web服务而不是数据库,但是,如果未正确处理证书,则会出现连接被拒绝的错误。

I've found the solution for the problem. 我找到了解决问题的方法。 My server was not listening to connections over the port 3306 and I made necessary configurations 我的服务器未监听端口3306上的连接,因此我进行了必要的配置

Thank you all 谢谢你们

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM