Java 用户 'root'@'localhost' 访问被拒绝(使用密码:YES)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16523324/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-16 07:17:07  来源:igfitidea点击:

Access denied for User 'root'@'localhost' (using password: YES )

javamysqljdbcmysql-connector

提问by Ramesh J

I would like to connect with my local MYSQL data base, which is installed along with XAMP server. I created a new User ie, " NewUser " and its password is "password". I given all PRIVILEGES to this user.

我想连接与 XAMP 服务器一起安装的本地 MYSQL 数据库。我创建了一个新用户,即“NewUser”,它的密码是“password”。我给了这个用户所有的特权。

I write the code to connect with data base by using user "root" (No password for this user). Its connected . Like bellow..

我编写代码以使用用户“root”(此用户没有密码)连接数据库。其连接。像下面这样..

return DriverManager.getConnection("jdbc:MySql://localhost/database_name","root","");

Now I wrote the code to connect with same data base by another user ie, "NewUser" and its pasword "password"

现在我编写了代码以连接另一个用户的相同数据库,即“NewUser”及其密码“password”

return DriverManager.getConnection("jdbc:MySql://localhost/database_name","NewUser","password");

but its not connected. The error in console is

但它没有连接。控制台中的错误是

java.sql.SQLException: Access denied for user 'NewUser'@'localhost' (using password: YES)
    at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1074)
    at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:4120)
    at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:4052)
    at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:925)
    at com.mysql.jdbc.MysqlIO.proceedHandshakeWithPluggableAuthentication(MysqlIO.java:1704)
    at com.mysql.jdbc.MysqlIO.doHandshake(MysqlIO.java:1250)
    at com.mysql.jdbc.ConnectionImpl.coreConnect(ConnectionImpl.java:2465)
    at com.mysql.jdbc.ConnectionImpl.connectOneTryOnly(ConnectionImpl.java:2498)
    at com.mysql.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:2283)
    at com.mysql.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:822)
    at com.mysql.jdbc.JDBC4Connection.<init>(JDBC4Connection.java:47)
    at sun.reflect.GeneratedConstructorAccessor207.newInstance(Unknown Source)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:513)
    at com.mysql.jdbc.Util.handleNewInstance(Util.java:411)
    at com.mysql.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:404)
    at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:317)
    at org.eclipse.datatools.connectivity.drivers.jdbc.JDBCConnection.createConnection(JDBCConnection.java:214)
    at org.eclipse.datatools.connectivity.DriverConnectionBase.internalCreateConnection(DriverConnectionBase.java:105)
    at org.eclipse.datatools.connectivity.DriverConnectionBase.open(DriverConnectionBase.java:54)
    at org.eclipse.datatools.connectivity.drivers.jdbc.JDBCConnection.open(JDBCConnection.java:73)
    at org.eclipse.datatools.enablement.internal.mysql.connection.JDBCMySQLConnectionFactory.createConnection(JDBCMySQLConnectionFactory.java:28)
    at org.eclipse.datatools.connectivity.internal.ConnectionFactoryProvider.createConnection(ConnectionFactoryProvider.java:83)
    at org.eclipse.datatools.connectivity.internal.ConnectionProfile.createConnection(ConnectionProfile.java:359)
    at org.eclipse.datatools.connectivity.ui.PingJob.createTestConnection(PingJob.java:76)
    at org.eclipse.datatools.connectivity.ui.PingJob.run(PingJob.java:59)
    at org.eclipse.core.internal.jobs.Worker.run(Worker.java:53)

I give the host type while providing PRIVILEGES to this user, as " any host" ie."%".

我在向该用户提供特权的同时给出了主机类型,作为“任何主机”,即“%”。

If I change this to " localhost " or "127.0.0.1" its working.

如果我将其更改为“ localhost ”或“ 127.0.0.1”,则其工作正常。

So How can i use my database with " anyhost " PRIVILEGES to the particular user like "NewUser" .

那么我如何将我的数据库与“anyhost”特权一起用于特定用户,例如“NewUser”。

If I got success here then I successes in connection to client live Data base..

如果我在这里取得成功,那么我就成功连接到客户端实时数据库。

Thanks to all and please let me out from this one.....

感谢所有人,请让我离开这个......

回答by hd1

  1. your URL is meant to be all lowercase -- `jdbc:MySql://localhost/database_name"
  2. Is your database really called database_name?
  1. 你的 URL 应该全部小写——`jdbc:MySql://localhost/database_name"
  2. 你的数据库真的叫database_name吗?

回答by Sharky

Please try this syntax

请试试这个语法

return DriverManager.getConnection("jdbc:mysql://localhost/database_name?user=NewUser&password=your_password_here");

(just replace your password where it says "your_password_here")

(只需将您的密码替换为“your_password_here”)

