关于mysql的url参数的设置
常用的几个较为重要的参数:
参数名称 | 参数说明 | 缺省值 | 最低版本要求 |
---|---|---|---|
user | 数据库用户名(用于连接数据库) | all | |
password | 用户密码(用于连接数据库) | all | |
useSSL | 是否使用SSL连接 | true | |
characterEncoding | 编码设置characterEncoding=utf-8 | ||
serverTimezone | 时区设置,mysql6以上需要制定时区,指定UTC | ||
useUnicode | 是否使用Unicode字符集,如果characterEncoding设置为gb2312或gbk,本参数值必须设置为true | false | 1.1 |
autoReconnect | 当数据库连接异常中断时,是否自动重新连接 | false | 1.1 |
autoReconnectForPools | 是否使用针对数据库连接池的重连策略 | false | 3.1.3 |
failOverReadOnly | 自动重连成功后,连接是否设置为只读 | true | 3.0.12 |
maxReconnect | autoReconnect设置为true时,重试连接的次数 | 3 | 1.1 |
initialTimeout autoReconnect | 设置为true时,两次重连之间的时间间隔,单位:秒 | 2 | 1.1 |
connectTimeout | connectTimeout 和数据库服务器建立socket连接时的超时,单位:毫秒。 0表示永不超时,适用于JDK 1.4及更高版本 | 0 | 3.0.1 |
socketTimeout | socket操作(读写)超时,单位:毫秒 | 0 | 3.0.1 |
useServerPrepStmts | 默认使用PreparedStatement是不能执行预编译的这需要在url中给出useServerPrepStmts=true参数,(MySQL 在5.0.5以后的版本,默认是没有开启预编译功能的)。 | false | |
cachePrepStmts | 当使用不同的PreparedStatement对象来执行相同的SQL语句时,还是会出现编译两次的现象,这是因为驱动没有缓存编译后的函数key,导致二次编译。如果希望缓存编译后函数的key,那么就要设置cachePrepStmts参数为true | false | |
allowMultiQueries | 批量执行sql | false |
- 对应中文环境,通常mysql连接详细url可以设置为
url=jdbc:mysql://127.0.0.1:3306/dbname?serverTimezone=UTC&useUnicode=true&characterEncoding=utf8&characterSetResults=utf8&useSSL=false&verifyServerCertificate=false&autoReconnct=true&autoReconnectForPools=true&allowMultiQueries=true