意外错误 运行 Liquibase:java.lang.RuntimeException:找不到数据库驱动程序:com.mysql.jdbc.Driver

Unexpected error running Liquibase: java.lang.RuntimeException: Cannot find database driver: com.mysql.jdbc.Driver

我在 Kubuntu 上使用 snap 包管理器安装了 Liquibase CLI。

调用以下命令会导致

liquibase calculateChecksum 2022-01-06-test.sql::1::bobby.tables --changeLogFile 2022-01-06-test.sql 
--url jdbc:mysql://localhost:3306 --username root --password password 
--driver com.mysql.jdbc.Driver --classpath ~/.m2/repository/mysql/mysql-connector-java/8.0.21/mysql-connector-java-8.0.21.jar

堆栈跟踪结果(DEBUG)

liquibase.exception.DatabaseException: liquibase.exception.DatabaseException: java.lang.RuntimeException: Cannot find database driver: com.mysql.jdbc.Driver
        at liquibase.integration.commandline.CommandLineUtils.createDatabaseObject(CommandLineUtils.java:131)
        at liquibase.integration.commandline.Main.doMigration(Main.java:1408)
        at liquibase.integration.commandline.Main.lambda$run[=14=](Main.java:361)
        at liquibase.Scope.lambda$child[=14=](Scope.java:160)
        at liquibase.Scope.child(Scope.java:169)
        at liquibase.Scope.child(Scope.java:159)
        at liquibase.Scope.child(Scope.java:138)
        at liquibase.Scope.child(Scope.java:222)
        at liquibase.Scope.child(Scope.java:226)
        at liquibase.integration.commandline.Main.run(Main.java:360)
        at liquibase.integration.commandline.Main.run(Main.java:193)
        at liquibase.Scope.child(Scope.java:169)
        at liquibase.Scope.child(Scope.java:145)
        at liquibase.integration.commandline.Main.run(Main.java:193)
        at liquibase.integration.commandline.Main.main(Main.java:156)
Caused by: liquibase.exception.DatabaseException: java.lang.RuntimeException: Cannot find database driver: com.mysql.jdbc.Driver
        at liquibase.database.DatabaseFactory.openConnection(DatabaseFactory.java:250)
        at liquibase.database.DatabaseFactory.openDatabase(DatabaseFactory.java:140)
        at liquibase.integration.commandline.CommandLineUtils.createDatabaseObject(CommandLineUtils.java:96)
        ... 14 more
Caused by: java.lang.RuntimeException: Cannot find database driver: com.mysql.jdbc.Driver
        at liquibase.database.DatabaseFactory.openConnection(DatabaseFactory.java:192)
        ... 16 more

我没有使用 liquibase.properties 文件,因为我只想计算一个 md5 哈希和,所以我可以将它与不同的 md5 值进行比较。

我需要将 mysql.jar 放在 liquibase 二进制文件所在的位置吗?

如果是这样,它作为 snap 包存放在哪里?

事实证明,Snap 不允许访问用户 $HOME 目录(madness/stupidity)中的 .m2 文件夹。

因此您需要授予 liquibase 快照访问 .m2 文件夹的权限

查看 Liquibase 可以访问的位置:

snap connections liquibase

这将打印出这个:

Interface        Plug                         Slot      Notes
home             liquibase:home               :home     -
network          liquibase:network            :network  -
personal-files   liquibase:dot-m2-repository  -         -
removable-media  liquibase:removable-media    -         -

然后运行以下命令授予 Liquibase 访问 .m2 文件夹的权限

sudo snap connect liquibase:dot-m2-repository

这应该允许 Liquibase 正常运行。

为什么这在任何地方都没有记录?我不知道...