我正在尝试测试与我的数据库的连接,但我一直收到以下错误
java.sql.SQLException: Access denied for user 'db'@'localhost' (using password: YES)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:2975)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:798)
at com.mysql.jdbc.MysqlIO.secureAuth411(MysqlIO.java:3700)
at com.mysql.jdbc.MysqlIO.doHandshake(MysqlIO.java:1203)
at com.mysql.jdbc.Connection.createNewIO(Connection.java:2572)
at com.mysql.jdbc.Connection.<init>(Connection.java:1485)
at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:266)
at java.sql.DriverManager.getConnection(Unknown Source)
at java.sql.DriverManager.getConnection(Unknown Source)
at ConnectingToDB.main(ConnectingToDB.java:18)
我的代码如下。该行发生错误 “conn = DriverManager.getConnection(url + dbName,userName,password);”
public class ConnectingToDB
{
public static void main(String[] args)
{
System.out.println("MySQL Connect Example.");
Connection conn = null;
String url = "jdbc:mysql://localhost:3306/";
String dbName = "steven";
String driver = "com.mysql.jdbc.Driver";
String userName = "db";
String password = "db";
try
{
Class.forName(driver).newInstance();
conn = DriverManager.getConnection(url+dbName,userName,password);
System.out.println("Connected to the database");
conn.close();
System.out.println("Disconnected from database");
}
catch (Exception e) {
e.printStackTrace();
}
}
}
答案 0 :(得分:2)
正如您所看到的,您的凭据由MySql处理但是被拒绝,这意味着该问题可能与凭证没有直接关系,因为如果凭据错误,则错误会有所不同然后被拒绝。凭据被拒绝时,问题主要与:
有关逐一尝试对上述步骤进行排查..
答案 1 :(得分:0)
你应该检查你的mysql中是否存在 db @ localhost 。否则,请使用必要的权限创建此用户配置文件。
CREATE USER user_specification
[, user_specification] ...
user_specification:
user [IDENTIFIED BY [PASSWORD] 'password']
有关详情,请查看CREATE USER Syntax
答案 2 :(得分:0)
LOCALHOST和“机器名称”与MySQL不同。我从命令行检查你可以连接到localhost:
mysql -hlocalhost -udb -pdb steven
如果失败,请使用适当的权限连接并运行它:
create user db@localhost identified by 'db';
grant select on steven.* to db@localhost;