jdbc mysql通信链接到远程数据库失败 [英] jdbc mysql communications link failure to remote database
问题描述
我试图在我的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:
- JDBC URL中的IP地址或主机名错误.
- 本地DNS服务器无法识别JDBC URL中的主机名.
- JDBC URL中的端口号丢失或错误.
- 数据库服务器已关闭.
- 数据库服务器不接受TCP/IP连接.
- 数据库服务器已用尽连接.
- Java和DB之间的某些情况阻止了连接,例如防火墙或代理.
- IP address or hostname in JDBC URL is wrong.
- Hostname in JDBC URL is not recognized by local DNS server.
- Port number is missing or wrong in JDBC URL.
- DB server is down.
- DB server doesn't accept TCP/IP connections.
- DB server has run out of connections.
- 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:
- 使用ping验证并测试它们.
- 刷新DNS或在JDBC URL中使用IP地址.
- 基于MySQL DB的my.cnf进行验证.
- 启动数据库.
- 验证是否在没有--skip-networking选项的情况下启动mysqld.
- 重新启动数据库,并相应地修复代码,使其最终关闭连接.
- 禁用防火墙和/或配置防火墙/代理以允许/转发端口
- Verify and test them with ping.
- Refresh DNS or use IP address in JDBC URL instead.
- Verify it based on my.cnf of MySQL DB.
- Start the DB.
- Verify if mysqld is started without the --skip-networking option.
- Restart the DB and fix your code accordingly that it closes connections in finally.
- Disable firewall and/or configure firewall/proxy to allow/forward the port
这篇关于jdbc mysql通信链接到远程数据库失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!