Springboot jpa/mybatis连接数据库失败
Springboot jpa/mybatis连接数据库失败
记录一下这个困扰我三天的问题。
先记录一下具体错误,再去分析为什么吧
spring-boot提供了两种配置方式来配置项目信息,properties和yml
前提:
我在服务器上用docker创建了一个mysql容器,MySQL版本是5.7.3
在自己本地电脑上装的MySQL版本是8.0.20
在本地的navicat上也连接上了服务器上的mysql数据库
后续:
创建springboot项目后在pom.xml里面配置好了jpa、数据库驱动、连接池如下:
<?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.5.6</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>cn.freespider</groupId>
<artifactId>blog</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>blog</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</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>
</plugin>
</plugins>
</build>
</project>
在src/main/resources目录下创建了一个application.yml文件来配置项目信息,如下:
server:
port: 7796
spring:
datasource:
username: root
password: lql010111*
driver-class-name: com.mysql.jdbc.Driver
url: jdbc:mysql://120.25.247.76:3307/db_my_blog?useSSL=false&serverTimezone=UTC&useUnicode=true&characterEncoding=utf-8
运行之后
报错了,这里困扰了我好久。(IP是对的,端口是对的,用户名密码也是对的,反复确认过绝对没错)
然后我又尝试连接本地的MySQL数据库 yml配置如下:(其实也就只改了IP和端口)
server:
port: 7796
spring:
datasource:
username: root
password: lql010111*
driver-class-name: com.mysql.jdbc.Driver
url: jdbc:mysql://localhost:3306/db_my_blog?useSSL=false&serverTimezone=UTC&useUnicode=true&characterEncoding=utf-8
我擦成功了,难道是数据库版本问题?然后我又在dockers里重新pull了一个MySQL镜像
然鹅并没有用,该报错还报错
然后今天我用了另外一种配置方式也就是properties
properties文件配置如下:(这里连接的是服务器的数据库5.7.3版本的)
server.port=7796
spring.datasource.username=root
spring.datasource.password=010111
spring.datasource.driver-class-name= com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://120.25.247.76:3307/db_my_blog?useSSL=false&serverTimezone=UTC&useUnicode=true&characterEncoding=utf-8
好家伙运行成功了
所以问题到底出在哪我也不知道。
反正目前的情况是yml可以连接本地数据库没毛病
properties连接服务器/本地的数据库都没问题
有没有大佬来看看这到底是怎么回事,待解决了之后再更一个贴说明一下。目前只能先用着properties来配置了