weixj_new 2023-11-25 19:57 采纳率: 0%
浏览 7

springboot 配置多源数据库

springboot 配置多源数据库问题。
测试了一下springboot配置多源数据库,mysql和postgresql,yml配置如下:

spring:
  datasource:
    dynamic:
      primary: mysql
      strict: false
      datasource:
        mysql:
          driver-class-name: com.mysql.cj.jdbc.Driver
          username: root
          password: root
          url: jdbc:mysql://*.*.*.*:3306/student?allowPublicKeyRetrieval=true&useSSL=false&useUnicode=true&characterEncoding=utf-8&serverTimezone=GMT
       
        postgresql:
          url: jdbc:postgresql://localhost:5432/postgres 
          username: postgres
          password: weixj5217
          driverClassName: org.postgresql.Driver

运行程序,出现如下错误:

……
9:30:59:048] [INFO] - org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.prepareWebApplicationContext(ServletWebServerApplicationContext.java:292) - Root WebApplicationContext: initialization completed in 1080 ms
[19:30:59:138] [INFO] - com.zaxxer.hikari.HikariDataSource.(HikariDataSource.java:80) - mysql - Starting...
[19:31:21:237] [ERROR] - com.zaxxer.hikari.pool.HikariPool.throwPoolInitializationException(HikariPool.java:594) - mysql - Exception during pool initialization.
com.mysql.cj.jdbc.exceptions.CommunicationsException: Communications link failure

The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received any packets from the server.
at com.mysql.cj.jdbc.exceptions.SQLError.createCommunicationsException(SQLError.java:174) ~[mysql-connector-java-8.0.29.jar:8.0.29]
at com.mysql.cj.jdbc.exceptions.SQLExceptionsMapping.translateException(SQLExceptionsMapping.java:64) ~[mysql-connector-java-8.0.29.jar:8.0.29]
at com.mysql.cj.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:828) ~[mysql-connector-java-8.0.29.jar:8.0.29]
at com.mysql.cj.jdbc.ConnectionImpl.(ConnectionImpl.java:448) ~[mysql-connector-java-8.0.29.jar:8.0.29]
……

从错误中看,好像mysql启动错误。

  • 写回答

3条回答 默认 最新

  • weixj_new 2023-11-25 20:03
    关注

    说明一下:
    单独连接mysql数据库(不是多源数据库),程序正常,不会出现上面的错误。

    spring:
      datasource:
        driver-class-name: com.mysql.cj.jdbc.Driver
        username: root
        password: root
        url: jdbc:mysql://****:3306/studentl?allowPublicKeyRetrieval=true&useSSL=false&useUnicode=true&characterEncoding=utf-8&serverTimezone=GMT
          default-read-only: false
    
    评论 编辑记录

报告相同问题?

问题事件

  • 修改了问题 11月25日
  • 创建了问题 11月25日