59

以下代码:

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

引发此异常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)

这是如何引起的,我该如何解决?

编辑:

    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();
        }
}

生产:

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)
4

28 回答 28

59

这可以帮助您:

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

使用命令行或一些 GUI 工具执行它。

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

于 2012-08-12T13:23:18.930 回答
22

这特定于Ubuntu 18.04 LTS 和 MySQL 5.x

sudo mysql_secure_installation

sudo mysql

登录 MySQL 后,从 MySQL 提示符执行以下命令:

ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'password';
FLUSH PRIVILEGES;

现在验证该表是否具有 root 的密码

SELECT user,authentication_string,plugin,host FROM mysql.user;

这解决了我的问题,现在我可以登录了。

于 2018-09-23T04:41:30.820 回答
21

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

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

这是一个相同的场景

于 2012-08-12T13:04:13.320 回答
9

我遇到了类似的问题,但不同之处在于:我没有从 执行我的 JavaApp localhost,而是从远程 PC 执行。所以我得到了类似java.sql.SQLException: Access denied for user 'root'@'a.remote.ip.adress' (using password: YES) 解决这个问题的方法,您可以简单地登录到 phpMyAdmin,转到Users,单击add user并输入您要从中执行 JavaApp 的主机(或选择Any Host

于 2015-06-20T18:16:53.940 回答
7

你可以用这个

   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);

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

于 2015-10-13T13:43:02.670 回答
5

我有同样的问题,如下所示

"java.sql.SQLException: Access denied for user 'root'@'localhost' (using password: YES)". Problem was "WRONG PASSWORD". 

将查询原样复制并粘贴到 shell 中以检查它是否提供所需的输出。小错误会消耗更多时间。

于 2016-09-08T20:41:49.477 回答
3

这似乎主要发生在 MySQL 用户名和密码不正确时。检查您的 MySQL 用户名和密码。

于 2018-05-29T13:17:40.407 回答
2

您应该指定要连接到的数据库:

jdbc:mysql://localhost:3306/mydb
于 2012-08-12T13:00:20.493 回答
1

试试这样......

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);
}
于 2012-08-12T13:00:07.913 回答
1

我遇到了同样的问题。新增mysql服务端口号(3307),解决问题。

conn = DriverManager.getConnection("jdbc:mysql://localhost:3307/?" + "user=root&password=password");
于 2013-07-05T11:21:18.150 回答
1

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

于 2016-06-21T20:00:34.773 回答
1

此异常也是由于 mysql db 和您的 pom.xml/jar 的版本不匹配引起的。

确保您的 pom.xml/jar 版本高于您的 mysql 数据库版本。因为较高版本与较低版本兼容,但反过来则不然。

java项目的解决方案

  • 用合适的版本替换 Jar。

基于maven的解决方案

  • 更改 pom.xml 中的依赖版本

spring boot 的解决方案 - 通过添加 5.1.5.Final 覆盖 spring boot 依赖项

于 2017-11-03T05:14:20.930 回答
1

我正在使用 Spring Boot 2.2.6(Windows) 并在尝试运行应用程序时遇到了同样的问题: java.sql.SQLException: Access denied for user 'root'@'localhost' (using password: YES)

什么解决了我的问题:

  1. 创建一个新用户(从 MySQL 工作台或您可能正在使用的类似 GUI)
  2. 也从 GUI 授予 DBA 特权(勾选 DBA 复选框)
  3. 运行弹簧启动应用程序。

或者按照@evg 解决方案从 Linux 环境中的命令行授予权限。

于 2020-04-16T06:39:47.277 回答
1

application.properties看起来像这样

spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/mydb

spring.datasource.username=root

spring.datasource.password=root

唯一对我有用的是 maven clean 然后 maven install 。

于 2020-09-08T16:37:44.347 回答
0

我必须更改我的 SQL 设置(在我的主机中,在我的情况下为 Dreamhost)以允许用户名从其他主机访问数据库,而不仅仅是 Dreamhost,因为我正在我的计算机上从 IDE 运行程序。我现在通过在允许的主机列表中添加 % 来做到这一点。现在可以了!

于 2017-11-16T02:18:44.033 回答
0

申报顺序:

  • 司机
  • 连接字符串
  • 用户名
  • 密码

如果我们按顺序声明

  • 连接字符串
  • 用户名
  • 密码
  • 司机

应用程序将失败

于 2019-06-02T00:58:20.977 回答
0
  1. 使用正确的 jar(正确的版本)
  2. 授予 root 用户独立于主机的访问权限或创建用户
于 2019-12-30T19:31:41.007 回答
0

对我来说,解决方案通过将 url 格式更改为: con=DriverManager.getConnection("jdbc:mysql://localhost/w3schools?user=###&password=###"); 确保将正确的 jar 添加到类路径并使用驱动程序 Class.forName("com.mysql.jdbc.Driver");

