![在这里插入图片描述](https://img-blog.csdnimg.cn/3151a05a6c5d4a77bf76e116f99d0d22.jpg?x-oss-process=image/watermark,type_ZHJvaWRzYW5zZmFsbGJhY2s,shadow_50,text_Q1NETiBAbTBfNDY0ODU3NzE=,size_20,color_FFFFFF,t_70,g_se,x_16)
官网给的解释
https://dev.mysql.com/doc/connector-j/8.0/en/connector-j-usagenotes-troubleshooting.html#qandaitem-15-1-8
15.8: What should I do if I receive error messages similar to the following: “Communications link failure – Last packet sent to the server was X ms ago”?
Generally speaking, this error suggests that the network connection has been closed. There can be several root causes:
Firewalls or routers may clamp down on idle connections (the MySQL client/server protocol does not ping).
The MySQL Server may be closing idle connections that exceed the wait_timeout or interactive_timeout threshold.
Although network connections can be volatile, the following can be helpful in avoiding problems:
Ensure connections are valid when used from the connection pool. Use a query that starts with /* ping */ to execute a lightweight ping instead of full query. Note, the syntax of the ping needs to be exactly as specified here.
Minimize the duration a connection object is left idle while other application logic is executed.
Explicitly validate the connection before using it if the connection has been left idle for an extended period of time.
Ensure that wait_timeout and interactive_timeout are set sufficiently high.
Ensure that tcpKeepalive is enabled.
Ensure that any configurable firewall or router timeout settings allow for the maximum expected connection idle time.
Note
Do not expect to be able to reuse a connection without problems if it
has being lying idle for a period. If a connection is to be reused
after being idle for any length of time, ensure that you explicitly
test it before reusing it.
15.8: 如果我收到类似于以下内容的错误消息,我该怎么办:“通信链接失败 – 发送到服务器的最后一个数据包是 X 毫秒前”?
一般来说,此错误表明网络连接已关闭。可能有几个根本原因:
防火墙或路由器可能会限制空闲连接(MySQL 客户端/服务器协议不会 ping)。
MySQL 服务器可能正在关闭超过wait_timeout或 interactive_timeout阈值的空闲连接。
尽管网络连接可能不稳定,但以下内容有助于避免出现问题:
从连接池中使用时,确保连接有效。使用以 开头的查询 /* ping */来执行轻量级 ping 而不是完整查询。请注意,ping 的语法需要与此处指定的完全相同。
最小化连接对象在执行其他应用程序逻辑时空闲的持续时间。
如果连接长时间处于空闲状态,请在使用前显式验证连接。
确保wait_timeout和 interactive_timeout设置得足够高。
确保tcpKeepalive已启用。
确保任何可配置的防火墙或路由器超时设置允许最大预期连接空闲时间。
笔记
如果连接闲置一段时间,不要期望能够毫无问题地重用连接。如果要在空闲一段时间后重用连接,请确保在重用之前明确测试它。
在界面中搜索communication关键字