0

I am trying to make a connection to my remote MySQL Database from Spring Boot application and got stuck with this error for a while.

Even after increasing the max_allowed_packet size, I am getting this error.

enter image description here

Simple class to test connection[Mkyong]

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

public class JDBCExample {

    public static void main(String[] argv) {

        try {
            Class.forName("com.mysql.jdbc.Driver");
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
            return;
        }

        Connection connection = null;

        try {

            connection = DriverManager.getConnection("jdbc:mysql://HOST:PORT/DB", "user", "password");

        } catch (SQLException e) {
            System.out.println("Connection Failed! Check output console");
            e.printStackTrace();
            return;
        }

        if (connection != null) {
            System.out.println("You made it, take control your database now!");
        } else {
            System.out.println("Failed to make connection!");
        }
    }
}

Console:

Connection Failed! Check output console
com.mysql.jdbc.PacketTooBigException: Packet for query is too large (4739923 > 1048576). You can change this value on the server by setting the max_allowed_packet' variable.
    at com.mysql.jdbc.MysqlIO.readPacket(MysqlIO.java:578)
    at com.mysql.jdbc.MysqlIO.doHandshake(MysqlIO.java:1014)
    at com.mysql.jdbc.ConnectionImpl.coreConnect(ConnectionImpl.java:2190)
    at com.mysql.jdbc.ConnectionImpl.connectOneTryOnly(ConnectionImpl.java:2221)
    at com.mysql.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:2016)
    at com.mysql.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:776)

UPDATE:

Please note that I have already increased max_allowed_packet size. Currently the size is

mysql>  Select @@global.max_allowed_packet;
+-----------------------------+
| @@global.max_allowed_packet |
+-----------------------------+
|                   536870912 |
+-----------------------------+
1 row in set (0.31 sec)

mysql>
Abhilash
  • 435
  • 5
  • 15
  • 1
    Given this occurs on connect, make sure you are connecting to the correct hostname and port (usually port 3306 for MySQL). The linked duplicate suggests that this problem can occur when you connect to something that is not a MySQL server, but - for example - a HTTP server. – Mark Rotteveel Sep 08 '18 at 08:17
  • Thanks @MarkRotteveel, The error message is misleading. Changing the port number solved the problem. Appreciate your help. – Abhilash Sep 08 '18 at 08:40

2 Answers2

0

change the settings in mysql console like this:

SET GLOBAL max_allowed_packet = 1024*1024*14;

Then try to connect again

UPDATE: You can check this SO thread:How to change max_allowed_packet size

Hope this will help you

UVM
  • 9,776
  • 6
  • 41
  • 66
0

You need set the max_allowed_packet size in server to avoid this error. After setting the parameter the server needs to be rebooted. I don't know the version you are using so please refer the below link for detail explanation.

https://dev.mysql.com/doc/refman/8.0/en/packet-too-large.html