0


mysql jdbc连接字符串详解以及属性配置

前言

mysql jdbc 这个虽然使用比较多,但是属性过多,这里做个属性配置收藏。

格式

一般通用的格式为:

protocol//[hosts][/database][?properties]

分别为 协议 连接地址 数据库名 属性配置

常用协议如下

  • jdbc:mysql: is for ordinary and basic JDBC failover connections.
  • jdbc:mysql:loadbalance: is for load-balancing JDBC connections. See Section 9.3, “Configuring Load Balancing with Connector/J” for details.
  • jdbc:mysql:replication: is for JDBC replication connections. See Section 9.4, “Configuring Source/Replica Replication with Connector/J” for details.
  • mysqlx: is for X DevAPI connections.
  • jdbc:mysql+srv: is for ordinary and basic failover JDBC connections that make use of DNS SRV records.
  • jdbc:mysql+srv:loadbalance: is for load-balancing JDBC connections that make use of DNS SRV records.
  • jdbc:mysql+srv:replication: is for replication JDBC connections that make use of DNS SRV records.
  • mysqlx+srv: is for X DevAPI connections that make use of DNS SRV records.

连接地址

肯定有人好奇 不就是个连接地址么,这有什么可说的。

官方文档如下
根据情况,主机部分可能仅由主机名组成,也可以是包含各种元素(如多个主机名、端口号、主机特定属性和用户凭据)的复杂结构。

  • Single host:Single-host connections without adding host-specific properties:The hosts part is written in the format of host:port. This is an example of a simple single-host connection URL:jdbc:mysql://host1:33060/sakilahost can be an IPv4 or an IPv6 host name string, and in the latter case it must be put inside square brackets, for example “[1000:2000::abcd].” When host is not specified, the default value of localhost is used.> host 可以是 IPv4 或 IPv6 主机名字符串,在后一种情况下,它必须放在方括号内,例如“[1000:2000::abcd]”。当未指定 host 时,将使用默认值 localhost。port is a standard port number, i.e., an integer between 1 and 65535. The default port number for an ordinary MySQL connection is 3306, and it is 33060 for a connection using the X Protocol. If port is not specified, the corresponding default is used.单主机连接添加主机特定属性:In this case, the host is defined as a succession of key=value pairs. Keys are used to identify the host, the port, as well as any host-specific properties. There are two alternate formats for specifying keys:> 在这种情况下,主机被定义为一系列键=值对。键用于标识主机、端口以及任何特定于主机的属性。有两种替代格式可用于指定键:The “address-equals” form:address=(host=host_or_ip)(port=port)(key1=value1)(key2=value2)…(keyN=valueN) Here is a sample URL using the“address-equals” form :jdbc:mysql://address=(host=myhost)(port=1111)(key1=value1)/dbThe “key-value” form:(host=host,port=port,key1=value1,key2=value2,…,keyN=valueN) Here is a sample URL using the “key-value” form :jdbc:mysql://address=(host=myhost)(port=1111)(key1=value1)/dbThe host and the port are identified by the keys host and port. The descriptions of the format and default values of host and port in Single host without host-specific properties above also apply here.Other keys that can be added include user, password, protocol, and so on. They override the global values set in the properties part of the URL. Limit the overrides to user, password, network timeouts, and statement and metadata cache sizes; the effects of other per-host overrides are not defined.Different protocols may require different keys. For example, the mysqlx: scheme uses two special keys, address and priority. address is a host:port pair and priority an integer. For example:> 不同的协议可能需要不同的密钥。例如,mysqlx: 方案使用两个特殊密钥,地址和优先级。地址是主机:端口对,优先级是整数。例如:mysqlx://(address=host:1111,priority=1,key1=value1)/dbkey is case-sensitive. Two keys differing in case only are considered conflicting, and there are no guarantees on which one will be used.
  • Multiple hostsThere are two formats for specifying multiple hosts:List hosts in a comma-separated list:host1,host2,…,hostN Each host can be specified in any of the three ways described in Single host above. Here are some examples:jdbc:mysql://myhost1:1111,myhost2:2222/dbjdbc:mysql://address=(host=myhost1)(port=1111)(key1=value1),address=(host=myhost2)(port=2222)(key2=value2)/dbjdbc:mysql://(host=myhost1,port=1111,key1=value1),(host=myhost2,port=2222,key2=value2)/dbjdbc:mysql://myhost1:1111,(host=myhost2,port=2222,key2=value2)/dbmysqlx://(address=host1:1111,priority=1,key1=value1),(address=host2:2222,priority=2,key2=value2)/dbList hosts in a comma-separated list, and then encloses the list by square brackets:[host1,host2,...,hostN]This is called the host sublist form, which allows sharing of the user credentials by all hosts in the list as if they are a single host. Each host in the list can be specified in any of the three ways described in Single host above. Here are some examples:jdbc:mysql://sandy:secret@[myhost1:1111,myhost2:2222]/dbjdbc:mysql://sandy:secret@[address=(host=myhost1)(port=1111)(key1=value1),address=(host=myhost2)(port=2222)(key2=value2)]/dbjdbc:mysql://sandy:secret@[myhost1:1111,address=(host=myhost2)(port=2222)(key2=value2)]/dbWhile it is not possible to write host sublists recursively, a host list may contain host sublists as its member hosts.User credentials 用户凭证User credentials can be set outside of the connection URL—for example, as arguments when getting a connection from the java.sql.DriverManager (see Section 6.3, “Configuration Properties” for details). When set with the connection URL, there are several ways to specify them:> 用户凭据可以在连接 URL 之外设置 - 例如,从 java.sql.DriverManager 获取连接时作为参数。当使用连接 URL 设置时,有几种方法可以指定它们:Prefix the a single host, a host sublist (see Multiple hosts), or any host in a list of hosts with the user credentials with an @:user:password@host_or_host_sublistFor example:mysqlx://sandy:secret@[(address=host1:1111,priority=1,key1=value1),(address=host2:2222,priority=2,key2=value2))]/dbUse the keys user and password to specify credentials for each host:(user=sandy)(password=mypass) For example:jdbc:mysql://[(host=myhost1,port=1111,user=sandy,password=secret),(host=myhost2,port=2222,user=finn,password=secret)]/dbjdbc:mysql://address=(host=myhost1)(port=1111)(user=sandy)(password=secret),address=(host=myhost2)(port=2222)(user=finn)(password=secret)/dbIn both forms, when multiple user credentials are specified, the one to the left takes precedence—that is, going from left to right in the connection string, the first one found that is applicable to a host is the one that is used.> 在这两种形式中,当指定多个用户凭据时,左侧的凭据优先 - 即,在连接字符串中从左到右,找到的第一个适用于主机的凭据就是使用的凭据。Inside a host sublist, no host can have user credentials in the @ format, but individual host can have user credentials specified in the key format.

