Problems encountered when Mysql 8.0 uses Mybatis to connect to the database

Problems encountered when Mysql 8.0 uses Mybatis to connect to the database

<!-- mybatis主配置文件 -->
<configuration>
    <!-- 配置环境 -->
    <environments default="mysql">
        <!-- 配置mysql环境 -->
        <environment id="mysql">
            <!-- 配置事务类型 -->
            <transactionManager type="JDBC"></transactionManager>
            <!-- 配置数据源(连接池) -->
            <dataSource type="POOLED">
                <property name="driver" value="com.mysql.cj.jdbc.Driver"/>
                <property name="url" value="jdbc:mysql://localhost:3306/mybatis"/>
                <property name="username" value="root"/>
                <property name="password" value="root"/>
            </dataSource>
        </environment>
    </environments>
</configuration>

When I use the Mybatis configuration file of the Mysql5.X version to configure and run the program directly, the console returns me such an error:

### Error querying database.  Cause: com.mysql.cj.jdbc.exceptions.CommunicationsException: Communications link failure

The point of this error is Communications link failurethat the communication line is faulty. When this error occurs, it means that Mybatis is not connected to the database at all, so I checked the account password and tried to connect to the database using a third-party database management software. The result is normal, and the database can be connected normally, so the problem of the database itself is ruled out.

After consulting the information, I urladded it at the end of the link address useSSL=true&allowPublicKeyRetrieval=true&serverTimezone=UTC, but found that &this symbol was actually illegal. In fact, this is the case, &the symbol is not supported by the xml file, so we need to replace it &amp;.

Run it again and connect successfully.

Why can it be executed normally after adding the above code? The focus is on useSSL=truethis part.

useSSLA very important function of this parameter is to verify when connecting to Mysql. When your JDBC version is consistent with the current Mysql server you need to connect to, you don’t need to open it; and when your JDBC is inconsistent with the current Mysql server, and incompatible When this parameter is added, the database can be successfully connected.

Guess you like

Origin blog.csdn.net/Holmes_shuai/article/details/119395783