Java mysql error communications link failure

com.mysql.cj.jdbc.exceptions.CommunicationsException: Communications link failure

The spring boot exception com.mysql.cj.jdbc.exceptions.CommunicationsException: Communications link failure happens when the java mysql database driver is unable to connect to mysql database. The Java application was unable to establish communication between mysql database driver and mysql database. The Jdbc connection between the java and mysql database could not be established. Therefore, the java exception com.mysql.cj.jdbc.exceptions.CommunicationsException: Communications link failure will be thrown.

Java Database Connectivity (JDBC) is used to connect a database to a Java application. The database driver helps to create a connection between the java and the database, and performs query execution in the java application database. If the database driver fails to establish a connection or a database communication link failure, the database exception com.mysql.cj.jdbc.exceptions.CommunicationsException: Communications link failure will be thrown.

Exception

The com.mysql.cj.jdbc.exceptions.CommunicationsException: Communications link failure exception will be shown as below stack trace

2020-12-01 04:11:37.796 ERROR 12682 --- [ main] com.zaxxer.hikari.pool.HikariPool : HikariPool-1 - Exception during pool initialization. com.mysql.cj.jdbc.exceptions.CommunicationsException: Communications link failure The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received any packets from the server. at com.mysql.cj.jdbc.exceptions.SQLError.createCommunicationsException(SQLError.java:174) ~[mysql-connector-java-8.0.21.jar:8.0.21] at org.springframework.boot.SpringApplication.run(SpringApplication.java:1226) ~[spring-boot-2.3.4.RELEASE.jar:2.3.4.RELEASE] at com.yawintutor.SpringBootTransactionApplication.main(SpringBootTransactionApplication.java:10) ~[classes/:na] Caused by: com.mysql.cj.exceptions.CJCommunicationsException: Communications link failure The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received any packets from the server. at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) ~[na:1.8.0_101] at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62) ~[na:1.8.0_101] at com.mysql.cj.jdbc.ConnectionImpl.connectOneTryOnly(ConnectionImpl.java:956) ~[mysql-connector-java-8.0.21.jar:8.0.21] at com.mysql.cj.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:826) ~[mysql-connector-java-8.0.21.jar:8.0.21] . 49 common frames omitted Caused by: java.net.ConnectException: Connection refused at java.net.PlainSocketImpl.socketConnect(Native Method) ~[na:1.8.0_101] at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:350) ~[na:1.8.0_101] at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:206) ~[na:1.8.0_101] at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:188) ~[na:1.8.0_101] at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392) ~[na:1.8.0_101] at java.net.Socket.connect(Socket.java:589) ~[na:1.8.0_101] at com.mysql.cj.protocol.StandardSocketFactory.connect(StandardSocketFactory.java:155) ~[mysql-connector-java-8.0.21.jar:8.0.21] at com.mysql.cj.protocol.a.NativeSocketConnection.connect(NativeSocketConnection.java:65) ~[mysql-connector-java-8.0.21.jar:8.0.21] . 52 common frames omitted 

Root Cause

The java database connectivity is responsible to establish the connection and execute the query in the database. The database driver creates a connection with the database and performs the query execution. If the database is unable to establish the connection or the communication link failure in the established connection, the exception com.mysql.cj.jdbc.exceptions.CommunicationsException: Communications link failure will be thrown.

Читайте также:  Css attribute selectors not

Solution 1 – Mysql database is shutdown

The java application attempts to connect to the mysql server that is not running. The mysql database server may not be started or shutdown. Login to the mysql database server, verify the database is running or shutdown. Starting or restarting the mysql database will resolve the exception “com.mysql.cj.jdbc.exceptions.CommunicationsException: Communications link failure”.

start or restart mysql database

Solution 2 – MySql run out of connections

The mysql database creates and runs maximum configured connections. If a new connection is tried by the java program, the mysql database will run out of connections. The restart of the mysql server releases the connection and establishes new connections. If this problem happens regularly, the maximum connection number should be raised.

restart mysql database increase maximum allowed concurrent connections in mysql database verify any database connection leak in java

Solution 3 – IP address or hostname in JDBC URL is wrong

The host name or IP address that the Java programme configures might not be right. In the configuration, check the host name or the IP address. Try modifying the IP address and host name. In mysql jdbc url, the host name and the IP address may be misspelt.

spring.datasource.url=jdbc:mysql://localhost:3306/test spring.datasource.username=root spring.datasource.password=root spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL5Dialect

Solution 4 – Port number is wrong or missing

The mysql database is executed on a port. 3306 is the default port. You should configure the MySQL database to run on various ports. Check the port of the mysql database and configure the right port number in the jdbc url. The port number could be missing in the jdbc url.

spring.datasource.url=jdbc:mysql://localhost:3306/test

Solution 5 – DNS server issues

The DNS server will not recognise the host name configured on the jdbc url. The host name may not be configured or there may be any DNS mapping issues. Restarting a DNS server addresses an exception or uses an IP address in the JDBC url instead of the host name.

verify the host name using ping command - ping localhost try to connect host name using telnet command - telnet localhost 3306 restart the dns server use IP address instead of the host name

Solution 6 – Mysql server issue

The mysql database server does not accept the TCP/IP connection. The mysql database server may not respond due to memory issues or hang the server due to long running hours / days. Restarting the mysql database server will release the port, memory issues such as OutOfMemoryError, Heap Memory error, cpu utilization

restart mysql database server verify the host name using ping command - ping localhost try to connect host name using telnet command - telnet localhost 3306

Solution 7 – Network issues

There could be some firewalls, proxy, antivirus softwares blocks the connection. Verify and disable the firewall, proxy settings. If an antivirus software blocks the connection, enable the antivirus to allow the connection. If the network speed is slow, there could be a timed out error. Make sure the network such as internet speed is good to establish the connection.

disable firewall, proxy allow the connection in antivirus verify the network, internet speed to establish connection

Solution 8 – Maven dependency

In the java application, use the correct maven dependency for the mysql database version. The mismatch of the mysql database version and the mysql database driver jar version will create the connectivity issue. make sue the right maven dependency is configured for the mysql database driver corresponding to the mysql database version.

  mysql mysql-connector-java runtime 

Источник

Читайте также:  Как повернуть матрицу python

Форум

Доброго времени суток Вам Максим, и всем участникам данного форума, к сожалению столкнулся с проблемой связанной о взаимодействии Java и MySQL, дело в том что я имею свои собственные игровые ресурсы, которые базируется на платформе Java, и не посредственно все данные хрянятся в MySQL, так вот возникает ошибки вида:

com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received any packets from the server.

Проверил все конфиги, на подключение к БД, порты и т.п., пробывал запустить сервер с другой WAMP системой, заработал, но я заинетесован именно в вашем инструменте Максим, так как он имеет огромный функционал и возможности.

Так вот прошу помощи в решении моей проблемы.
С уважением, .dtramp!

Добавлено спустя 49 минут 2 секунды:

Caused by: com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communicatio ns link failure The last packet successfully received from the server was 1а876а706 milliseconds ago. The last packet sent successfully to the server was 0 milliseconds ago. at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source) at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Sou rce) at java.lang.reflect.Constructor.newInstance(Unknown Source) at com.mysql.jdbc.Util.handleNewInstance(Util.java:411) at com.mysql.jdbc.SQLError.createCommunicationsException(SQLError.java:1 117) at com.mysql.jdbc.MysqlIO.reuseAndReadPacket(MysqlIO.java:3603) at com.mysql.jdbc.MysqlIO.reuseAndReadPacket(MysqlIO.java:3492) at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:4043) at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:2503) at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2664) at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2788) at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2738) at com.mysql.jdbc.StatementImpl.executeQuery(StatementImpl.java:1617) at com.mysql.jdbc.ConnectionImpl.isReadOnly(ConnectionImpl.java:3894) . 14 more

Источник

Оцените статью