具体操作如下:
1.先启动mysql,执行下面的脚本(可选配置:–network=host )

docker run -d --name mysql -it  --restart=always \
-v /root/mysql/data:/var/lib/mysql \
-p 3306:3306 -e MYSQL_ROOT_PASSWORD="123456" \
-e MYSQL_USER="admin" -e MYSQL_PASSWORD="Xx123456." \
 mysql:5.7.26 --lower_case_table_names=1 --character-set-server=utf8 --collation-server=utf8_general_ci

2.把容器中的配置文件复制到宿主机,再修改配置

docker cp mysql:/etc/mysql/mysql.conf.d/mysqld.cnf /root/mysql/conf/

然后修改mysqld.cnf,加入sql_mode配置
sql_mode=STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION

本地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 stop mysql;docker rm mysql;docker run -d --name mysql -it  --restart=always \
-v /home/mysql/data:/var/lib/mysql \
-v /home/mysql/conf/mysqld.cnf:/etc/mysql/mysql.conf.d/mysqld.cnf \
# -v /home/mysql/log:/var/log/mysql \
-p 3306:3306 -e MYSQL_ROOT_PASSWORD="123456" \
-e MYSQL_USER="admin" -e MYSQL_PASSWORD="Xx123456." \
-e TZ=Asia/Shanghai \
 mysql:5.7.26 --lower_case_table_names=1 --character-set-server=utf8 --collation-server=utf8_general_ci

再次启动mysql,执行sql时没有 only_full_group_by 的问题了

如果要创建新用户,则进行如下操作:

# 1.进入mysql容器
docker exec -it mysql bash
# 2.登录mysql的root
mysql -uroot -p 
输入root密码:123456
# 3.创建用户和赋权限
use mysql;
# 3.1.创建一个新用户 testuser 密码为 testuserpass
 CREATE USER 'testuser'@'localhost' IDENTIFIED BY 'testuserpass';
# 3.2.创建数据库testDB
 create database testDB;
# 3.3.为testuser用户添加使用testDB权限
 grant all privileges on testDB.* to testuser@localhost identified by 'testuserpass';
 # 3.4.为testuser用户添加远程访问权限
  GRANT ALL PRIVILEGES ON testDB.* TO 'testuser'@'%' IDENTIFIED BY 'testuserpass' WITH GRANT OPTION;
 # 3.5.刷新权限表,使命令生效
 flush privileges;

# 3.6.为用户添加所有数据库的权限
grant all on *.* to testuser@'%' identified by 'testuserpass';  
Logo

权威|前沿|技术|干货|国内首个API全生命周期开发者社区

更多推荐