连接Mysql数据库时出错了

时间:2013-11-15 07:04:03

标签: mysql

连接数据库时出现此错误。请提供解决方案。

  

发送到服务器的最后一个数据包是1毫秒前。   com.mysql.jdbc.exceptions.jdbc4.CommunicationsException:通信   链接失败

2 个答案:

答案 0 :(得分:0)

所以,你有一个

CommunicationsException:通信链接失败 我引用this answer,其中还包含一步一步的MySQL + JDBC教程:

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:

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. 

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

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. 

答案 1 :(得分:0)

  

CommunicationsException:通讯链接失败

请参阅:Connect Java to a MySQL database

缩短它:

如果您收到SQLException:连接被拒绝或连接超时或MySQL特定的CommunicationsException:通信链接失败,则表示根本无法访问数据库。这可能有以下一个或多个原因:

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:

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.