【问题标题】:Access denied connecting from java to mysql访问被拒绝从 java 连接到 mysql
【发布时间】:2015-06-21 12:38:11
【问题描述】:

我正在尝试从 java 连接到数据库并收到以下错误消息:

java.sql.SQLException: Access denied for user 'gda'@'localhost' (using password: YES)

但我正在连接的命令行使用相同的凭据

server$ mysql -ugda -pgda
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 2666453
Server version: 5.5.39-36.0-log Percona Server (GPL), Release 36.0, Revision 697

Copyright (c) 2009-2014 Percona LLC and/or its affiliates
Copyright (c) 2000, 2014, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql>

在另一个数据库上,我的 java 工作正常并且能够连接。似乎是数据库级别的问题?!我需要在数据库级别修复什么?

添加连接代码:

public Connection getConnection() {     
    try {
        Class.forName(jdbcDriver);

        connection = DriverManager.getConnection(connectionString());
    }
    catch(Exception ex) {
        ex.printStackTrace();
        logger.error("Exception:" + ex.getMessage());
        errorMessage = ex.getMessage();
    }

    return connection;
}

地点:

jdbc:mysql://127.0.0.1:3306/db?user=gda&password=gda

【问题讨论】:

  • 你能显示连接的代码吗?
  • 我不知道我从我们的 dba 那里得到了这个凭据,我猜他更改了 root 密码
  • 也许它可以帮助(类似问题)这里stackoverflow.com/questions/28954740/…
  • 我遇到了那个错误,我使用的密码不正确
  • 我遇到了类似的问题,但使用的是 php。不要使用localhost,而是尝试使用127.0.0.1,这对我有用。

标签: java mysql


【解决方案1】:

尝试以这种方式传递用户名和密码:

String DB_URL = "jdbc:mysql://localhost/xyz";
String USER = "username";
String PASS = "password";

conn = DriverManager.getConnection(DB_URL,USER,PASS);

【讨论】:

  • 我怀疑这是否能解决问题,也许它应该作为评论发布?
  • 如果不解决问题,我认为值得一试。
  • 试过但不起作用,我创建了一个新用户并且能够使用旧的 java 代码进行连接。所以数据库级别的用户有问题。我把这个写给我们的 dba
猜你喜欢
  • 2018-05-19
  • 2016-02-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多