pringmvc项目mysql版本从5.7切换8.0版本

**

1. 替换jar包:

mysql-connector-java-5.0.5.jar替换成了mysql-connector-java-8.0.11.jar
下载mysql-connector-java-8.0.11.jar地址

2. 修改驱动配置jdbc.properies文件

将com.mysql.jdbc.Driver 修改为 com.mysql.cj.jdbc.Driver

3. 修改连接地址:

jdbc.url=jdbc:mysql://192.168.1.245:3306/procuratorate?useUnicode=true&characterEncoding=UTF-8&useSSL=false&serverTimezone=UTC&allowPublicKeyRetrieval=true&useOldAliasMetadataBehavior=true**

①添加userSSL=false
报错:(报这个警告的原因主要是JDBC的版本与MySQL的版本不兼容,而MySQL在高版本需要指明是否进行SSL连接)

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.

翻译:不建议在没有服务器身份验证的情况下建立SSL连接。根据MySQL5.5.45+、5.6.26+和5.7.6+的要求,如果没有设置显式选项,默认必须建立SSL连接。为了符合不使用SSL的现有应用程序,verifyServerCertificate属性被设置为’false’。您需要通过设置useSSL=false显式禁用SSL,或者设置useSSL=true并为服务器证书验证提供信任存储区。

②添加serverTimezone=UTC, 同时修改了 mysql> set global time_zone=‘+8:00’; mysql> flush privileges;
报错:

java.sql.SQLException: The server time zone value ‘Öйú±ê׼ʱ¼ä’ is unrecognized or represents more than one time zone. You must configure either the server or JDBC driver (via the serverTimezone configuration property) to use a more specifc time zone value if you want to utilize time zone support.

翻译:服务器时区值 ‘�й���׼ʱ��’ 无法识别或表示多个时区。如果您想利用时区支持,您必须配置服务器或 JDBC 驱动程序(通过 ‘serverTimezone’ 配置属性)来使用更具体的时区值。
此问题解决方案:

  • 不重启 mysql 服务临时生效可以这样设置
    mysql> show variables like ‘%time_zone%’;
    mysql> set global time_zone=‘+8:00’;
    mysql> flush privileges;
    在这里插入图片描述

报错:

Caused by:com.mysql.cj.exceptions.InvalidConnectionAttributeException: Theserver time zone value ‘Öйú±ê׼ʱ¼ä’ is unrecognized or represents more than one time zone. You must configure either the server or JDBC driver (via the serverTimezone configuration property) to use a more specifc time zone value if you want to utilize time zone support

翻译:com.mysql.cj异常,InvalidConnectionAttributeException服务时区值 ‘Öйú±ê׼ʱ¼ä’, 如果您想利用时区支持,您必须配置服务器或 JDBC 驱动程序(通过 ‘serverTimezone’ 配置属性)来使用更具体的时区值。
此问题为时区问题,在JDBC的连接url部分加上serverTimezone=UTC即可。

③添加allowPublicKeyRetrieval=true
报错:

java.sql.SQLNonTransientConnectionException: Public Key Retrieval is not allowe

④添加useOldAliasMetadataBehavior=true
报错:

column “username” not found

但是在数据库客户端执行语句没有此问题
原因 Java无法使用MySQL别名(as xxx)获取列,需要在连接池配置url加上useOldAliasMetadataBehavior=true

**

4.修改了mysql配置文件mysql.ini

**
报错:

Expression #1 of ORDER BY clause is not in GROUP BY clause and contains nonaggregated column ‘procuratorate.case_management.accepted_date’ which is not functionally dependent on columns in GROUP BY clause; this is incompatible with sql_mode=only_full_group_by

简单来说就是:输出的结果是叫target list,就是select后面跟着的字段,还有一个地方group by column,就是group by后面跟着的字段。由于开启了ONLY_FULL_GROUP_BY的设置,所以如果一个字段没有在target list 和group by字段中同时出现,或者是聚合函数的值的话,那么这条sql查询是被mysql认为非法的,会报错误。

解决办法:
1)可以采取临时解决方法,但mysql重启后仍然会报这个错误
查询模型:SELECT @@GLOBAL.sql_mode; SELECT @@SESSION.sql_mode;
在这里插入图片描述
可以看到mysql的配置是含有sql_mode=“ONLY_FULL_GROUP_BY”

接下来执行以下4条sql语句即可解决 (重启mysql会失效):

set sql_mode='STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_ENGINE_SUBSTITUTION';
 
set global sql_mode='STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION';
 
set session sql_mode='STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION';
 
set @@sql_mode='STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION';

2)最终解决办法:修改MySQL的配置文件
Windows下打开my.ini文件 添加下列文件 注意不要添加在最后面,不会生效的, 最好添加在[mysqld]下:

sql_mode=STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_ENGINE_SUBSTITUTION

猜你喜欢

转载自blog.csdn.net/weixin_42915457/article/details/131561075
今日推荐