在主机子列表中,没有主机可以具有 @ 格式的用户凭据,但单个主机可以具有以密钥格式指定的用户凭据。

数据库名

这个没啥好说的,就是要连接哪个数据库

属性配置

这有点多 根据不同种类分为以下表格分别描述了属性名称 以及默认值 以及属性出现的版本
常用的有 连接属性配置 会话属性 网络属性 高可用性和集群属性

示例的连接配置 指定了账号密码 连接超时 等配置

jdbc:mysql://hostname:port/databaseName?user=username&password=password&connectTimeout=30000&socketTimeout=30000&autoReconnect=true&maxReconnects=3&retriesAllDown=120&queryTimeoutKillsConnection=true&characterEncoding=UTF-8&connectionCollation=utf8_general_ci&useUnicode=true&characterSetResults=UTF-8&serverTimezone=Asia/Shanghai
连接属性配置

NameDefault ValueSince Versionuser-all versionspassword-all versionspassword1-8.0.28password2-8.0.28password3-8.0.28authenticationPlugins-5.1.19disabledAuthenticationPlugins-5.1.19defaultAuthenticationPluginmysql_native_password5.1.19ldapServerHostname-8.0.23ociConfigFile-8.0.27ociConfigProfileDEFAULT8.0.33

会话属性

