java.sql。SQLException:用户“root”@“localhost”(使用密码:YES)拒绝访问

时间:2021-12-30 20:22:22

The following code:

下面的代码:

Class.forName("com.mysql.jdbc.Driver");
Connection m_connection = DriverManager.getConnection("jdbc:mysql://localhost","root","root");

Throws this exception on getConnection():

在getConnection()中抛出这个异常:

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.Database.<init>(Database.java:91)
    at db.Main.main(Main.java:10)

How is this caused and how can I solve it?

这是如何造成的,我该如何解决?

EDIT:

编辑:

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

        try
        {
            Connection conn = (Connection) DriverManager.getConnection("jdbc:mysql://localhost/?user=root&password=rootpassword"); 
            Statement   s = (Statement) conn.createStatement();
            int result = s.executeUpdate("CREATE DATABASE databasename");
        }


        catch ( Exception e)
        {
            e.printStackTrace();
        }
}

Produces :

生产:

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:19)

12 个解决方案

#1


8  

As you are creating a database from scratch, you could use:

当您从头创建数据库时,您可以使用:

Connection connection = DriverManager.getConnection("jdbc:mysql://localhost/?user=root&password=rootpassword"); 
PreparedStatement ps = connection.prepareStatement("CREATE DATABASE databasename");
int result = ps.executeUpdate();

Here is an identical scenario.

这里有一个相同的场景。

#2


40  

This can help you:

这可以帮助你:

GRANT ALL PRIVILEGES ON *.* TO 'root'@'localhost' IDENTIFIED BY '%password%' WITH GRANT OPTION;

Execute it with command line or some GUI tool.

用命令行或GUI工具执行它。

Don't forget to replace %password% with real password.

不要忘记用真实密码替换%password%。

#3


5  

I had a similar problem, but the differemce was: I didn't executed my JavaApp from localhost, but from a remote PC. So I got something like java.sql.SQLException: Access denied for user 'root'@'a.remote.ip.adress' (using password: YES) To solve this, you can simply login to phpMyAdmin, go to Users, click add user and enter the host from which you want to execute your JavaApp (or choose Any Host)

我有一个类似的问题,但是不同的是:我没有从本地主机上执行我的JavaApp,而是从远程PC上执行。我得到了java。sql。SQLException:用户“root”@ .remote.ip拒绝访问。adress(使用密码:是的)来解决这个问题,你可以登录phpMyAdmin,访问用户,点击添加用户,然后输入你想要执行JavaApp的主机(或者选择任何主机)

#4


3  

you can use this

你可以使用这个

   static final String JDBC_DRIVER = "com.mysql.jdbc.Driver";  
   static final String DB_URL = "jdbc:mysql://localhost:3306/YOUR_DB_NAME";


   static final String USER = "root";
   static final String PASS = "YOUR_ROOT_PASSWORD"; 

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

you have to give the right root password .

您必须提供正确的根密码。

#5


2  

You should specify the db you are connecting to:

您应该指定连接到的db:

jdbc:mysql://localhost:3306/mydb

#6


1  

I was hitting the same issue. Added mysql service port number(3307), resolved the issue.

我也遇到了同样的问题。添加了mysql服务端口号(3307),解决了这个问题。

conn = DriverManager.getConnection("jdbc:mysql://localhost:3307/?" + "user=root&password=password");

#7


1  

Try it like this....

试着这样....

public static Connection getConnection() throws SQLException{

    String driver = "com.mysql.jdbc.Driver";
    String url    = "jdbc:mysql://localhost:3306/test";
    String username = "root";
    String password = "vicky";            // Change it to your Password
    System.setProperty(driver,"");

    return DriverManager.getConnection(url,username,password);
}

#8


1  

Although this may not be the cause of your issue, you'll get the same error if there are two MySQL services running on the same port. You can check on windows by looking at the list of services in the services tab of Task Manager.

尽管这可能不是您的问题的原因,但是如果在同一个端口上运行两个MySQL服务,您将会得到相同的错误。您可以通过查看任务管理器的services选项卡中的服务列表来检查windows。

#9


1  

This Exception is also caused due to version mismatch of mysql db and your pom.xml/jar.

这个异常也是由于mysql db和您的pom.xml/jar版本不匹配造成的。

Make sure your pom.xml/jar version is higher than your mysql db version. Because higher versions are compatible with lower version's, but the same is not true for the inverse.

确保你的砰的一声。xml/jar版本比您的mysql db版本高。因为更高的版本与较低的版本兼容,但是对于相反的版本来说是不一样的。

Solution's for java project

解决方案的java项目

  • Replace the Jar with suitable version.
  • 用合适的版本替换罐子。

Solution for maven based

解决方案基于maven

  • Change the dependency Version in pom.xml
  • 更改pom.xml中的依赖项版本。

Solution for spring boot - Override the spring boot dependency by adding 5.1.5.Final

spring启动的解决方案-通过添加5.1.5.Final来覆盖spring启动依赖项。

#10


0  

I had the same issue "java.sql.SQLException: Access denied for user 'root'@'localhost' (using password: YES)". Problem was "WRONG PASSWORD". Copy and paste the query as-it-is in the shell to check whether it gives the desired output or not. Small errors consumes more time.

我有同样的问题“java.sql”。SQLException:用户“root”@“localhost”(使用密码:YES)拒绝访问。问题是“错误密码”。复制并粘贴查询,它位于shell中,以检查它是否提供了所需的输出。小错误会消耗更多的时间。

#11


0  

I had to change my SQL setup (in my host, Dreamhost in my case) to allow the username to access the database from other hosts, not just Dreamhost since I'm running a program from the IDE on my computer. I did that for now by adding % to the list of Allowable Hosts. It now works!

