Liquibase MySQL JDBC 使用 liquibase.properties 的驱动程序连接错误

Liquibase MySQL JDBC Driver connectivity error using liquibase.properties

我目前正在评估 Liquibase 作为数据库版本控制解决方案。我正在 运行 宁 Ubuntu 16.04 LTS java 版本“1.8.0_181”。我已经安装了 Liquibase 3.6.2,我设置了两个 MySQL 5.7 docker containers on ports 4408 and 4409 for testing, and I have downloaded MySQL Connector/J 8.0,推荐用于 MySQL Server 5.7。我根据 liquibase 自述文件将 mysql-connector-java-8.0.12.jar 放在 /usr/local/liquibase/lib/ 中:

The "lib" directory is automatically scanned by the liquibase scripts and all .jar files are added to the classpath. If you have JDBC drivers or Liquibase extensions that you want to be automatically included, add them to this directory.

在我的项目测试目录中,我有以下 liquibase.properties 文件:

classpath=/usr/local/liquibase/lib/mysql-connector-java-8.0.12.jar
driver=com.mysql.cj.jdbc.Driver
changeLogFile=db.changelog-1.0.xml
url="jdbc:mysql://localhost:4409/myDatabase"
username=myUser
password=myPassword

我想在端口 4409 上从我的开发数据库的当前状态生成更新日志,然后(在集成例程和触发器之后)运行 在端口 4408 上的空数据库上生成更新日志,然后进行比较查看两者是否相同,然后从那里与生产数据库执行差异以测试更改的集成程度。所以我的第一步是 运行 从与我的 liquibase.porperties 文件相同的目录中执行以下命令:

liquibase generateChangeLog

结果是下面的输出:

Starting Liquibase at Thu, 23 Aug 2018 07:37:21 PDT (version 3.6.2 built at 2018-07-03 11:28:09)
Unexpected error running Liquibase: liquibase.exception.DatabaseException: Connection could not be created to "jdbc:mysql://localhost:4409/myDatabase" with driver com.mysql.cj.jdbc.Driver.  Possibly the wrong driver for the given database URL
liquibase.exception.DatabaseException: liquibase.exception.DatabaseException: liquibase.exception.DatabaseException: Connection could not be created to "jdbc:mysql://localhost:4409/myDatabase" with driver com.mysql.cj.jdbc.Driver.  Possibly the wrong driver for the given database URL
    at liquibase.integration.commandline.CommandLineUtils.createDatabaseObject(CommandLineUtils.java:132)
    at liquibase.integration.commandline.Main.doMigration(Main.java:953)
    at liquibase.integration.commandline.Main.run(Main.java:191)
    at liquibase.integration.commandline.Main.main(Main.java:129)
Caused by: liquibase.exception.DatabaseException: liquibase.exception.DatabaseException: Connection could not be created to "jdbc:mysql://localhost:4409/myDatabase" with driver com.mysql.cj.jdbc.Driver.  Possibly the wrong driver for the given database URL
    at liquibase.database.DatabaseFactory.openConnection(DatabaseFactory.java:254)
    at liquibase.database.DatabaseFactory.openDatabase(DatabaseFactory.java:149)
    at liquibase.integration.commandline.CommandLineUtils.createDatabaseObject(CommandLineUtils.java:97)
... 3 common frames omitted
Caused by: liquibase.exception.DatabaseException: Connection could not be created to "jdbc:mysql://localhost:4409/myDatabase" with driver com.mysql.cj.jdbc.Driver.  Possibly the wrong driver for the given database URL
    at liquibase.database.DatabaseFactory.openConnection(DatabaseFactory.java:249)
    ... 5 common frames omitted

注意:由于我已将连接器文件包含在 liquibase lib 目录中,根据上面自述文件的引述,我不必指定类路径。我已经尝试了两种方法并收到了相同的错误。此外,我尝试使用 mysql-connector-java-5.1.32.jar,这是我的 maven 本地存储库中的内容,并将 liquibase 属性文件中的驱动程序调整为 driver=com.mysql.jdbc.Driver,但仍然出现相同的错误。

现在,这是令人困惑的地方:如果我执行以下命令而不是使用 liquibase 属性文件:

liquibase \
--classpath=/usr/local/liquibase/lib/mysql-connector-java-8.0.12.jar \
--driver=com.mysql.cj.jdbc.Driver \
--changeLogFile=db.changelog-1.0.xml \
--url="jdbc:mysql://localhost:4409/myDatabase" \
--username=myUser \
--password=myPassword \
generateChangeLog

它成功生成了具有以下输出的更改日志文件:

Starting Liquibase at Thu, 23 Aug 2018 09:54:41 PDT (version 3.6.2 built at 2018-07-03 11:28:09)
Thu Aug 23 09:54:43 PDT 2018 WARN: Establishing SSL connection without 
server's identity verification is not recommended. According to MySQL 
5.5.45+, 5.6.26+ and 5.7.6+ requirements SSL connection must be 
established by default if explicit option isn't set. For compliance 
with existing applications not using SSL the verifyServerCertificate 
property is set to 'false'. You need either to explicitly disable SSL 
by setting useSSL=false, or set useSSL=true and provide truststore for 
server certificate verification.
Liquibase command 'generateChangeLog' was executed successfully.

现在,我还有其他问题想解决,比如为什么生成的变更日志包含备注,但是当将变更日志应用于空数据库时,每个 table 的第一列上的备注不是包括或者当我在两者之间执行 diffChangeLog 并尝试使用我得到的结果更改日志更新数据库时,错误是更改验证失败:mysql 不支持 setColumnRemarks,或者 diff 生成的视图有replaceIfExists 已启用,但更新会引发 table 已存在的错误。无论如何,在我 post 为这些问题创建一个单独的线程之前,我想为什么不从我无法解决的第一个问题开始工作,看看在这个过程中的某个地方是否可能导致其他问题。

所以解决方案真的很简单,真不敢相信我没有看到。在 liquibase.properties 文件中,url 不应有引号。将文件编辑为:

classpath=/usr/local/liquibase/lib/mysql-connector-java-8.0.12.jar
driver=com.mysql.cj.jdbc.Driver
changeLogFile=db.changelog-1.0.xml
url=jdbc:mysql://localhost:4409/myDatabase
username=myUser
password=myPassword

成功生成变更日志文件。它必须是参数在属性文件中与命令行中解析的方式。