于 2020-03-13T18:23:31.897 回答
0

这为我解决了问题。

ALTER USER 'root'@'%' IDENTIFIED WITH mysql_native_password BY 'password';

GRANT ALL PRIVILEGES ON *.cfe TO 'root'@'%' IDENTIFIED BY 'password';

FLUSH PRIVILEGES;
于 2020-06-04T08:24:12.933 回答
0

我也面临同样的问题,我的连接字符串有效,用户名和密码也有效,即使用户有足够的权限访问数据库。

我通过删除 MySQL 创建的临时文件夹解决了这个问题(请在删除任何文件夹之前进行备份,以防万一它不起作用,那么您可以再次放置该文件)。

Windows 中 MySQL 的临时文件和连接文件的默认文件夹位置是:

C:\ProgramData\MySQL

我删除(备份)以下文件夹:

C:\ProgramData\MySQL\MySQL 服务器\Data\sys

注意:在删除任何项目之前,请确保 MySQL 服务没有启动,否则它不允许删除任何文件

它对我有用。

于 2020-06-13T13:02:17.373 回答
0

我在运行springboot项目时,application.yml配置是这样的:</p>

server:
  port: 8080
spring:
  datasource:
    url: jdbc:mysql://localhost:3306/lof?serverTimezone=GMT
    username: root
    password: root
    driver-class-name: com.mysql.cj.jdbc.Driver

请注意,密码周围没有引号。我可以在我的 Windows 系统中运行这个项目。

但是当我尝试部署到服务器时,我遇到了问题,我通过将 application.yml 更改为:

server:
  port: 8080
spring:
  datasource:
    url: jdbc:mysql://localhost:3306/lof?serverTimezone=GMT
    username: root
    password: "root"
    driver-class-name: com.mysql.cj.jdbc.Driver
于 2021-02-13T13:49:55.893 回答
0

在以下位置添加端口号(类似于 3306):

Connection c = DriverManager.getConnection("jdbc:mysql://localhost:urport","root","root");
于 2021-02-14T13:38:48.080 回答
0

在执行以下语句后使用相同的密码对我来说工作正常:

ALTER USER 'root'@'localhost' IDENTIFIED BY 'root';
于 2021-09-05T12:02:58.413 回答
0
spring.jpa.generate-ddl=true
spring.jpa.show-sql=true
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL5Dialect
spring.jpa.hibernate.ddl-auto=create-drop
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.username=root
spring.datasource.password=
spring.datasource.url=jdbc:mysql://localhost:3306/DATABASE_NAME? useUnicode=true&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=UTC

转到您的 phpmyadmin 数据库

创建一个名为 DATABASE_NAME 的数据库并转到权限并检查数据库中具有权限的用户并使用它们,默认为“root”

于 2021-12-05T13:20:45.250 回答
-1

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

于 2017-12-18T03:42:26.363 回答
-1

如果您使用 java 从本地计算机连接远程 mysql 服务器,请参阅以下步骤。错误:“java.sql.SQLException:用户'xxxxx'@'101.123.163.141'的访问被拒绝(使用密码:YES)”

远程访问可能是 cpanel 或其他人授予您本地 IP 地址的远程访问权限。

在上面的错误信息中:“101.123.163.141”是我的本地机器ip。因此,首先我们必须在 Cpanel-> Remote MySQL® 中提供远程访问权限。然后运行您的应用程序进行连接。

Class.forName("com.mysql.jdbc.Driver");  
Connection con=DriverManager.getConnection(  
    "jdbc:mysql://www.xyz.com/abc","def","ghi");  
//here abc is database name, def is username and ghi        
Statement stmt=con.createStatement();  
ResultSet rs=stmt.executeQuery("select * from employee");  
    while(rs.next())  
        System.out.println(rs.getInt(1)+" "+rs.getString(2)+"  
                          "+rs.getString(3));  
con.close();    

希望它能解决您的问题。

谢谢

于 2018-11-18T16:32:50.193 回答
-1

我也有这个问题,解决了。

更改:

Sring url = "jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=UTF-8&serverTimezone=UTC&user=root&password=password";

放:

"serverTimezone=UTC""Unified standard world time""useUnicode=true&characterEncoding=UTF-8""Solve Chinese garbled"

虽然我的数据库没有任何中文单词。但它正在工作。参考自https://www.cnblogs.com/EasonJim/p/6906713.html

于 2019-09-16T07:23:15.157 回答
-1

如果您使用的是 MySql 工作台,请进入工作台主页,右键单击您的数据库,复制 JDBC 连接字符串,并将其粘贴到 Java 程序中。

于 2020-01-03T18:04:26.237 回答