why spring-boot can't connect to local mysql server(Access denied for user 'root'#'localhost' (using password: YES)) - mysql

I encountered a very strange phenomenon about Spring boot and use mysql to store data, today in my private computer I git cloned my developing project. When run the entry main class in eclipse, it throws exception
2015-11-15 18:28:08.912 ERROR 9183 --- [ restartedMain] o.a.tomcat.jdbc.pool.ConnectionPool : Unable to create initial connections of pool.
java.sql.SQLException: Access denied for user 'root'#'localhost' (using password: YES)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:998) ~[mysql-connector-java-5.1.36.jar:5.1.36]
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3847) ~[mysql-connector-java-5.1.36.jar:5.1.36]
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3783) ~[mysql-connector-java-5.1.36.jar:5.1.36]
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:871) ~[mysql-connector-java-5.1.36.jar:5.1.36]
at com.mysql.jdbc.MysqlIO.proceedHandshakeWithPluggableAuthentication(MysqlIO.java:1665) ~[mysql-connector-java-5.1.36.jar:5.1.36]
at com.mysql.jdbc.MysqlIO.doHandshake(MysqlIO.java:1207) ~[mysql-connector-java-5.1.36.jar:5.1.36]
at com.mysql.jdbc.ConnectionImpl.coreConnect(ConnectionImpl.java:2249) ~[mysql-connector-java-5.1.36.jar:5.1.36]
at com.mysql.jdbc.ConnectionImpl.connectOneTryOnly(ConnectionImpl.java:2280) ~[mysql-connector-java-5.1.36.jar:5.1.36]
at com.mysql.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:2079) ~[mysql-connector-java-5.1.36.jar:5.1.36]
at com.mysql.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:794) ~[mysql-connector-java-5.1.36.jar:5.1.36]
at com.mysql.jdbc.JDBC4Connection.<init>(JDBC4Connection.java:44) ~[mysql-connector-java-5.1.36.jar:5.1.36]
it's application.properties :
spring.datasource.url = jdbc:mysql://localhost:3306/foo?useUnicode=true&characterEncoding=utf8
spring.datasource.username = root
spring.datasource.password = 123456
but I could login mysql directly in terminal, see below:
mysql -hlocalhost -uroot -p123456
Warning: Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 50
Server version: 5.6.27-0ubuntu0.14.04.1 (Ubuntu)
and I even write a sample java code in the same project to test connection with local mysql server,
public static void main(String[] args) throws SQLException {
String url = "jdbc:mysql://localhost:3306/foo?useUnicode=true&characterEncoding=utf8";
String user = "root";
String password = "123456";
Connection connection = DriverManager.getConnection(url , user , password);
System.out.println(connection);
Statement stat = connection.createStatement();
ResultSet rs = stat.executeQuery("select version()");
rs.next();
System.out.println(rs.getString(1));
}
Run it have normal output:
com.mysql.jdbc.JDBC4Connection#48cf768c
5.6.27-0ubuntu0.14.04.1
So why spring-boot can't connect to local mysql server, but other ways could?

Related

User can access MySQL DB through workbench, but not through VB .NET Code. Using ssl tunel to external server

I am using same connection data, TCP/IP over SSH: ssh hostname, username, password, MySQL Hostname, server port, username, password
I have used MySql.Data.MySqlClient and MySqlConnector, and what I get is: "MySql.Data.MySqlClient.MySqlException: 'Authentication to host 'localhost' for user 'myuser' using method 'mysql_native_password' failed with message: Access denied for user 'myuser'#'localhost' (using password: YES)'" or "MySqlConnector.MySqlException: Access denied for user 'myuser'#'localhost' (using password: YES)'"
I am using the same user and password I use in workbench, where it runs ok
I have tried this code
connectionInfo = New PasswordConnectionInfo("w.x.y.z", 22, "MyUserSsl", "MyPasswordSsl") With {
.Timeout = TimeSpan.FromSeconds(30)
}
client = New SshClient(connectionInfo)
client.Connect()
If Not client.IsConnected Then
MsgBox("Wrong")
End If
portFwld = New ForwardedPortLocal("127.0.0.1", 0, "127.0.0.1", 3306)
client.AddForwardedPort(portFwld)
portFwld.Start()
con = New MySqlConnection With {
.ConnectionString = "port=" + CStr(portFwld.BoundPort) + ";Database=MyDB;Data Source=localhost;User Id=MyUserDB;Password=MyPassword"
}
and this portFwld = New ForwardedPortLocal("127.0.0.1", 3305, "127.0.0.1", 3306)
and this portFwld = New ForwardedPortLocal("127.0.0.1", 3306, "127.0.0.1", 3306)
and also this
con = New MySqlConnection With {
.ConnectionString = "Database=MyDB;Data Source=localhost;User Id=MyUserDB;Password=MyPassword"
}
it looks like there is user permissions issue
see gc5 answer:
"Connect failed: Access denied for user 'root'#'localhost' (using password: YES)" from php function

sqlalchemy.exc.OperationalError: (pymysql.err.OperationalError) (1045, "Access denied for user 'root'#'localhost' (using password: YES)")

