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 的问题了

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