执行数据库保存提示:Caused by:java.sql.SQLException:Access denied for user

程序连接mysql时,报错:Caused by: java.sql.SQLException: Access denied for user ‘root’@’localhost’ (using password: YES)
具体异常如下:

Caused by: 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.SQLError.createSQLException(SQLError.java:97)
	at com.mysql.cj.jdbc.exceptions.SQLExceptionsMapping.translateException(SQLExceptionsMapping.java:122)
	at com.mysql.cj.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:836)
	at com.mysql.cj.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:456)
	at com.mysql.cj.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:246)
	at com.mysql.cj.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:197)
	at java.sql.DriverManager.getConnection(DriverManager.java:664)
	at java.sql.DriverManager.getConnection(DriverManager.java:247)
	at com.itzhimei.sink.Sink_5_Jdbc$MyJdbcSink.open(Sink_5_Jdbc.java:62)
	at org.apache.flink.api.common.functions.util.FunctionUtils.openFunction(FunctionUtils.java:34)
	at org.apache.flink.streaming.api.operators.AbstractUdfStreamOperator.open(AbstractUdfStreamOperator.java:100)
	at org.apache.flink.streaming.api.operators.StreamSink.open(StreamSink.java:46)
	at org.apache.flink.streaming.runtime.tasks.RegularOperatorChain.initializeStateAndOpenOperators(RegularOperatorChain.java:110)
	at org.apache.flink.streaming.runtime.tasks.StreamTask.restoreGates(StreamTask.java:711)
	at org.apache.flink.streaming.runtime.tasks.StreamTaskActionExecutor$1.call(StreamTaskActionExecutor.java:55)
	at org.apache.flink.streaming.runtime.tasks.StreamTask.restoreInternal(StreamTask.java:687)
	at org.apache.flink.streaming.runtime.tasks.StreamTask.restore(StreamTask.java:654)
	at org.apache.flink.runtime.taskmanager.Task.runWithSystemExitMonitoring(Task.java:958)
	at org.apache.flink.runtime.taskmanager.Task.restoreAndInvoke(Task.java:927)
	at org.apache.flink.runtime.taskmanager.Task.doRun(Task.java:766)
	at org.apache.flink.runtime.taskmanager.Task.run(Task.java:575)
	at java.lang.Thread.run(Thread.java:748)
	Suppressed: java.lang.NullPointerException
		at com.itzhimei.sink.Sink_5_Jdbc$MyJdbcSink.close(Sink_5_Jdbc.java:82)
		at org.apache.flink.api.common.functions.util.FunctionUtils.closeFunction(FunctionUtils.java:41)
		at org.apache.flink.streaming.api.operators.AbstractUdfStreamOperator.close(AbstractUdfStreamOperator.java:114)
		at org.apache.flink.streaming.runtime.tasks.StreamOperatorWrapper.close(StreamOperatorWrapper.java:141)
		at org.apache.flink.streaming.runtime.tasks.RegularOperatorChain.closeAllOperators(RegularOperatorChain.java:127)
		at org.apache.flink.streaming.runtime.tasks.StreamTask.closeAllOperators(StreamTask.java:1035)
		at org.apache.flink.streaming.runtime.tasks.StreamTask.runAndSuppressThrowable(StreamTask.java:1021)
		at org.apache.flink.streaming.runtime.tasks.StreamTask.cleanUp(StreamTask.java:928)
		at org.apache.flink.runtime.taskmanager.Task.lambda$restoreAndInvoke$0(Task.java:940)
		at org.apache.flink.runtime.taskmanager.Task.runWithSystemExitMonitoring(Task.java:958)
		at org.apache.flink.runtime.taskmanager.Task.restoreAndInvoke(Task.java:940)
		... 3 more

原因是因为密码错误。此时要重置myslq账号密码,以MySQL 8.版本修改root密码为例,操作步骤如下:

1、MySQL 8.版本 修改root密码
查看版本:select version() from dual;

2、命令行登录mysql:
mysql -u root -p
Enter password:
因为我是本地,并且使用navcat连接过,我从本地直接用navcat连接到mysql。

3、MySQL 8.*版本用户和密码在mysql库的user表中:
user:当前数据库的用户名;
authentication_string: 用户密码(后面有提到此字段);

4、开始更新密码:
1)update user set authentication_string=” where user=’root’
2)ALTER user ‘root’@’localhost’ IDENTIFIED BY ‘Abcd12345#’