我正在尝试通过localhost从 JDBC 连接到 MySQL 。但是连接失败。在异常中,我看到 JDBC 正在尝试连接到127.0.0.1
String connectionString = "";
try {
loadProperties();
Class.forName("com.mysql.jdbc.Driver");
// Setup the connection with the DB
connectionString = "jdbc:mysql://" + properties.getProperty("host") + "/" + properties.getProperty
("database") + "?user=" + properties.getProperty("user") + "&password=" + properties
.getProperty
("password");
connect = DriverManager
.getConnection(connectionString);
logger.debug("Connected to " + properties.getProperty("host"));
} catch (Exception e) {
logger.error("Database Connection failed with connection string - " + connectionString,e);
}
从日志:
Database Connection failed with connection string - jdbc:mysql://localhost/testdb?user=testuser&password=testpass
java.sql.SQLException: Access denied for user 'testuser'@'127.0.0.1' (using password: YES)
为什么用 127.0.0.1 替换 localhost?我只为本地主机配置了登录。