美文网首页
springboot application.yml文件中配置使

springboot application.yml文件中配置使

作者: 愤怒的阿昆达 | 来源:发表于2021-11-26 09:22 被阅读0次

    一、使用Maven引入mysql的jdbc驱动依赖:

    在pom.xml中添加如下:

    <dependency>
                <groupId>mysql</groupId>
                <artifactId>mysql-connector-java</artifactId>
                <version>8.0.27</version>
            </dependency>
            <dependency>
    

    注意:其中<version>8.0.27</version>需要和你连的mysql版本一致!
    否则报错:Could not create connection to database server

    2021-11-26 08:46:47.198 ERROR 31972 --- [eate-1776794006] com.alibaba.druid.pool.DruidDataSource   : create connection SQLException, url: jdbc:mysql://192.168.18.124:3306/mysql?useUnicode=true&characterEncoding=utf8&autoReconnect=true&failOverReadOnly=false&rewriteBatchedStatements=TRUE, errorCode 0, state 08001
    
    com.mysql.jdbc.exceptions.jdbc4.MySQLNonTransientConnectionException: Could not create connection to database server. Attempted reconnect 3 times. Giving up.
        at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) ~[na:1.8.0_181]
        at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62) ~[na:1.8.0_181]
        at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45) ~[na:1.8.0_181]
        at java.lang.reflect.Constructor.newInstance(Constructor.java:423) ~[na:1.8.0_181]
    

    如何查询 mysql 版本?
    最简单的就是执行sql语句:select version() from dual;

    image.png

    二、yml中配置 mysql jdbc driver:

    先看我的配置:

    server:
      host: 192.168.xx.xxx
    spring:
      # 数据源配置
      datasource:
        url: jdbc:mysql://${server.host}:3306/mysql?useUnicode=true&characterEncoding=utf8&autoReconnect=true&failOverReadOnly=false&rewriteBatchedStatements=TRUE
        driver-class-name: com.mysql.cj.jdbc.Driver
        username: root
        password: '123456'
        type: com.alibaba.druid.pool.DruidDataSource
        #   数据源其他配置
        initialSize: 5
        minIdle: 5
        maxActive: 20
        maxWait: 60000
        timeBetweenEvictionRunsMillis: 60000
        minEvictableIdleTimeMillis: 300000
        validationQuery: SELECT 1
        testWhileIdle: true
        testOnBorrow: false
        testOnReturn: false
        poolPreparedStatements: true
        #   配置监控统计拦截的filters,去掉后监控界面sql无法统计,'wall'用于防火墙
        filters: stat,wall,logback
        maxPoolPreparedStatementPerConnectionSize: 20
        useGlobalDataSourceStat: true
        connectionProperties: druid.stat.mergeSql=true;druid.stat.slowSqlMillis=500
    

    其中,jdbc:mysql://${server.host}:3306/mysql?useUnicode=true&characterEncoding=utf8&autoReconnect=true&failOverReadOnly=false&rewriteBatchedStatements=TRUE
    解释:
    mysql:你要连接的数据库名称
    autoReconnect:当数据库连接异常中断时,是否自动重新连接?
    failOverReadOnly: 自动重连成功后,连接是否设置为只读?
    rewriteBatchedStatements:true:开启jdbc驱动支持批处理,默认为false
    其他的:
    password: '123456'密码值加引号,不要写成 password: 123456

    参考文献:

    查看mysql的版本号 - _再见理想 - 博客园 (cnblogs.com)
    (9条消息) [已解决] Could not create connection to database server._小王的学习blog-CSDN博客
    (9条消息) Mysql JDBC Driver参数配置_w345731923的专栏-CSDN博客
    mysql配置jdbc - 苗士军 - 博客园 (cnblogs.com)

    相关文章

      网友评论

          本文标题:springboot application.yml文件中配置使

          本文链接:https://www.haomeiwen.com/subject/eqytxrtx.html