【問(wèn)題描述】
使用MyBatis對(duì)MySQL數(shù)據(jù)庫(kù)中的數(shù)據(jù)表進(jìn)行操作時(shí)出現(xiàn)“Error updating database.”報(bào)錯(cuò)。主要內(nèi)容為:Error updating database. Cause: java.sql.SQLException: Access denied for user 'root'@'localhost' (using password: YES)
【問(wèn)題詳情】
### Error updating database. Cause: java.sql.SQLException: Access denied for user 'root'@'localhost' (using password: YES)
### The error may exist in com/mapper/UserMapper.xml
### The error may involve com.mapper.UserMapper.addUser
### The error occurred while executing an update
### Cause: java.sql.SQLException: Access denied for user 'root'@'localhost' (using password: YES)
at org.apache.ibatis.exceptions.ExceptionFactory.wrapException(ExceptionFactory.java:30)
at org.apache.ibatis.session.defaults.DefaultSqlSession.update(DefaultSqlSession.java:199)
at org.apache.ibatis.session.defaults.DefaultSqlSession.insert(DefaultSqlSession.java:184)
at com.service.impl.UserServiceImpl.addUser(UserServiceImpl.java:64)
at test.Test.main(Test.java:11)
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:827)
at com.mysql.cj.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:447)
at com.mysql.cj.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:237)
at com.mysql.cj.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:199)
at java.sql.DriverManager.getConnection(DriverManager.java:664)
at java.sql.DriverManager.getConnection(DriverManager.java:208)
at org.apache.ibatis.datasource.unpooled.UnpooledDataSource.doGetConnection(UnpooledDataSource.java:221)
at org.apache.ibatis.datasource.unpooled.UnpooledDataSource.doGetConnection(UnpooledDataSource.java:216)
at org.apache.ibatis.datasource.unpooled.UnpooledDataSource.getConnection(UnpooledDataSource.java:95)
at org.apache.ibatis.datasource.pooled.PooledDataSource.popConnection(PooledDataSource.java:422)
at org.apache.ibatis.datasource.pooled.PooledDataSource.getConnection(PooledDataSource.java:89)
at org.apache.ibatis.transaction.jdbc.JdbcTransaction.openConnection(JdbcTransaction.java:138)
at org.apache.ibatis.transaction.jdbc.JdbcTransaction.getConnection(JdbcTransaction.java:60)
at org.apache.ibatis.executor.BaseExecutor.getConnection(BaseExecutor.java:336)
at org.apache.ibatis.executor.SimpleExecutor.prepareStatement(SimpleExecutor.java:85)
at org.apache.ibatis.executor.SimpleExecutor.doUpdate(SimpleExecutor.java:49)
at org.apache.ibatis.executor.BaseExecutor.update(BaseExecutor.java:117)
at org.apache.ibatis.executor.CachingExecutor.update(CachingExecutor.java:76)
at org.apache.ibatis.session.defaults.DefaultSqlSession.update(DefaultSqlSession.java:197)
... 3 more
【初步檢查】
根據(jù)報(bào)錯(cuò)內(nèi)容檢查相應(yīng)的UserMapper.xml文件的代碼后,確定代碼邏輯、語(yǔ)法無(wú)誤。
【問(wèn)題分析】
報(bào)錯(cuò)內(nèi)容“Access denied for user 'root'@'localhost' (using password: YES)”表達(dá)的直接意思是:名為“root”的用戶在“l(fā)ocalhost”端口使用密碼對(duì)數(shù)據(jù)庫(kù)的訪問(wèn)被拒絕了。
初步猜想有兩種情況導(dǎo)致:1.數(shù)據(jù)庫(kù)服務(wù)未啟動(dòng)。 2.連接數(shù)據(jù)庫(kù)的用戶名或密碼錯(cuò)誤。
【解決方案】
1.使用管理員權(quán)限,通過(guò)命令提示符打開(kāi)mysql數(shù)據(jù)庫(kù)的服務(wù)。
2.使用管理員權(quán)限,通過(guò)命令提示符重置密碼。
【執(zhí)行解決方案1】
使用管理員權(quán)限運(yùn)行命令提示符,在彈出的窗口輸入:“net start mysql;”回車(chē)。啟動(dòng)數(shù)據(jù)庫(kù)服務(wù)后,發(fā)現(xiàn)問(wèn)題依然存在。
【執(zhí)行解決方案2】
接下來(lái)重置數(shù)據(jù)庫(kù)8.0.16的root密碼。
【解決完成】
重置root密碼后,按照新重置的密碼編輯代碼,連接數(shù)據(jù)庫(kù)成功。
【總結(jié)】
本次問(wèn)題的癥結(jié)是:遺忘了數(shù)據(jù)庫(kù)root密碼,編輯代碼時(shí)使用了錯(cuò)誤的密碼訪問(wèn)數(shù)據(jù)庫(kù),導(dǎo)致訪問(wèn)被拒絕。