docker下 MYSQL5.7版本 sql_mode=only_full_group_by問題

解決辦法:把docker容器中mysql的 mysqld.cnf 文件複製出來,修改其配置,然後啓動容器時使用本地修改後的配置。

具體操作如下:
1.先啓動mysql,執行下面的腳本

docker run -d --name mysql -it  --restart=always \
-v /root/mysql/data:/var/lib/mysql \
-p 3306:3306 -e MYSQL_ROOT_PASSWORD=zhuyu123456 \
--network=host  mysql:5.7.26 --lower_case_table_names=1

2.進入到容器,複製mysql的配置文件

# 1.進入容器
docker exec -it mysql /bin/bas
# 2.登錄進入mysql
mysql -u -root -p
# 3.輸入mysql密碼,也就是啓動mysql容器時手動設置的密碼 zhuyu123456
# 4.查看sql_mode,並複製其內容,等下要寫到本地mysqld.cnf文件中
show variables like '%sql_mode';

#5.查看mysql容器的配置文件,並複製到本地的mysqld.cnf中
cat /etc/mysql/mysql.conf.d/mysqld.cnf

sql_mode的內容如下圖,複製好這行的值,去掉 最前面的 only_full_group_by ,並把內容放到本地的mysqld.cnf文件的最後,可參考下面的配置文件在這裏插入圖片描述

本地mysqld.cnf的內容如下:

# Copyright (c) 2014, 2016, Oracle and/or its affiliates. All rights reserved.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; version 2 of the License.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA

#
# The MySQL  Server configuration file.
#
# For explanations see
# http://dev.mysql.com/doc/mysql/en/server-system-variables.html

[mysqld]
pid-file        = /var/run/mysqld/mysqld.pid
socket          = /var/run/mysqld/mysqld.sock
datadir         = /var/lib/mysql
#log-error      = /var/log/mysql/error.log
# By default we only accept connections from localhost
#bind-address   = 127.0.0.1
# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0

#如果要更改docker mysql的端口,請先修改mysql.cnf文件中的端口
port=3306 

#從 show variables like '%sql_mode'; 裏面複製出來,並去掉了 only_full_group_by 
sql_mode=STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION

docker mysql 更改端口,需要先改MySQL配置文件中的端口,再在docker 啓動命令中 -p 3308:3308

3.定製mysql容器,且移除mysql容器,並使用本地的mysqld.cnf配置文件,腳本如下:

docker run -d --name mysql -it  --restart=always \
-v /root/mysql/data:/var/lib/mysql \
-v /root/mysql/conf/mysqld.cnf:/etc/mysql/mysql.conf.d/mysqld.cnf \
-p 3306:3306 -e MYSQL_ROOT_PASSWORD=zhuyu123456 \
--network=host  mysql:5.7.26 --lower_case_table_names=1

再次啓動mysql,執行sql時沒有 only_full_group_by 的問題了

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章