I have used SQLalchemy in python3 to connect to local MySQL.
However, I got such error: sqlalchemy.exc.OperationalError: (pymysql.err.OperationalError) (1045, "Access denied for user 'root'#'localhost' (using password: YES)")
The password is the same as in MySQL workbench, that is "root", I do not know why I got this error.
My code:
engine = create_engine("mysql+pymysql://root:root#localhost/db?host=localhost?port=3308", echo=True)
with engine.connect() as conn, conn.begin():
book_df.to_sql(name='test', con=conn, if_exists='append', index=False)
Already answered at the top, but if you want connect to RDS and if you have special characters in your password like "#" be sure to use urllib.parse.quote or else you will encounter the same error.
import urllib
user = "user"
password = urllib.parse.quote("p#ssword")
endpoint = "rds_endpoint"
port = "port"
db = "db_name"
engine = create_engine(f"mysql+pymysql://{user}:{password}#{endpoint}:{port}/{db}", echo=True)

springboot,Can't connect to my database java.sql.SQLException: Access denied for user 'root'#'localhost' (using password: YES)

java.sql.SQLException: Access denied for user 'root'#'localhost' (using password: YES)
at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:129)
at com.mysql.cj.jdbc.exceptions.SQLExceptionsMapping.translateException(SQLExceptionsMapping.java:122)
at com.mysql.cj.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:828)
at com.mysql.cj.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:448)
Process finished with exit code -1
my test
#SpringBootTest
class Springboot04DataApplicationTests {
#Autowired
DataSource dataSource;
#Test
void contextLoads() throws SQLException {
System.out.println(dataSource.getClass());
Connection connection = dataSource.getConnection();
System.out.println(connection);
}
}
here is my application.yml
spring:
datasource:
username: root
password: 011575
url: jdbc:mysql://localhost:3306/mybatis?useSSL=false&useUnicode=true&characterEncoding=UTF-8&allowPublicKeyRetrieval=true
driver-class-name: com.mysql.cj.jdbc.Driver
I confirmed that my username is root and my pwd is 011575.And also open the mysql service, please help me!!
When springboot reads the data in yml, it will read the pure digital data starting with the number 0 in binary, octal or decimal, and the read value will be different from the original stored value.
so just change your application.yml like this:
password: "011575"
The username and password should be like this
username: root
password: "011575"

Locall MySQL DB login Error : Access denied for user 'root'#'localhost' (using password: YES)

I have the weirdest MySQL login problem ever!
The DBMS is installed on my desktop and I'm accessing it locally, not through a network or internet.
When I try to connect to it via cmd:
mysql -u root -proot;
it connects perfectly..
but, when I try to connect through a Java program in NetBeans:
Connection con = null;
String url ="jdbc:mysql://localhost:3306/testdb";
String user ="root";
String password ="root";
try
{
con = DriverManager.getConnection(url,user,password);
}
Catch(Exception e)
{
System.out.println(e.toString() );
}
it throws an exception !!
java.sql.SQLException: Access denied for user 'root'#'localhost' (using password: YES)
I have the mysql-connector-java-5.1.24-bin.jar library added to NetBeans..
It was working before, not sure why it is not working now!!
Also, when I connect to MySQL like this:
mysql -u root -p;
then I enter the password, it gives this error:
ERROR 1045 (28000): Access denied for user 'root'#'localhost' (using password: YES)
??
Connection con = null;
String dbase = "database";
String dbuser = "root";
String dbpass = "";
try{
String ConnString;
Class.forName("com.mysql.jdbc.Driver").newInstance();
ConnString = "jdbc:mysql://localhost:3306/"+dbase+"?user="+dbuser+"&password="+dbpass;
con = DriverManager.getConnection(ConnString);
//System.out.println("Connection to database successful!");
}catch(Exception e){
e.printStackTrace();
}
Try this?
so stupid ~~~
I found the problem ...
Here's why this is happening and how I solved it:
Reason of problem: I changed the password for root from blank to 'root'
Then, I couldn't connect to MySQL from Java ..
it turns out I have to update the privileges of the database after changing the password.
So I did that by:
GRANT ALL PRIVILEGES ON [database_name].* to '[user]'#'[hostname]' identified by '[password]'
and now it works 100%

Grails 2.2.0 mysql access rights and permissions

I've started getting weird access denied errors running grails 2.2.0 and mysql (mysql Ver 14.14 Distrib 5.6.10, for osx10.7 (x86_64) using EditLine wrapper) on Mac OS X.
Caused by: java.sql.SQLException: Access denied for user 'nfuser'#'localhost' (using password: YES)
Sample code
dataSource {
pooled = true
driverClassName = "com.mysql.jdbc.Driver"
username = "nfuser"
password = "nfuser"
dialect = org.hibernate.dialect.MySQL5InnoDBDialect
}
..
development {
dataSource {
dbCreate = "create-drop" // one of 'create', 'create-drop', 'update', 'validate', ''
url = "jdbc:mysql://localhost:3306/nf_dev"
}
}
The usual grants and db create before running grails.
GRANT all ON nf_dev.* TO 'nfuser'#'%' identified by 'nfuser';
Tested with a brand new project and of course it works.. Ideas?
/S
Duuh, external property file override on dataSource.password..