【日常记录-Docker】构建自定义MySQL镜像

复制代码
Author:赵志乾
Date:2024-08-02
Declaration:All Right Reserved!!!

1. 概述

自定义MySQL镜像,使其在启动时执行指定的SQL脚本;

2. 文件说明

总共3个文件:my.cnf、db.sql、Dockerfile,且三个文件放在同一个目录。

mysql的配置文件,文件名为my.cnf,内容如下:

复制代码
[client]
default-character-set=utf8mb4
[mysql]
default-character-set=utf8mb4
[mysqld]
init_connect='SET collation_connection = utf8mb4_unicode_ci'
init_connect='SET NAMES utf8mb4'
default_authentication_plugin = mysql_native_password
default-time-zone='+08:00'
character-set-server=utf8mb4
collation-server=utf8mb4_unicode_ci
expire_logs_days=1
skip-character-set-client-handshake
skip-name-resolve
secure_file_priv=null
skip-log-bin

自定义sql文件,文件名为db.sql,内容如下:

复制代码
#创建数据库
create database if not exists mydb;
#切换数据库
use mydb;
#创建数据表
create table if not exists `input_data` (
    `id`          bigint(20)    unsigned   not null auto_increment,
    `task_id`     varchar(50)              not null    default '0'   comment '任务id',
    `iteration`   int(10)                  not null    default 0     comment '迭代次数',
    `value`       longblob                 not null                  comment '数据内容',
    `create_time` datetime                 not null    default CURRENT_TIMESTAMP,
    `update_time` datetime                 not null    default CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
    primary key             (`id`)                   using btree,
    unique index `uniq_idx` (`task_id`, `iteration`) using btree
)
comment = '输入数据'
collate ='utf8mb4_0900_ai_ci'
engine =InnoDB
auto_increment=1
;

Dockerfile内容如下:

复制代码
FROM mysql:8.0.26

COPY my.cnf /etc/mysql/my.cnf
COPY db.sql /docker-entrypoint-initdb.d/db.sql
RUN chmod a+x /docker-entrypoint-initdb.d/db.sql

3. 镜像构建与容器运行

复制代码
# 构建镜像
docker build --no-cache -f ./Dockerfile -t custommysql:8.0.26 .
# 运行容器
docker run -p 3308:3306 --name custommysql -e MYSQL_ROOT_PASSWORD=123456 -v /project/mysql:/var/lib/mysql -d --restart=always custommysql:8.0.26
相关推荐
JuiceFS13 小时前
从 MLPerf Storage v2.0 看 AI 训练中的存储性能与扩展能力
运维·后端
Java水解16 小时前
Mysql查看执行计划、explain关键字详解(超详细)
后端·mysql
chen94518 小时前
mysql 3节点mgr集群部署
运维·后端
知其然亦知其所以然19 小时前
MySQL 社招必考题:如何优化查询过程中的数据访问?
后端·mysql·面试
LH_R19 小时前
OneTerm开源堡垒机实战(三):功能扩展与效率提升
运维·后端·安全
dessler20 小时前
Hadoop HDFS-高可用集群部署
linux·运维·hdfs
DemonAvenger21 小时前
NoSQL与MySQL混合架构设计:从入门到实战的最佳实践
数据库·mysql·性能优化
程序新视界21 小时前
如何在MySQL中创建聚集索引?
mysql
AAA修煤气灶刘哥1 天前
后端人速藏!数据库PD建模避坑指南
数据库·后端·mysql
程序新视界1 天前
学习MySQL绕不开的两个基础概念:聚集索引与非聚集索引
mysql