我正在尝试从Spring Boot应用程序连接到我的远程MySQL数据库,并在一段时间内被此错误困扰。
即使增加了max_allowed_packet的大小,我仍然收到此错误。
测试连接的简单类[ 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!");
}
}
}
控制台:
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)
更新:
请注意,我已经增加了max_allowed_packet的大小。当前大小为
mysql> Select @@global.max_allowed_packet;
+-----------------------------+
| @@global.max_allowed_packet |
+-----------------------------+
| 536870912 |
+-----------------------------+
1 row in set (0.31 sec)
mysql>
答案 0 :(得分:0)
像这样更改mysql控制台中的设置:
SET GLOBAL max_allowed_packet = 1024*1024*14;
然后尝试重新连接
更新: 您可以检查以下SO线程:How to change max_allowed_packet size
希望这对您有帮助
答案 1 :(得分:0)
您需要在服务器中设置max_allowed_packet大小,以避免出现此错误。 设置参数后,需要重启服务器。 我不知道您使用的版本,因此请参考下面的链接以获取详细说明。
https://dev.mysql.com/doc/refman/8.0/en/packet-too-large.html