我正在开发一个mysql数据库,通过Mac上的Servlet程序(通过Eclipse)访问。我最近的问题是,我无法通过我的 Servlet 访问 mysql DB,它之前工作得很好。我使用 MySQLWorkBench,其中数据库在 Mac 上运行。 当 Servlet 尝试访问它时,它会抛出以下错误。实际上,密码为空。在 Mac 机器上工作。
java.sql.SQLException: Access denied for user 'root'@'localhost' (using password: YES)
Servlet 代码如下:
// MySQL
Connection conn = null;
Properties connectionProps = new Properties();
connectionProps.put("user", "root");
connectionProps.put("password", ""); // No password is set
try {
Class.forName("com.mysql.jdbc.Driver");
conn = DriverManager.getConnection(
"jdbc:" + "mysql" + "://" +
"localhost" +
":" + "3306" + "/InfoDB",
connectionProps);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("Connected to database");
// insert record
try {
Statement stmt = (Statement) conn.createStatement();
String sql = "SELECT IP FROM InfoTable";
ResultSet rs = stmt.executeQuery(sql);
//STEP 5: Extract data from result set
String ipaddress = null;
while(rs.next()){
//Retrieve by column name
ipaddress = rs.getString("IP");
System.out.println("IP Address retrieved from DB: " + ipaddress);
}
PrintWriter out = response.getWriter();
out.println(ipaddress);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
相同的代码之前运行良好。我在这里看到了一些建议,
Solution for this issue ,
但我不知道这个 information.schema 表
驻留在哪里以及如何设置权限。
并且,此链接中有另一个建议,Solution for this issue但是,我不明白如何解决这个问题?
有人可以指导我解决这个问题吗?
最佳答案
您的root
帐户没有密码。因此,无需密码即可建立连接。
connectionProps.put("user", "root");
// Remove this line --> connectionProps.put("password", ""); // No password is set
或者更新root
帐户的密码并使用该密码进行连接。
MySQL:
GRANT ALL PRIVILEGES ON *.* TO 'root'@'localhost' IDENTIFIED BY 'your-password';
然后
connectionProps.put("user", "root");
connectionProps.put("password", "your-password");
关于mysql - 用户 'root' @'localhost' 从 servlet 的访问被拒绝,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22070438/