简体   繁体   English

如何确定 Maven 依赖项的 MySQL / MariaDB 版本?

[英]How to determine MySQL / MariaDB version for Maven dependency?

I'm writing a simple Maven project to access my MySQL / MariaDB database.我正在编写一个简单的 Maven 项目来访问我的 MySQL/MariaDB 数据库。 In order to download the proper database connector jar file, I'm adding the following Maven dependency:为了下载正确的数据库连接器 jar 文件,我添加了以下 Maven 依赖项:

<dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
    <version>5.1.47</version>
    <scope>provided</scope>
</dependency>

I've got that 5.1.47 version above "by chance", based on a previous project which already worked on my computer.基于之前已经在我的计算机上运行的项目,我“偶然”获得了5.1.47版本。

However, when I type mysql -V (capital V) on terminal to check my MySQL/MariaDB version, I get the following:但是,当我在终端上输入mysql -V (大写 V)来检查我的 MySQL/MariaDB 版本时,我得到以下信息:

>mysql -V
mysql  Ver 15.1 Distrib 10.4.6-MariaDB, for Win64 (AMD64), source revision b8e655ce029a1f182602c9f12c3cc5931226eec2

But that 10.4.6 version doesn't mean much to me in terms of determining a proper version to set up my Maven dependency.但是在确定合适的版本来设置我的 Maven 依赖项方面, 10.4.6版本对我来说意义不大。

What's the procedure to determine MySQL version for Maven dependency based on my local MySQL / MariaDB server?根据我的本地 MySQL/MariaDB 服务器确定 Maven 依赖项的 MySQL 版本的程序是什么?

What's the procedure to determine MySQL version for Maven dependency based on my local MySQL / MariaDB server?根据我的本地 MySQL/MariaDB 服务器确定 Maven 依赖项的 MySQL 版本的程序是什么?

You cannot .They are different things.你不能。它们是不同的东西。 The one specified in is the client version while one the you checked with mysql -V is the server version. 指定的一个是客户端版本,而您使用mysql -V检查的一个是服务器版本。 The JDBC client version does not needed to be exactly matched with the server version. JDBC 客户端版本不需要与服务器版本完全匹配。

Instead , the thing that you need to ensure is that the server support client version that you used.相反,您需要确保服务器支持您使用的客户端版本。

From the official docs , 5.1.47 should support MySQL 5.61, 5.71, 8.0.从官方文档来看,5.1.47 应该支持 MySQL 5.61、5.71、8.0。 And from the compatibility matrix of MariaDB , MariaDB 10.4.6 is comparability with MySQL 5.6 and 5.7 , which mean the JDBC client version that you are using should have no problem.并且从 MariaDB 的兼容性矩阵来看,MariaDB 10.4.6 与 MySQL 5.6 和 5.7 具有可比性,这意味着您使用的 JDBC 客户端版本应该没有问题。 But if it is possible , you can update the client to version 8.0 series , which is the official recommendation and should also work with your existing MariaDB version.但如果可能,您可以将客户端更新到 8.0 系列版本,这是官方推荐的,也应该与您现有的 MariaDB 版本一起使用。

You can get the newest version from mvnrepository .您可以从mvnrepository获取最新版本。

If you have the older version of mysql, you need to make sure, that the versions of the connector, jdbc, database etc. match.如果你有旧版本的 mysql,你需要确保连接器、jdbc、数据库等的版本匹配。 You can find this information in the mysql developer guide .您可以在 mysql 开发人员指南中找到此信息。 The same thing applies to the compatibility for mariadb - check out the documentation .同样的事情适用于 mariadb 的兼容性 - 查看文档

Note, that the driver version depends not only on the version of the database, but also on java version.请注意,驱动程序版本不仅取决于数据库的版本,还取决于 java 版本。

I've checked MySQL connector MVN Repository for latest version, then I decided to replace 5.1.47 to 8.0.18 .我已经检查了MySQL connector MVN Repository的最新版本,然后我决定将5.1.47替换为8.0.18

However, after replacing to 8.xx, I got "hibernate.dialect not set" error.但是,替换到 8.xx 后,出现“hibernate.dialect not set”错误。 So I added MySQL8Dialect to persistence.xml, then got "time zone value 'unknown' is unrecognized" error.所以我将MySQL8Dialect添加到persistence.xml,然后得到“时区值‘未知’无法识别”错误。 So I added &amp;serverTimezone=UTC to my connection parameters, than it worked.所以我将&amp;serverTimezone=UTC到我的连接参数中,然后它就起作用了。

Here is the updated persistence.xml after using 8.xx:这是使用8.xx后更新的persistence.xml:

<?xml version="1.0" encoding="UTF-8"?>
<persistence xmlns="http://xmlns.jcp.org/xml/ns/persistence"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence
    http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd"
    version="2.1">

    <persistence-unit name="myunit" transaction-type="RESOURCE_LOCAL">
        <properties>
            <property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver" />
            <property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost/databasename?useSSL=false&amp;serverTimezone=UTC" />
            <property name="javax.persistence.jdbc.user" value="user" />
            <property name="javax.persistence.jdbc.password" value="password" />

            <property name="hibernate.hbm2ddl.auto" value="update" />

            <property name="hibernate.dialect" value="org.hibernate.dialect.MySQL8Dialect" />
        </properties>
    </persistence-unit>
</persistence>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM