問題描述:在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語句中查詢出來的列必須是明確的(其他語句也是一樣)。 以SQL語句select columes from table group by list為例:columns必須是聚集函數或者在group by后的表達式list中,並且list中必須包含主鍵,否則也會報錯。 insert、update、delete語句都會報錯(但不影響SQL語句的執行),因為這三種語句執行之前也會執行查詢操作。
以主鍵為id的表為例: SELECT count(1) FROM customer GROUP BY name
;該SQL執行成功,因為count是聚集函數; SELECT * FROM customer GROUP BY name
;該SQL執行失敗,因為*中包含主鍵id,而group by后的表達式中並沒有包含id SELECT name FROM customer GROUP BY name
;該SQL執行成功,因為name包含在group by后的表達式中 SELECT name, contact FROM customer GROUP BY name
;該SQL執行失敗,因為contact沒有包含在group by后的表達式中
解決方案: 一、永久解決
1)在MySQL下執行SELECT @@sql_mode語句 2)將查詢結果中的ONLY_FULL_GROUP_BY去掉然后復制,打開MySQL的配置文件,將sql_mode的值設置為復制的值(若沒有sql_mode在[mysqld]下方添加一行即可)。 MySQL配置文件所在位置:安裝版可通過windows服務所對應mysql啟動項,查看其對應屬性->可執行文件路徑,獲取my.ini路徑。 免安裝版一般在其根目錄下。(默認是my-default.ini,必須將名字改為my.ini才能生效)
sql_mode=…(刪掉ONLY_FULL_GROUP_BY的那段)
詳見:https://www.cnblogs.com/haoyul/p/9882853.html
如果沒有找到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 = 16M