Solve the problem that the server (local) mysql database reports this is incompatible with sql_mode=only_full_group_by error

server

Yesterday, the project tried to deploy to the cloud, and then modified the database, but I don't know what happened, and a bunch of 500s were reported. The main reason was that
this is incompatible with sql_mode=only_full_group_by
many 500s were sent concurrently because of this error.
This is the product printed by the console

Expression #1 of SELECT list is not in GROUP BY clause and contains
nonaggregated column ‘数据库名.表.字段名’ which is not
functionally dependent on columns in GROUP BY clause; this is
incompatible with sql_mode=only_full_group_by] with root cause

After learning from the solutions of many predecessors, I found that I still have to rely on my own smart brain.
Modify the configuration file directly, simple and crude, and permanent modification.

Solve the problem

My server is ubuntu. I have modified it in /etc/mysql/my.cnf. Enter it under [mysqld]. If there is no [mysqld], write [mysqld] at the bottom.


sql_mode='STRICT_TRANS_TABLES,NO_ENGINE_SUBSTITUTION'

Some servers are configured in the my.ini file. You can try many times. If you are a programmer, don't be afraid of reporting errors.
It can also be configured under [mysqld]

collation-server = utf8_unicode_ci
init-connect='SET NAMES utf8'
character-set-server = utf8

Then restart the database

service mysql restart

Finally found that the bug was resolved.
If the problem cannot be solved after restarting, you can try configuring the above configuration in /etc/mysql/mysql.cnf and /etc/mysql/conf.d/mysql.cnf.

local

Query @@GLOBAL.sql_mode on Navicat;

select @.sql_mode;

Check to see if there is only_full_group_by in the results. If so, you need to remove it.

Option 1: Modify the configuration file and make permanent changes.
Find the my.ini file in C:\Program Files\MySQL\MySQL Server 8.0. If you cannot find it, look for my.ini in the C:\ProgramData\MySQL\MySQL Server 8.0 folder. Depending on the version of mysql, the my.ini file will be in different folders by default.
Add under [mysqld]:

sql_mode=STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_ENGINE_SUBSTITUTION

Remember: Do not modify sql-mode, the two are different. Although it is the same as the value of sql_mode and contains only_full_group_by, it is irrelevant.
Insert image description here
How to restart the mysql service:
win+r, services.msc, find the service and restart it.

Option 2: Used for statement modification (not recommended, not permanent).
Enter the following in the query in Navicat:

SET @.sql_mode ='STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_ENGINE_SUBSTITUTION';
SET sql_mode ='STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_ENGINE_SUBSTITUTION';

Although the two statements solve the problem, they are temporary and will become invalid after restarting.
If you really can't find the my.ini configuration file on the local side, then reinstall the database. After all, the time to solve this problem is much longer than the time to reinstall. . .

Summarize

Let’s analyze the reasons and how they occur.
If you remove group up, you will find that the sql statement runs normally, but after adding it, an error is reported. It’s really strange. After some searching, it turns out that the MySQL version has turned on sql_mode by default since 5.7. As long as the fields that appear in the select, except the aggregate function, other fields must appear in the group up, but this will As a result, the data we are looking for will mutate and we will not get what we want.

The road is long, let us continue to work hard and enrich ourselves.

Supongo que te gusta

Origin blog.csdn.net/weixin_52473844/article/details/128671721
Recomendado
Clasificación