我試圖通過localhost從JDBC連接到MySQL。但連接失敗。在例外,我看到JDBC試圖連接到127.0.0.1JDBC MySQL自動將localhost轉換爲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替換本地主機?我只爲本地主機配置了登錄。
我知道這一點。但是我的MySQL服務器只對本地主機開放,而不是對任何其他IP地址,包括127.0.0.1 – emaillenin
你有你的hosts文件配置映射嗎? – Keerthivasan
是的。我有'127.0.0。1 \t localhost' in my'/ etc/hosts' – emaillenin