MySQL JDBC URL 使用 2 个不同的数据库名称进行故障转移

MySQL JDBC URL Failover with 2 different database names

我想在 SpringBoot 应用程序的 application.yml 中有一个具有 2 个不同 schema/database 名称的 jdbc url。

我按照 Reference Link 进行了尝试,但不幸的是我无法正常工作。

jdbc:mysql://address=(type=master)(protocol=tcp)(host=IP1)(port=3306)(user=root)(password=root)/dbname1?failOverReadOnly=false,address=(type=master)(protocol=tcp)(host=IP2)(port=3306)(user=test)(password=test)/dbname2?failOverReadOnly=false

jdbc:mysql://address=(type=master)(protocol=tcp)(host=IP1)(port=3306)(user=root)(password=root)(dbname=dbname1)?failOverReadOnly=false,address=(type=master)(protocol=tcp)(host=IP2)(port=3306)(user=test)(password=test)(dbname=dbname2)?failOverReadOnly=false

我在启动应用程序时收到错误,因为 MySQL 连接器无法解析 url 连接字符串。

2017-02-21 11:37:40.724] log4j - 3060 ERROR [main] --- o.a.t.j.p.ConnectionPool: Unable to create initial connections of pool.
java.sql.SQLException: The connection property 'failOverReadOnly' only accepts values of the form: 'true', 'false', 'yes' or 'no'. The value 'false,address=(type=master)(protocol=tcp)(host=localhost)(port=3306)(user=test)(password=test)/dbname2?failOverReadOnly=false' is not in this set.
    at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:964)
    at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:897)
    at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:886)
    at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:860)

我想,错误消息非常简单明了- The connection property 'failOverReadOnly' only accepts values of the form: 'true', 'false', 'yes' or 'no'. The value 'false,address=(type=master)(protocol=tcp)(host=localhost)(port=3306)(user=test)(password=test)/dbname2?failOverReadOnly=false' is not in this set.

您的 属性 - failOverReadOnly 值被视为 - false,address=(type=master)(protocol=tcp)(host=localhost)(port=3306)(user=test)(password=test)/dbname2?failOverReadOnly=false 而不是简单的 false

格式 -

jdbc:mysql://address=(key1=value)[(key2=value)]...[,address=(key3=value)[(key4=value)]...]...[/[database]]»
[?propertyName1=propertyValue1[&propertyName2=propertyValue2]...]

指定先列出所有 address 然后最后列出所有属性,而您只在一个地址后写属性。

总而言之,您的网址不是文档中提到的格式。

希望对您有所帮助!!

这是一个老话题,不知道你有没有找到适合你的解决方案。

但我也遇到过类似的情况,下面的 JDBC URL 对我来说就像一个魅力

"jdbc:mysql://IP1:port1,IP2:port2/CommonDbName?failOverReadOnly=false"

在这种情况下,您的应用程序在任何时候都只会连接到 IP1 上的一个数据库,一旦该数据库关闭,它将切换到第二个数据库,即 IP2,并且由于您已标记 failOverReadOnly 为 false,第二个数据库将在两种 read/write 模式下都处于活动状态。

参考-

8.1 Configuring Server Failover