jdbc mysql通信链接到远程数据库失败 [英] jdbc mysql communications link failure to remote database

查看:295
本文介绍了jdbc mysql通信链接到远程数据库失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在我的java程序中使用jdbc连接到mysql数据库.我正在使用RHEL 6机器连接到mysql数据库.我在这台机器上打开了端口3306,如果这样做,则mysql -u root -p mysql数据库连接.

I am trying to connect to mysql database using jdbc in my java program. I am using a RHEL 6 machine to connect to the mysql database. I have opened the port 3306 in this machine and if I do, mysql -u root -p the mysql database connects.

如果我这样做,则mysql -h servername.edu -u root -p mysql数据库连接.

If I do, mysql -h servername.edu -u root -p the mysql database connects.

从我的Java程序中,

From my java program,

String url = "jdbc:mysql://localhost:3306/";

工作.

现在,如果我将localhost更改为servername,它将无法正常工作.

Now, if I change the localhost to servername, it doesn't work.

String url = "jdbc:mysql://server.com:3306/";

我收到错误消息,

com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure to remote databse

在发布问题之前,我对

Before posting the question, I did a fair bit of research on this exception from here.

我已经正确设置了my.cnf文件,但是在该文件中没有看到问题.我在这里想念什么?服务器的普通ping也可以正常工作.

I have setup the my.cnf file correctly and I do not see an issue in that file. What am I missing here? The normal ping to the server works fine as well.

推荐答案

CommunicationsException:通信链接失败

CommunicationsException: Communications link failure

您可以看到此答案,其中还包含有关您的问题的优质教程:

You can see this answer which also contains good tutorial about your problem:

如果收到SQLException:连接被拒绝或连接超时 或特定于MySQL的CommunicationsException:通信链接 失败,则意味着该数据库根本无法访问.这个可以 有以下一种或多种原因:

If you get a SQLException: Connection refused or Connection timed out or a MySQL specific CommunicationsException: Communications link failure, then it means that the DB isn't reachable at all. This can have one or more of the following causes:

  1. JDBC URL中的IP地址或主机名错误.
  2. 本地DNS服务器无法识别JDBC URL中的主机名.
  3. JDBC URL中的端口号丢失或错误.
  4. 数据库服务器已关闭.
  5. 数据库服务器不接受TCP/IP连接.
  6. 数据库服务器已用尽连接.
  7. Java和DB之间的某些情况阻止了连接,例如防火墙或代理.
  1. IP address or hostname in JDBC URL is wrong.
  2. Hostname in JDBC URL is not recognized by local DNS server.
  3. Port number is missing or wrong in JDBC URL.
  4. DB server is down.
  5. DB server doesn't accept TCP/IP connections.
  6. DB server has run out of connections.
  7. Something in between Java and DB is blocking connections, e.g. a firewall or proxy.

要解决一个问题,请遵循以下建议:

To solve the one or the other, follow the following advices:

  1. 使用ping验证并测试它们.
  2. 刷新DNS或在JDBC URL中使用IP地址.
  3. 基于MySQL DB的my.cnf进行验证.
  4. 启动数据库.
  5. 验证是否在没有--skip-networking选项的情况下启动mysqld.
  6. 重新启动数据库,并相应地修复代码,使其最终关闭连接.
  7. 禁用防火墙和/或配置防火墙/代理以允许/转发端口
  1. Verify and test them with ping.
  2. Refresh DNS or use IP address in JDBC URL instead.
  3. Verify it based on my.cnf of MySQL DB.
  4. Start the DB.
  5. Verify if mysqld is started without the --skip-networking option.
  6. Restart the DB and fix your code accordingly that it closes connections in finally.
  7. Disable firewall and/or configure firewall/proxy to allow/forward the port

这篇关于jdbc mysql通信链接到远程数据库失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