【日常记录-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
相关推荐
回忆是昨天里的海12 分钟前
docker网络-自定义网络
运维·docker·容器
晚风吹人醒.16 分钟前
Awk文本处理工具:命令模式,脚本模式的介绍及正则表达式应用举例
linux·运维·服务器·awk
2401_8322981018 分钟前
云边协同新范式:边缘云服务器如何重构实时业务体验
运维·服务器·重构
飞函安全35 分钟前
Mattermost限制历史消息访问:开源软件商业化困局
运维·安全·开源软件
你好helloworld37 分钟前
linux离线安装nvidia-docker
linux·运维·服务器
飞Link38 分钟前
【开发工具】Docker常用操作
运维·docker·容器
IT教程资源D39 分钟前
[N_101]基于springboot,vue企业网盘系统
mysql·vue·前后端分离·springboot网盘
忙里偷闲学python42 分钟前
mysql
linux·数据库·mysql·oracle
nxlifebao3571 小时前
自媒体整体效率提升怎么做?AI智能媒体助理自动化是关键
运维·自动化·媒体·ai内容创作·自动化工作流·自媒体效率提升·多平台分发
sun0077001 小时前
iptables 配置 3台设备 进行路由转发(不同网段)
运维·服务器