NameDefault ValueSince VersionsessionVariables-3.1.8characterEncoding-1.1gcharacterSetResults-3.0.13connectionCollation-3.0.13customCharsetMapping-8.0.26trackSessionStatefalse8.0.26

网络属性

NameDefault ValueSince VersionsocksProxyHost-5.1.34socksProxyPort10805.1.34socketFactorycom.mysql.cj.protocol.StandardSocketFactory3.0.3connectTimeout03.0.1socketTimeout03.0.1dnsSrvfalse8.0.19localSocketAddress-5.0.5maxAllowedPacket655355.1.8socksProxyRemoteDnsfalse8.0.29tcpKeepAlivetrue5.0.7tcpNoDelaytrue5.0.7tcpRcvBuf05.0.7tcpSndBuf05.0.7tcpTrafficClass05.0.7useCompressionfalse3.0.17

安全属性

NameDefault ValueSince Versionparanoidfalse3.0.1serverRSAPublicKeyFile-5.1.31allowPublicKeyRetrievalfalse5.1.31sslModePREFERRED8.0.13trustCertificateKeyStoreUrl-5.1.0trustCertificateKeyStoreTypeJKS5.1.0trustCertificateKeyStorePassword-5.1.0fallbackToSystemTrustStoretrue8.0.22clientCertificateKeyStoreUrl-5.1.0clientCertificateKeyStoreTypeJKS5.1.0clientCertificateKeyStorePassword-5.1.0fallbackToSystemKeyStoretrue8.0.22tlsCiphersuites-5.1.35tlsVersions-8.0.8fipsCompliantJssefalse8.1.0KeyManagerFactoryProvider-8.1.0trustManagerFactoryProvider-8.1.0keyStoreProvider-8.1.0sslContextProvider-8.1.0allowLoadLocalInfilefalse3.0.3allowLoadLocalInfileInPath-8.0.22allowMultiQueriesfalse3.1.1allowUrlInLocalInfilefalse3.1.4requireSSLfalse3.1.0useSSLtrue3.0.2

语句属性

NameDefault ValueSince VersioncacheDefaultTimeZonetrue8.0.20continueBatchOnErrortrue3.0.3………

预准备语句属性

NameDefault ValueSince VersionallowNanAndInffalse3.1.5autoClosePStmtStreamsfalse3.1.12compensateOnDuplicateKeyUpdateCountsfalse5.1.7emulateUnsupportedPstmtstrue3.1.7generateSimpleParameterMetadatafalse5.0.5processEscapeCodesForPrepStmtstrue3.1.12useServerPrepStmtsfalse3.1.0useStreamLengthsInPrepStmtstrue3.0.2

结果集属性

NameDefault ValueSince VersionclobberStreamingResultsfalse3.0.9emptyStringsConvertToZerotrue3.1.8holdResultsOpenOverStatementClosefalse3.1.7jdbcCompliantTruncationtrue3.1.2netTimeoutForStreamingResults6005.1.0padCharsWithSpacefalse5.0.6populateInsertRowWithDefaultValuesfalse5.0.5scrollTolerantForwardOnlyfalse8.0.24strictUpdatestrue3.0.4tinyInt1isBittrue3.0.16transformedBitIsBooleanfalse3.1.9

元数据属性

NameDefault ValueSince VersiongetProceduresReturnsFunctionstrue5.1.26noAccessToProcedureBodiesfalse5.0.3getProceduresReturnsFunctionstrue5.1.26noAccessToProcedureBodiesfalse5.0.3nullDatabaseMeansCurrentfalse3.1.8useHostsInPrivilegestrue3.0.2useInformationSchemafalse5.0.0

BLOB/CLOB处理属性

NameDefault ValueSince VersionblobSendChunkSize10485763.1.9blobsAreStringsfalse5.0.8clobCharacterEncoding-5.0.0emulateLocatorsfalse3.1.0functionsNeverReturnBlobsfalse5.0.8locatorFetchBufferSize10485763.2.1

日期时间类型处理属性

