自从安装干净的Ubuntu 20.1以来,我一直遭受mysql连接的困扰。 我使用phpmyadmin运行了LAMP类型安装的常规安装。
在mysql上加载的版本是: 服务器版本:8.0.22 MySQL Community Server-GPL
我为用户添加了所有权限。
下面的代码适用于我的旧连接,即查询192.168.0.12
当我使用新系统和localhost或127.0.0.1
没有防火墙等。
下面的代码查看我尝试过的各种选项的字符串查询。
public class DatabaseInterface {
final public String dbName = "bloodpressure";
final public String dbUserid = "test";
final public String dbPassword = "pass";
protected Connection conn = null;
protected Statement stmt = null;
/** Creates a new instance of DatabaseInterface */
public DatabaseInterface() {}
public Connection connectToDatabase(PrintWriter out) {
/*
* Function to connect to the database
*/
String query = null;
ResultSet rs = null;
String errorMsg = "";
boolean rc = true;
try { // The newInstance() call is a work around for some
// broken Java implementations
Class.forName("com.mysql.jdbc.Driver").newInstance();
} catch (Exception ex) {
if (out != null) {
out.println("<p>Error while defining database driver: " +
"Class.forName: " + ex.getMessage());
out.println("<p>" + ex.toString() + "<p>");
}
}
boolean databaseFound = false;
boolean loggingOn = true;
// Retrieve the database userid and password from the system registry
try {
// ===================== These fail =============================
// query = "jdbc:mysql:127.0.0.1//:3306/?user=" + dbUserid + "&password=" + dbPassword;
query = "jdbc:mysql://localhost:3306/?user=" + dbUserid + "&password=" + dbPassword;
// =================== This works ==========================================
// query = "jdbc:mysql://192.168.0.12:3306/?user=" + dbUserid + "&password=" + dbPassword;
// This throw the exception
conn = DriverManager.getConnection(query);
==================
该异常给出: com.mysql.cj.jdbc.exceptions.CommunicationsException:通信链接失败最近一次成功发送到服务器的数据包是在0毫秒之前。驱动程序尚未收到来自服务器的任何数据包。 Query = jdbc:mysql:// localhost:3306 /?user = test&password = pass SQLException:通信链接失败最近一次成功发送到服务器的数据包是在0毫秒之前。驱动程序尚未收到来自服务器的任何数据包。 SQL状态:08S01 VendorError:0
我还比较了可以正常工作的旧系统192.168.0.12和全新安装的my.cnf文件。他们都只包含 [mysql]
我在这里找到的建议之一建议尝试各种 绑定地址选项没有影响。
在此先感谢您提供的任何帮助,对此我完全感到茫然。
非常感谢 杰夫。