创建目录结构
目录结构如下
docker/docker-compose.yml
docker/mysql/data
docker/mysql/my.cnf
docker/redis/data
docker/redis/redis.conf
编写 my.cnf 文件
[client]
port = 3306
socket = /var/lib/mysql/data/mysql.sock
[mysqld]
# 针对5.7版本执行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'
# 一般配置选项
basedir = /var/lib/mysql
datadir = /var/lib/mysql/data
port = 3306
socket = /var/lib/mysql/data/mysql.sock
lc-messages-dir = /usr/share/mysql # 务必配置此项,否则执行sql出错时,只能显示错误代码而不显示具体错误消息
character-set-server=utf8
back_log = 300
max_connections = 3000
max_connect_errors = 50
table_open_cache = 4096
max_allowed_packet = 32M
#binlog_cache_size = 4M
max_heap_table_size = 128M
read_rnd_buffer_size = 16M
sort_buffer_size = 16M
join_buffer_size = 16M
thread_cache_size = 16
query_cache_size = 64M
query_cache_limit = 4M
ft_min_word_len = 8
thread_stack = 512K
transaction_isolation = REPEATABLE-READ
tmp_table_size = 64M
#log-bin=mysql-bin
long_query_time = 6
server_id=1
innodb_buffer_pool_size = 256M
innodb_thread_concurrency = 16
innodb_log_buffer_size = 16M
编写 redis.conf 文件
此处省略
编写 docker-compose.yml 文件
version: '3'
services:
mysql:
hostname: mysql
image: mysql:5.7.26
# network_mode: "host" # 如果需要容器使用宿主机IP(内网IP),则可以配置此项
container_name: mysql # 指定容器名称,如果不设置此参数,则由系统自动生成
restart: unless-stopped # 设置容器自启模式
command: mysqld --character-set-server=utf8mb4 --collation-server=utf8mb4_unicode_ci # 设置utf8字符集
environment:
- TZ=Asia/Shanghai # 设置容器时区与宿主机保持一致
- MYSQL_ROOT_PASSWORD=root # 设置root密码
volumes:
- /etc/localtime:/etc/localtime:ro # 设置容器时区与宿主机保持一致
- ./mysql/data:/var/lib/mysql/data # 映射数据库保存目录到宿主机,防止数据丢失
- ./mysql/my.cnf:/etc/mysql/my.cnf # 映射数据库配置文件
ports:
- "3306:3306"
redis:
hostname: redis
image: redis:5.0.4
container_name: redis
restart: unless-stopped
command: redis-server /etc/redis.conf # 启动redis命令
environment:
- TZ=Asia/Shanghai
volumes:
- /etc/localtime:/etc/localtime:ro # 设置容器时区与宿主机保持一致
- ./redis/data:/data
- ./redis/redis.conf:/etc/redis.conf
ports:
- "6379:6379"
创建并启动容器
如果文件名就叫做docker-compose.yml
则可以不适用-f
选项指定文件路劲,up
选项启动容器,-d
选项以守护模式运行
docker-compose -f docker-compose.yml up -d
如果要批量停止容器并清除容器,可以使用down
命令
docker-compose -f docker-compose.yml down
如果要重新构建容器,可以使用--build
选项
docker-compose -f docker-compose.yml up --build -d
启动成功后,可使用以下命令查看容器运行状态
docker ps -a
如果启动失败,可以查看容器日志信息获取帮助
docker logs 容器名词或容器ID # 既docker-compose.yml文件参数container_name指定的值
mysql 执行 group by 语句出错
In aggregated query without GROUP BY, expression #1 of SELECT list contains nonaggregated column 'db_test.t.id'; this is incompatible with sql_mode=only_full_group_by