EDIT - SECOND GUESS: Its possible you have firewalled yourself by blocking 192.168.x.x range.

编辑 - 第二个猜测:您可能通过阻止 192.168.xx 范围来为自己设置防火墙。

i assume you running windows. open up command prompt, type "ipconfig" press enter. see your ipv4 adress (should be something like 192.168.1.x)

我假设你运行 Windows。打开命令提示符,输入“ipconfig”按回车。查看您的 ipv4 地址(应该类似于 192.168.1.x)

Be sure your antivirus/firewall program permits connection from 192.168.1.x (which is yourself) and then try using that instead of "%" or "localhost"

确保您的防病毒/防火墙程序允许来自 192.168.1.x(您自己)的连接,然后尝试使用它而不是“%”或“localhost”

If this doesn't work, close all your firewall/antivirus and try again.

如果这不起作用,请关闭所有防火墙/防病毒软件,然后重试。

Also try reloading privileges either by:

还可以尝试通过以下方式重新加载权限:

  • restarting xampp
  • FLUSH PRIVILEGES;
  • 重启xampp
  • 同花顺特权;

回答by Biggie Mac

Since the first specified code works and also based on the reported trace, i'm pretty sure the problem is on the database, not the code syntax.

由于第一个指定的代码有效并且还基于报告的跟踪,我很确定问题出在数据库上,而不是代码语法上。

Based on the Mysql version, please try as an alternative to set privileges without specifying the any host (%) as based on Mysql documentation,

基于 Mysql 版本,请尝试根据Mysql 文档,在不指定任何主机 (%) 的情况下设置权限,

The simple form user_name is a synonym for user_name@'%'

简单形式 user_name 是 user_name@'%' 的同义词

Also flush privileges immediately after using FLUSH PRIVILEGES;

使用后立即刷新权限 FLUSH PRIVILEGES;

Just to make sure everything is correct, also run a SHOW GRANTS FOR NewUser;and check if NewUser appears in the list with the corresponding permissions.

只是为了确保一切正确,还运行 a SHOW GRANTS FOR NewUser;并检查 NewUser 是否出现在具有相应权限的列表中。

回答by TomS

In order to give users access to your database, you need to specify a host where they should be allowed to connect from. But please be careful: Even if you use a wildcard (%) as the host name, the user cannot connect from localhost. They can connect from ANY host, but not from localhost. When connecting from localhost, I assume your mysql installation assumes you to be an anonymous user. In order to allow a user to connect from localhost, you need to add a separate user whith "localhost" in the host field.

为了让用户访问您的数据库,您需要指定一个允许他们连接的主机。但请注意:即使您使用通配符 (%) 作为主机名,用户也无法从 localhost 连接。他们可以从任何主机连接,但不能从本地主机连接。从本地主机连接时,我假设您的 mysql 安装假设您是匿名用户。为了允许用户从本地主机连接,您需要在主机字段中添加一个带有“本地主机”的单独用户。

For more details, please refer to the MySQL documentation

更多详情请参考MySQL 文档

Two of the accounts have a user name of monty and a password of some_pass. Both accounts are superuser accounts with full privileges to do anything. The 'monty'@'localhost' account can be used only when connecting from the local host. The 'monty'@'%' account uses the '%' wildcard for the host part, so it can be used to connect from any host.

It is necessary to have both accounts for monty to be able to connect from anywhere as monty. Without the localhost account, the anonymous-user account for localhost that is created by mysql_install_db would take precedence when monty connects from the local host. As a result, monty would be treated as an anonymous user. The reason for this is that the anonymous-user account has a more specific Host column value than the 'monty'@'%' account and thus comes earlier in the user table sort order. (user table sorting is discussed in Section 6.2.4, “Access Control, Stage 1: Connection Verification”.)

其中两个帐户的用户名是 monty,密码是 some_pass。这两个帐户都是具有执行任何操作的完全权限的超级用户帐户。'monty'@'localhost' 帐户只能在从本地主机连接时使用。'monty'@'%' 帐户使用 '%' 通配符作为主机部分,因此它可用于从任何主机连接。

必须同时拥有 monty 的两个帐户才能从任何地方作为 monty 进行连接。如果没有 localhost 帐户,当 monty 从本地主机连接时,由 mysql_install_db 创建的 localhost 的匿名用户帐户将优先。因此,monty 将被视为匿名用户。这样做的原因是匿名用户帐户具有比 'monty'@'%' 帐户更具体的主机列值,因此在用户表排序顺序中排在更早的位置。(用户表排序在第 6.2.4 节“访问控制,阶段 1:连接验证”中讨论。)

回答by brijesh

GO to users/privileges/edit privileges/ change password or select no password update the password in hibernate config file and restart the server.

转到用户/权限/编辑权限/更改密码或选择无密码更新休眠配置文件中的密码并重新启动服务器。