数据库1055错误

问题描述:在MySQL数据库下,执行SQL插入语句报错或者进入数据库时。出现1055错误信息。错误原因:在MySQL5.7之后,sql_mode中默认存在ONLY_FULL_GROUP_BY,SQL语句未通过ONLY_FULL_GROUP_BY语义检查所以报错。ONLY_FULL_GROUP_BY:ONLY_FULL_GROUP_BY要求select语句中查询出来的列必须是明确的(其他语句也是一样)。    insert、update、delete语句都会报错(但不影响SQL语句的执行),因为这三种语句执行之前也会执行查询操作。

以主键为id的表为例:name;该SQL执行成功,因为count是聚集函数;name;该SQL执行失败,因为*中包含主键id,而group by后的表达式中并没有包含idname;该SQL执行成功,因为name包含在group by后的表达式中name;该SQL执行失败,因为contact没有包含在group by后的表达式中

解决方案:一、永久解决

1)在MySQL下执行SELECT @@sql_mode语句

1054错误mysql mysql报1055错误_SQL

2)将查询结果中的ONLY_FULL_GROUP_BY去掉然后复制,打开MySQL的配置文件,将sql_mode的值设置为复制的值(若没有sql_mode在[mysqld]下方添加一行即可)。MySQL配置文件所在位置:安装版可通过windows服务所对应mysql启动项,查看其对应属性->可执行文件路径,获取my.ini路径。免安装版一般在其根目录下。(默认是my-default.ini,必须将名字改为my.ini才能生效)

1054错误mysql mysql报1055错误_1054错误mysql_02

3)重新MySQL服务即可生效!

详见:

如果没有找到my.ini配置文件

新建my.ini配置文件

有些时候需要设置mysql的属性,一般的可以通过以下方式找到my.ini文件的路径

mysql> show variables like 'datadir';
+---------------+--------------------------------------------+
| Variable_name | Value                                      |
+---------------+--------------------------------------------+
| datadir       | C:\ProgramData\MySQL\MySQL Server 8.0\ |
+---------------+--------------------------------------------+
1 row in set (0.03 sec)
1234567

执行结果就是配置文件的路径

:可以通过select @@basedir; 获取到mysql的安装路径

但是笔者的mysql有点傲娇,不太一样:

mysql> show variables like 'datadir';
+---------------+--------------------------------------------+
| Variable_name | Value                                      |
+---------------+--------------------------------------------+
| datadir       | C:\install\mysql\mysql-8.0.16-winx64\data\ |
+---------------+--------------------------------------------+
1 row in set (0.03 sec)
1234567

并且该目录下根本就没有my.ini配置文件。。。。。

嘿嘿~~ 那就新建一个!

比如在这里新建一个空的my.ini:

C:\install\mysql\mysql-8.0.16-winx64\my.ini
1

内容可以copy一份这里的:

# For advice on how to change settings please see
# http://dev.mysql.com/doc/refman/5.6/en/server-configuration-defaults.html
# *** DO NOT EDIT THIS FILE. It's a template which will be copied to the
# *** default location during install, and will be replaced if you
# *** upgrade to a newer version of MySQL.
[client]
default-character-set = utf8mb4
[mysql]
default-character-set = utf8mb4
[mysqld]
character-set-client-handshake = FALSE
character-set-server = utf8mb4
collation-server = utf8mb4_bin
init_connect='SET NAMES utf8mb4'
# Remove leading # and set to the amount of RAM for the most important data
# cache in MySQL. Start at 70% of total RAM for dedicated server, else 10%.
innodb_buffer_pool_size = 128M
# Remove leading # to turn on a very important data integrity option: logging
# changes to the binary log between backups.
# log_bin
# These are commonly set, remove the # and set as required.
basedir = D:\MySQL
datadir = D:\MySQL\data
port = 3306
# server_id = .....
# Remove leading # to set options mainly useful for reporting servers.
# The server defaults are faster for transactions and fast SELECTs.
# Adjust sizes as needed, experiment to find the optimal values.
join_buffer_size = 128M
sort_buffer_size = 16M
read_rnd_buffer_size