MyBatis account password is correct to connect to the database, but Access denied problem still occurs

Problem description and solution

When writing a sample program that MyBatis reads data from the database, the following error message always appears:

Exception in thread "main" org.apache.ibatis.exceptions.PersistenceException: 
### Error querying database.  Cause: java.sql.SQLException: Access denied for user 'root'@'localhost' (using password: YES)
### The error may exist in org/example/dao/StudentDao.xml
### The error may involve org.example.dao.StudentDao.selectStudents
### The error occurred while executing a query
### 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.selectList(DefaultSqlSession.java:153)
	at org.apache.ibatis.session.defaults.DefaultSqlSession.selectList(DefaultSqlSession.java:145)
	at org.apache.ibatis.session.defaults.DefaultSqlSession.selectList(DefaultSqlSession.java:140)
	at org.apache.ibatis.session.defaults.DefaultSqlSession.selectList(DefaultSqlSession.java:135)
	at org.example.MyApp.main(MyApp.java:29)
Caused by: java.sql.SQLException: Access denied for user 'root'@'localhost' (using password: YES)
	at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1055)
	at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:956)
	at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3558)
	at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3490)
	at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:919)
	at com.mysql.jdbc.MysqlIO.secureAuth411(MysqlIO.java:3996)
	at com.mysql.jdbc.MysqlIO.doHandshake(MysqlIO.java:1284)
	at com.mysql.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:2137)
	at com.mysql.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:776)
	at com.mysql.jdbc.JDBC4Connection.<init>(JDBC4Connection.java:46)
	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:406)
	at com.mysql.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:352)
	at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:284)
	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:224)
	at org.apache.ibatis.datasource.unpooled.UnpooledDataSource.doGetConnection(UnpooledDataSource.java:219)
	at org.apache.ibatis.datasource.unpooled.UnpooledDataSource.getConnection(UnpooledDataSource.java:95)
	at org.apache.ibatis.datasource.pooled.PooledDataSource.popConnection(PooledDataSource.java:432)
	at org.apache.ibatis.datasource.pooled.PooledDataSource.getConnection(PooledDataSource.java:89)
	at org.apache.ibatis.transaction.jdbc.JdbcTransaction.openConnection(JdbcTransaction.java:139)
	at org.apache.ibatis.transaction.jdbc.JdbcTransaction.getConnection(JdbcTransaction.java:61)
	at org.apache.ibatis.executor.BaseExecutor.getConnection(BaseExecutor.java:337)
	at org.apache.ibatis.executor.SimpleExecutor.prepareStatement(SimpleExecutor.java:86)
	at org.apache.ibatis.executor.SimpleExecutor.doQuery(SimpleExecutor.java:62)
	at org.apache.ibatis.executor.BaseExecutor.queryFromDatabase(BaseExecutor.java:325)
	at org.apache.ibatis.executor.BaseExecutor.query(BaseExecutor.java:156)
	at org.apache.ibatis.executor.CachingExecutor.query(CachingExecutor.java:109)
	at org.apache.ibatis.executor.CachingExecutor.query(CachingExecutor.java:89)
	at org.apache.ibatis.session.defaults.DefaultSqlSession.selectList(DefaultSqlSession.java:151)
	... 4 more

Process finished with exit code 1

At this time, in the main configuration file of the database, the content of the dataSource tag is as follows:

<dataSource type="POOLED">
    <property name="driver" value="com.mysql.jdbc.Driver"/>
    <property name="url" value="jdbc:mysql://localhost:3307/mybatis_test"/>
    <property name="username" value="root"/>
    <property name="password" value="123"/>
</dataSource>

Access denied for user 'root'@'localhost', That is, access is denied, but the username and password I wrote in the configuration file are correct. I spent an hour or two trying to find out the problem, but to no avail.

The database I am accessing is MariaDB, and there are two databases installed on my computer, one is MySQL, on port 3306, and the other is MariaDB, on port 3307. The account and password of the MySQL database are rootand password(yes, the password is in English), and the account and password of MariaDB are rootand respectively 123.

When learning JDBC before, writing com.mysql.jdbc.Driverand writing jdbc:mysql://localhost:3307/数据库名can normally access the data in MariaDB. Because I knew that the bottom layer of MariaDB is actually very similar to MySQL, I thought that there is no difference in access between MariaDB and MySQL.

But now it doesn't work on MyBatis. Later, I thought, could it be that MariaDB and MySQL have different dependencies? Later, it turned out to be true, so I changed the dependency to MariaDB, as follows:

<!--mariadb依赖-->
<dependency>
    <groupId>org.mariadb.jdbc</groupId>
    <artifactId>mariadb-java-client</artifactId>
    <version>2.7.3</version>
</dependency>

And change the driver tag and url tag in the dataSource tag to MariaDB:

<dataSource type="POOLED">
    <property name="driver" value="org.mariadb.jdbc.Driver"/>
    <property name="url" value="jdbc:mariadb://localhost:3307/mybatis_test"/>
    <property name="username" value="root"/>
    <property name="password" value="123"/>
</dataSource>

Then you can access the MariaDB database normally.

Insert picture description here

to sum up

So why does it report an Access denied error?

It is because MyBatis thought that I was going to access the MySQL database, but the MySQL account password was incorrect, so an access denied error occurred. So modify the configuration, specify that you want to access the MariaDB database, and the problem is solved.