NameDefault ValueSince VersionconnectionTimeZone-3.0.2forceConnectionTimeZoneToSessionfalse8.0.23preserveInstantstrue8.0.23sendFractionalSecondstrue5.1.37sendFractionalSecondsForTimetrue8.0.23treatMysqlDatetimeAsTimestampfalse8.2.0treatUtilDateAsTimestamptrue5.0.5yearIsDateTypetrue3.1.9zeroDateTimeBehaviorEXCEPTION3.1.4

高可用性和集群属性

NameDefault ValueSince VersionautoReconnectfalse1.1autoReconnectForPoolsfalse3.1.3failOverReadOnlytrue3.0.12maxReconnects31.1reconnectAtTxEndfalse3.0.10retriesAllDown1205.1.6initialTimeout21.1queriesBeforeRetrySource503.0.2secondsBeforeRetrySource303.0.2allowReplicaDownConnectionsfalse6.0.2allowSourceDownConnectionsfalse5.1.27ha.enableJMXfalse5.1.27loadBalanceHostRemovalGracePeriod150006.0.3readFromSourceWhenNoReplicasfalse6.0.2selfDestructOnPingMaxOperations05.1.6selfDestructOnPingSecondsLifetime05.1.6ha.loadBalanceStrategyrandom5.0.6

性能扩展属性

NameDefault ValueSince VersioncallableStmtCacheSize1003.1.2metadataCacheSize503.1.1alwaysSendSetIsolationtrue3.1.7maintainTimeStatstrue3.1.9useCursorFetchfalse5.0.0cacheCallableStmtsfalse3.1.2cachePrepStmtsfalse3.0.10cacheResultSetMetadatafalse3.1.1cacheServerConfigurationfalse3.1.5defaultFetchSize03.1.9dontCheckOnDuplicateKeyUpdateInSQLfalse5.1.32elideSetAutoCommitsfalse3.1.3enableEscapeProcessingtrue

调试/分析属性

NameDefault ValueSince Versionloggercom.mysql.cj.log.StandardLogger3.1.1profilerEventHandlercom.mysql.cj.log.LoggingProfilerEventHandler5.1.6………

异常/警告属性

NameDefault ValueSince VersiondumpQueriesOnExceptionfalse3.1.3exceptionInterceptors-5.1.8………

与其他产品集成调整属性

NameDefault ValueSince VersionoverrideSupportsIntegrityEnhancementFacilityfalse3.1.12ultraDevHackfalse2.0.3

JDBC合规性属性

NameDefault ValueSince VersionuseColumnNamesInFindColumnfalse5.1.7pedanticfalse3.0.0useOldAliasMetadataBehaviorfalse5.0.4

X协议和X DevAPI属性

NameDefault ValueSince Versionxdevapi.authPLAIN8.0.8xdevapi.compressionPREFERRED8.0.20xdevapi.compression-algorithmszstd_stream,lz4_message,deflate_stream8.0.22xdevapi.compression-extensions-8.0.22xdevapi.connect-timeout100008.0.13xdevapi.connection-attributes-8.0.16xdevapi.dns-srvfalse8.0.19xdevapi.fallback-to-system-keystoretrue8.0.22xdevapi.fallback-to-system-truststoretrue8.0.22xdevapi.ssl-keystore-8.0.22xdevapi.ssl-keystore-password-8.0.22xdevapi.ssl-keystore-typeJKS8.0.22xdevapi.ssl-modeREQUIRED8.0.7xdevapi.ssl-truststore-6.0.6xdevapi.ssl-truststore-password-6.0.6xdevapi.ssl-truststore-typeJKS6.0.6xdevapi.tls-ciphersuites-8.0.19xdevapi.tls-versions-8.0.19

参考文档:
https://dev.mysql.com/doc/connector-j/en/connector-j-reference-jdbc-url-format.html
https://dev.mysql.com/doc/connector-j/en/connector-j-reference-configuration-properties.html


本文转载自: https://blog.csdn.net/a15835774652/article/details/139773416
版权归原作者 寂夜了无痕 所有, 如有侵权,请联系我们删除。

“mysql jdbc连接字符串详解以及属性配置”的评论:

还没有评论