Springboot中mysql配置参数大全

78 阅读2分钟

Springboot中配置mysql数据库,使用Druid连接池

spring:
    datasource:
        url: jdbc:mysql://localhost:3306/test?serverTimezone=Asia/Shanghai&zeroDateTimeBehavior=convertToNull&autoReconnect=true&useSSL=false&failOverReadOnly=false
        # 用户
        username: test
 
        # 密码
        password: 123456
 
        # 根据mysql版本选其一:mysql 8 
        driver-class-name: com.mysql.cj.jdbc.Driver
        #mysql 5
        driver-class-name: com.mysql.jdbc.Driver
 
        # 连接池类型druid
        type: com.alibaba.druid.pool.DruidDataSource
 
        #druid 数据源配置
        # 初始化大小
        initialSize: 5
 
        # 最小连接数
        minIdle: 5
 
        # 最大连接数
        maxActive: 20
 
        # 获取连接等待的超时时间,单位是毫秒
        maxWait: 60000
 
        # 配置一个连接在池中最小生存的时间,单位是毫秒
        minEvictableIdleTimeMillis: 300000
 
        # 指定空闲连接回收器运行的时间间隔,单位为毫秒。
        # 若设置为非负值,则回收器在指定的时间间隔运行并关闭空闲连接。若设置为负数,则回收器停用。
        timeBetweenEvictionRunsMillis: 60000
        
        # 检测数据库连接是否有效的sql语句,这有助于保持连接的活跃状态,同时也可以用于数据库健康检查,如果validationQuery为null,testOnBorrow、testOnReturn、testWhileIdle都不会起作用
        validationQuery: SELECT 1
        # 单位:秒,检测连接是否有效的超时时间,默认:-1
        validation-query-timeout: 5
        # 建议配置为true,不影响性能,并且保证安全性。申请连接的时候检测,如果空闲时间大于timeBetweenEvictionRunsMillis,执行validationQuery检测连接是否有效。默认:true
        testWhileIdle: true
        # 设置从连接池获取连接时是否检查连接有效性,true检查,false不检查
        testOnBorrow: false
        # 设置从连接池归还连接时是否检查连接有效性,true检查,false不检查
        testOnReturn: false
 
        # 可以支持PSCache(提升写入、查询效率)
        poolPreparedStatements: true
        # 打开PSCache,并且指定每个连接上PSCache的大小
        maxPoolPreparedStatementPerConnectionSize: 20
 
        # 配置监控统计拦截的filters,去掉后监控界面sql无法统计,'wall'用于防火墙
        filters: stat, wall
 
        # 通过connectProperties属性来打开mergeSql功能;慢SQL记录
        connectionProperties: druid.stat.mergeSql=true;druid.stat.slowSqlMillis=500
 
        # 合并多个DruidDataSource的监控数据
        useGlobalDataSourceStat: true
 
        #监控
        stat-view-servlet:
            enabled: true
            loginUsername: test
            loginPassword: 123456
            web-stat-filter:
                  enabled: true