问题
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)
回答1:
I suggest that you carefully go through each and every one of the suggestions in the MySQL Manual's section on connection troubleshooting.
- "Troubleshooting Problems Connecting to MySQL"
Also, check the Connector/J troubleshooting page:
- "Troubleshooting Connector/J Applications"
The latter page includes this useful tip:
Note: Testing your connectivity with the mysql command-line client will not work unless you add the "host" flag, and use something other than localhost for the host. The mysql command-line client will use Unix domain sockets if you use the special host name localhost. If you are testing connectivity to localhost, use 127.0.0.1 as the host name instead.
This says ... in effect ... that you may not be checking the actual connection method that the JDBC driver is using. It could make a difference to authentication.
回答2:
Maybe you have different versions of mysql installed at the same time and you have registered in the enviroment variables another mysql version. Try to check if you have something that you can uninstall. Then, add to the enviroment variable the right mysql in the right folder.
回答3:
...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.
回答4:
Access denied for user 'strada'@'localhost' (using password: YES)
The Exception explains the following:
- 'strada' user do not have enough privileges for performing the operation in DB.
- 'using password: YES' means- The password provided for user 'strada' is right.
Fix:
- Login as root user to DB, provide user 'strada' with DBA (Database Administrator) previlage.
- Reconnect using Correct JDBC connection string.
回答5:
Class.forName("com.mysql.jdbc.Driver");
conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/correctDBname", "correctUsername", "correctPassword");
来源:https://stackoverflow.com/questions/43071975/sqlexception-access-denied-for-user-whateverlocalhost