我必须改变我的SQL设置(在我的主机中,在我的例子中是Dreamhost),允许用户名从其他主机访问数据库,而不仅仅是Dreamhost,因为我在我的计算机上运行了一个IDE程序。我这样做是为了增加%到允许的主机列表。它现在工作了!

#12


-1  

I encountered this error. The problem was that I had the wrong database name.

我遇到了这个错误。问题是我的数据库名错了。

#1


8  

As you are creating a database from scratch, you could use:

当您从头创建数据库时,您可以使用:

Connection connection = DriverManager.getConnection("jdbc:mysql://localhost/?user=root&password=rootpassword"); 
PreparedStatement ps = connection.prepareStatement("CREATE DATABASE databasename");
int result = ps.executeUpdate();

Here is an identical scenario.

这里有一个相同的场景。

#2


40  

This can help you:

这可以帮助你:

GRANT ALL PRIVILEGES ON *.* TO 'root'@'localhost' IDENTIFIED BY '%password%' WITH GRANT OPTION;

Execute it with command line or some GUI tool.

用命令行或GUI工具执行它。

Don't forget to replace %password% with real password.

不要忘记用真实密码替换%password%。

#3


5  

I had a similar problem, but the differemce was: I didn't executed my JavaApp from localhost, but from a remote PC. So I got something like java.sql.SQLException: Access denied for user 'root'@'a.remote.ip.adress' (using password: YES) To solve this, you can simply login to phpMyAdmin, go to Users, click add user and enter the host from which you want to execute your JavaApp (or choose Any Host)

我有一个类似的问题,但是不同的是:我没有从本地主机上执行我的JavaApp,而是从远程PC上执行。我得到了java。sql。SQLException:用户“root”@ .remote.ip拒绝访问。adress(使用密码:是的)来解决这个问题,你可以登录phpMyAdmin,访问用户,点击添加用户,然后输入你想要执行JavaApp的主机(或者选择任何主机)

#4


3  

you can use this

你可以使用这个

   static final String JDBC_DRIVER = "com.mysql.jdbc.Driver";  
   static final String DB_URL = "jdbc:mysql://localhost:3306/YOUR_DB_NAME";


   static final String USER = "root";
   static final String PASS = "YOUR_ROOT_PASSWORD"; 

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

you have to give the right root password .

您必须提供正确的根密码。

#5


2  

You should specify the db you are connecting to:

您应该指定连接到的db:

jdbc:mysql://localhost:3306/mydb

#6


1  

I was hitting the same issue. Added mysql service port number(3307), resolved the issue.

我也遇到了同样的问题。添加了mysql服务端口号(3307),解决了这个问题。

conn = DriverManager.getConnection("jdbc:mysql://localhost:3307/?" + "user=root&password=password");

#7


1  

Try it like this....

试着这样....

public static Connection getConnection() throws SQLException{

    String driver = "com.mysql.jdbc.Driver";
    String url    = "jdbc:mysql://localhost:3306/test";
    String username = "root";
    String password = "vicky";            // Change it to your Password
    System.setProperty(driver,"");

    return DriverManager.getConnection(url,username,password);
}

#8


1  

Although this may not be the cause of your issue, you'll get the same error if there are two MySQL services running on the same port. You can check on windows by looking at the list of services in the services tab of Task Manager.

尽管这可能不是您的问题的原因,但是如果在同一个端口上运行两个MySQL服务,您将会得到相同的错误。您可以通过查看任务管理器的services选项卡中的服务列表来检查windows。

#9


1  

This Exception is also caused due to version mismatch of mysql db and your pom.xml/jar.

这个异常也是由于mysql db和您的pom.xml/jar版本不匹配造成的。

Make sure your pom.xml/jar version is higher than your mysql db version. Because higher versions are compatible with lower version's, but the same is not true for the inverse.

确保你的砰的一声。xml/jar版本比您的mysql db版本高。因为更高的版本与较低的版本兼容,但是对于相反的版本来说是不一样的。

Solution's for java project

解决方案的java项目

  • Replace the Jar with suitable version.
  • 用合适的版本替换罐子。

Solution for maven based

解决方案基于maven

  • Change the dependency Version in pom.xml
  • 更改pom.xml中的依赖项版本。

Solution for spring boot - Override the spring boot dependency by adding 5.1.5.Final

spring启动的解决方案-通过添加5.1.5.Final来覆盖spring启动依赖项。

#10


0  

I had the same issue "java.sql.SQLException: Access denied for user 'root'@'localhost' (using password: YES)". Problem was "WRONG PASSWORD". Copy and paste the query as-it-is in the shell to check whether it gives the desired output or not. Small errors consumes more time.

我有同样的问题“java.sql”。SQLException:用户“root”@“localhost”(使用密码:YES)拒绝访问。问题是“错误密码”。复制并粘贴查询,它位于shell中,以检查它是否提供了所需的输出。小错误会消耗更多的时间。

#11


0  

I had to change my SQL setup (in my host, Dreamhost in my case) to allow the username to access the database from other hosts, not just Dreamhost since I'm running a program from the IDE on my computer. I did that for now by adding % to the list of Allowable Hosts. It now works!

我必须改变我的SQL设置(在我的主机中,在我的例子中是Dreamhost),允许用户名从其他主机访问数据库,而不仅仅是Dreamhost,因为我在我的计算机上运行了一个IDE程序。我这样做是为了增加%到允许的主机列表。它现在工作了!

#12


-1  

I encountered this error. The problem was that I had the wrong database name.

我遇到了这个错误。问题是我的数据库名错了。