MySQL数据库报错

本文最后更新于:2025年2月18日 下午

MySQL数据库报错:

1
### Error querying database.  Cause: java.sql.SQLSyntaxErrorException: Expression #3 of SELECT list is not in GROUP BY clause and contains nonaggregated column 'hrdatabase.d.id' which is not functionally dependent on columns in GROUP BY clause; this is incompatible with sql_mode=only_full_group_by

解决方法1:【只生效一次,重启后就又恢复了】

在数据库命令行运行此命令:

1
set@@global.sql_mode='STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_ENGINE_SUBSTITUTION'

解决方法2:【永久生效】

修改数据库配置文件my.ini:

Windows系统下位置:C:\ProgramData\MySQL\MySQL Server 5.7

打开文件,把sql-mod中开头的 ONLY_FULL_GROUP_BY 去掉后保存即可。

1
2
3
4
5
# The current server SQL mode, which can be set dynamically.
# Modes affect the SQL syntax MySQL supports and the data validation checks it performs. This
# makes it easier to use MySQL in different environments and to use MySQL together with other
# database servers.
sql-mode="STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION"

重启MySQL服务器,就生效了。

日志记录控制台输出

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
package io.github.superlovelace.standard;


import java.io.IOException;
import java.util.logging.FileHandler;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.logging.SimpleFormatter;

public class LoggingRecord {
private static final Logger logger = Logger.getLogger(LoggingRecord.class.getName());

@SuppressWarnings("all")
public static void main(String[] args) {

try {
FileHandler fileHandler = new FileHandler("log.txt",true);
fileHandler.setFormatter(new SimpleFormatter());
logger.addHandler(fileHandler);

// 将 System.out 重定向到日志记录器
System.setOut(new LoggingPrintStream(logger, Level.INFO));
// 以下是你的代码块
System.out.println("Hello, World!");
int result = divide(10, 0);
System.out.println("Result: " + result);
} catch (IOException e) {
e.printStackTrace();
}
}

public static int divide(int dividend, int divisor) {
try {
return dividend / divisor;
} catch (ArithmeticException e) {
logger.log(Level.SEVERE, "Exception occurred: " + e.getMessage(), e);
throw e;
}
}
}

class LoggingPrintStream extends java.io.PrintStream {
private final Logger logger;
private final Level level;

public LoggingPrintStream(Logger logger, Level level) {
super(System.out);
this.logger = logger;
this.level = level;
}

@Override
public void println(String x) {
logger.log(level, x);
}
}


MySQL数据库报错
https://superlovelace.top/2024/05/31/MySQL异常错误/
作者
棱境
发布于
2024年5月31日
更新于
2025年2月18日
许可协议