I'm creating a spring boot project. Then i try to connect mysql database to that project. I got the error called "HikariPool-1 - Exception during pool initialization".
This is the whole stacktrace.
2022-01-15 11:09:29.374 INFO 2748 --- [nio-8888-exec-3] com.zaxxer.hikari.HikariDataSource : HikariPool-1 - Starting...
2022-01-15 11:09:30.481 ERROR 2748 --- [nio-8888-exec-3] com.zaxxer.hikari.pool.HikariPool: HikariPool-1 - Exception during pool initialization.
java.sql.SQLException: Access denied for user 'root'@'localhost' (using password: YES)
at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:129) ~[mysql-connector-java-8.0.27.jar:8.0.27]
2022-01-15 11:09:30.490 ERROR 2748 --- [nio-8888-exec-3] o.a.c.c.C.[.[.[/].[dispatcherServlet] : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is org.mybatis.spring.MyBatisSystemException: nested exception is org.apache.ibatis.exceptions.PersistenceException:
### Error querying database. Cause:org.springframework.jdbc.CannotGetJdbcConnectionException: Failed to obtain JDBC Connection; nested exception is java.sql.SQLException: Access denied for user 'root'@'localhost' (using password: YES)
### The error may exist in com/example/reservation/mapper/userMapper.java (best guess)
### The error may involve com.example.reservation.mapper.userMapper.findAll
### The error occurred while executing a query
### Cause: org.springframework.jdbc.CannotGetJdbcConnectionException: Failed to obtain JDBC Connection; nested exception is java.sql.SQLException: Access denied for user 'root'@'localhost' (using password: YES)] with root cause
My is as follows,application.yml
java.sql.SQLException: Access denied for user 'root'@'localhost' (using password: YES)
at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:129) ~[mysql-connector-java-8.0.27.jar:8.0.27]
My is as follows,application.yml
server:
port: 8888
spring:
datasource:
url: jdbc:mysql://127.0.0.1:3306/reservation?useUnicode=true&characterEncoding=UTF-8&serverTimezone=UTC
username: root
password: "123456"
driver-class-name: com.mysql.cj.jdbc.Driver
My dependancies are,pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.6.2</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>reservation</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>reservation</name>
<description>reservation project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.2.0</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<excludes>
<exclude>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</exclude>
</excludes>
</configuration>
</plugin>
</plugins>
</build>
</project>
user.java
@Data
@AllArgsConstructor
@NoArgsConstructor
public class user {
private Integer id;
private String username;
private String password;
}
userMapper.java
public interface userMapper {
@Select("select * from user")
List<user> findAll();
}
userMapper.java
public class userController {
@Resource
userMapper usermapper;
@GetMapping()
public String hello(){
return "hello";
}
@GetMapping("/user")
public List<user> getUser(){
return usermapper.findAll();
}
}
ReservationApplication.java
@SpringBootApplication
@MapperScan("com.example.reservation.mapper")
public class ReservationApplication {
public static void main(String[] args) {
SpringApplication.run(ReservationApplication.class, args);
}
}
When I access localhost:8888 it works successfully,
but when I access localhost:8888/user it gives an error
What happened?:(
I'm too stupid to have two MySQL services on my computer without noticing.reason
One of them is installed by myself(mysql57), the other exists in the PhpStudy integrated environment(mysqla).When I found out that there are two MySQL services in Windows services, I was stunned.Usually the service I use is mysql57, but my IDE accesses the service mysqla.
When I first asked the question, the passwords for the root accounts of mysql57 and mysqla were 123456 and root respectively.When I tried many methods to no avail, I changed the password of the mysql57 root account and modified the configuration in the IDE at the same time.
Then a magical scene happened, an error that should not have appeared in the IDE: database xxx does not exist.When I changed the database configuration in the IDE to the myql table that exists in every MySQL, visiting localhost:xx/user returned a set of Josn, then I realized that the problem was in my MySQL, okay Well, this is all caused by the usual habits.