首页 > 解决方案 > mysql jdbc驱动中的通信链路故障

问题描述

我无法使用此代码从 java 建立与 mysql 的连接

 Connection con=null;
    String dbUrl="jdbc:mysql://localhost:3300/sys?useSSL=true";
    String driver = "com.mysql.cj.jdbc.Driver";
    String userName = "root";
    String password = "cool123";

    Class.forName(driver).getDeclaredConstructor().newInstance();
    con = DriverManager.getConnection(dbUrl,userName,password);

它向我抛出了这个错误The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received any packets from the server. The driver has not received any packets from the server. No appropriate protocol (protocol is disabled or cipher suites are inappropriate)

标签: javamysqlssljdbc

解决方案


您正在尝试使用传输层安全性(TLS 或 SSL)连接到您的服务器。那是因为你的连接字符串说?useSSL=true. 当您连接到服务器时,这有点奇怪localhost——没有传输进出您的 Java 程序运行的服务器,因此使用 TLS 会花费大量额外的计算周期而没有太多好处,如果您甚至可以让它工作的话。

您的错误消息提示 TLS 不起作用(协议已禁用或密码套件不合适是 TLS 行话。)

试试这个连接字符串:( jdbc:mysql://localhost:3300/sys无 TLS)。它可能会起作用。

然后试试这个连接字符串jdbc:mysql://localhost:3306/sys。它使用默认值。

设置 MySQL 服务器以使用 TLS需要一些服务器工程工作和知识。可能没有正确完成。如果有人为您这样做,请向该人寻求帮助。


推荐阅读