【问题标题】:Access denied for user 'root'@'localhost' (using password: YES): PhpMyAdmin用户'root'@'localhost'的访问被拒绝(使用密码:YES):PhpMyAdmin
【发布时间】:2012-07-17 08:51:37
【问题描述】:

鉴于此代码:

package db;

import java.sql.*;

public class Main
{
    public static void main (String[] args) throws ClassNotFoundException, SQLException
    {

        Class.forName("com.mysql.jdbc.Driver");

        String string = "jdbc:mysql://localhost:3306/testdb";

        Connection con = DriverManager.getConnection(string , "root" , "root");
        PreparedStatement statement = con.prepareStatement("select * from name");
        ResultSet result = statement.executeQuery();
        while (result.next())
        {
            System.out.println(result.getString(1) + " " + result.getString(2));
        }


    }
}

我正在尝试连接 mysql 服务器,但我得到了这个:

Exception in thread "main" java.sql.SQLException: Access denied for user 'root'@'localhost' (using password: YES)
    at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1074)
    at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:4074)
    at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:4006)
    at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:919)
    at com.mysql.jdbc.MysqlIO.proceedHandshakeWithPluggableAuthentication(MysqlIO.java:1694)
    at com.mysql.jdbc.MysqlIO.doHandshake(MysqlIO.java:1244)
    at com.mysql.jdbc.ConnectionImpl.coreConnect(ConnectionImpl.java:2397)
    at com.mysql.jdbc.ConnectionImpl.connectOneTryOnly(ConnectionImpl.java:2430)
    at com.mysql.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:2215)
    at com.mysql.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:813)
    at com.mysql.jdbc.JDBC4Connection.<init>(JDBC4Connection.java:47)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
    at java.lang.reflect.Constructor.newInstance(Unknown Source)
    at com.mysql.jdbc.Util.handleNewInstance(Util.java:411)
    at com.mysql.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:399)
    at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:334)
    at java.sql.DriverManager.getConnection(Unknown Source)
    at java.sql.DriverManager.getConnection(Unknown Source)
    at db.Main.main(Main.java:14)

14 行是:Connection con = DriverManager.getConnection(string , "root" , "root");

我确定我在安装 MySql 时输入了密码 root 。只想确认一下 , 我在这里输入:,

并将密码更改为root,如下所示:,但我仍然无法获得任何正确的输出。

知道它有什么问题吗?

谢谢

备注: 为了清楚起见,我正在使用:

  1. 面向 Web 开发人员的 Eclipse Java EE IDE。

  2. 雄猫 7

  3. Xampp

【问题讨论】:

  • 你能打开 phpMyAdmin 并在那里运行查询吗?好像连接没有打开。
  • @imrantariq:现在我被锁定在 phpMyAdmin 之外。我按照建议删除了这些行,而不是让我离开MySQL said: #1045 - Access denied for user 'root'@'localhost' (using password: NO)
  • 这意味着您无法打开phpMyAdmin。对吗?
  • @imrantariq:是的,但现在我删除了所有内容并重新安装了mysql & xampp & tomcat7,我又回来了。但是,关于来自javajdbc 的连接,我仍然从mysql 得到相同的输出。

标签: java mysql jdbc privileges


【解决方案1】:

线程“主”java.sql.SQLException 中的异常:访问被拒绝 用户'root'@'localhost'(使用密码:YES)

当用户无权访问本地主机时,您会收到此错误。

您需要从 localhost 授予对 root 的访问权限。

正如您添加的那样。我怀疑问题出在password='NO'mySQL 设置的行上。

【讨论】:

    【解决方案2】:

    使用您的用户名和密码执行以下命令:

    grant usage on *`.`* to `devuser@localhost` identified by '`devpassword`';
    grant all privileges on `dev.` to `devuser@localhost`;
    

    【讨论】:

    • replace star.star 显示不正确。所以请在“授权使用”之后立即使用star.star,而不仅仅是点
    【解决方案3】:

    您的问题与 phpMyAdmin 无关。它只是一个界面。

    除此之外,我认为您应该删除密码为“否”的行

    【讨论】:

    • 你确定吗?因为我上次这样做时,我的帐户权限已被撤销。
    • 好吧,在我们的服务器上,root 有 4 行。所有人都有 password="YES" 。包括 localhost 和 127.0.0.1 。您还可以删除“任何”行。而且您的代码应该在同一台机器上运行,或者在隧道机器上运行
    • ????我的答案是正确的,并且与所选答案相同。我是第一个回答的人。但我得到了反对?!??!
    猜你喜欢
    • 2013-05-07
    • 2020-03-18
    • 2016-07-11
    • 2017-08-24
    • 1970-01-01
    相关资源
    最近更新 更多