SQLException:用户'whatever'@'localhost'的访问被拒绝 [英] SQLException: Access denied for user 'whatever'@'localhost'

查看:44
本文介绍了SQLException:用户'whatever'@'localhost'的访问被拒绝的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

凭据是正确的,数据库名称是正确的,MySQL用户设置正确并且具有所有特权,我有'username'@'%'以及'username'@'localhost'的条目,我可以连接到此用户通过命令行正确地以及通过mysql工作台.换句话说,除了我的Java应用程序无法连接之外,一切都应按其应有的方式进行并按应有的方式进行设置.

我做了3次干净的MySQL卸载和安装,没有重启,没有重启,试图以root身份通过应用程序登录(通过),这是行不通的,试图以没有密码的用户身份通过​​应用程序登录(错误)等.

此外,在localhost上运行的PHP网站使用完全相同的登录名也可以正常工作.

过去3天,我一直在检查和重新检查并尝试所有这些方法,我经历了所有相关的SO问题,所有可以找到的博客和文档,所有这些.

try {
  Class.forName("com.mysql.jdbc.Driver").newInstance();
  conn = DriverManager.getConnection("jdbc:mysql://localhost/correctDBname", "correctUsername", "correctPassword");
} catch(ClassNotFoundException | SQLException ex) {
  Logger.getLogger(SqlConnection.class.getName()).log(Level.SEVERE, ex.getMessage(), ex);
}

MySQL驱动程序名称与在项目的NetBeans库"部分中显示的完全相同: MySQL JDBC驱动程序-mysql-connector-java-5.1.23-bin.jar

例外的全文:

java.sql.SQLException: Access denied for user 'strada'@'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.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
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 java.sql.DriverManager.getConnection(DriverManager.java:664)
at java.sql.DriverManager.getConnection(DriverManager.java:247)
at eu.storemedia.stradaserver.engine.SqlConnection.<init>(SqlConnection.java:23)
at eu.storemedia.stradaserver.engine.StradaServer.<init>(StradaServer.java:51)
at eu.storemedia.stradaserver.Home.<clinit>(Home.java:31)

解决方案

...结果表明从.txt文件中读取凭据可能很危险,而以简化它的名义从问题中忽略了这一事实是错误的. /p>

如果我使用硬编码的凭据,则登录有效.如果我使用从错误创建的.txt文件中读取的凭据,则会发生错误.如果我使用从正确创建的.txt文件(不带BOM的utf-8)中读取的凭据,则登录有效.

我已经正确地重写了我从中读取凭据的文件,因此我无法找到任何详细信息,但是基本上它包含一些使凭据无效的不可见字符(可能是BOM)

.

Credentials are correct, database name is correct, MySQL users are set correctly and have all the privileges, I have entries for 'username'@'%' as well as 'username'@'localhost', I can connect to this user via commandline correctly, as well as through mysql workbench. In other words, everything works as it should and is set as it should be, except my java app is unable to connect.

I did about 3 clean uninstalls and installs of MySQL, with restarts, without restarts, tried to login through app as root (with pass) which didn't work, tried to login through app as user with no password (same error), etc.

Also, a PHP website running on localhost using precisely the same login works perfectly.

I've been checking and re-checking and trying all of this for the past 3 days, I went through all the related SO questions, all the blogs and docs I could find, all of it.

try {
  Class.forName("com.mysql.jdbc.Driver").newInstance();
  conn = DriverManager.getConnection("jdbc:mysql://localhost/correctDBname", "correctUsername", "correctPassword");
} catch(ClassNotFoundException | SQLException ex) {
  Logger.getLogger(SqlConnection.class.getName()).log(Level.SEVERE, ex.getMessage(), ex);
}

MySQL driver name precisely as it appears in NetBeans' "Libraries" section in project: MySQL JDBC Driver - mysql-connector-java-5.1.23-bin.jar

Full text of the exception:

java.sql.SQLException: Access denied for user 'strada'@'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.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
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 java.sql.DriverManager.getConnection(DriverManager.java:664)
at java.sql.DriverManager.getConnection(DriverManager.java:247)
at eu.storemedia.stradaserver.engine.SqlConnection.<init>(SqlConnection.java:23)
at eu.storemedia.stradaserver.engine.StradaServer.<init>(StradaServer.java:51)
at eu.storemedia.stradaserver.Home.<clinit>(Home.java:31)

解决方案

...turns out reading credentials from .txt file might be dangerous, and omitting this fact from the question in the name of simplifying it was a mistake.

if I use hardcoded credentials, login works. If I use credentials read from incorrectly created .txt file, the error occurs. If I use credentials read from correctly created .txt file (utf-8 with no BOM), login works.

I've rewritten the file I'm reading the credentials from to be correct, so I can't find out any specifics, but basically it contained some invisible character (possibly BOM) that was making the credentials invalid.

这篇关于SQLException:用户'whatever'@'localhost'的访问被拒绝的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