【日常记录-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
相关推荐
w***9549几秒前
mysql之如何获知版本
数据库·mysql
Ares-Wang10 分钟前
网络》》防火墙
运维·服务器·网络
可爱又迷人的反派角色“yang”11 分钟前
k8s(四)
linux·网络·云原生·容器·kubernetes·云计算
朝阳58116 分钟前
树莓派 Ubuntu 系统登录问题完整指南:解决 Permission denied (publickey)错误
linux·运维·ubuntu
默|笙17 分钟前
【Linux】基础IO(1)文件、fd
linux·运维·服务器
凌波粒21 分钟前
Linux高级篇-日志管理/Linux裁剪/内核源码/备份与恢复/可视化管理
linux·运维·服务器
BJ_Bonree23 分钟前
数智先锋 | Bonree ONE助力温氏集团构建“零一五十”智能运维体系,夯实智慧养殖数字底座!
运维
m0_7269659824 分钟前
【服务器二】下载拓展成功
运维·服务器
2501_9399090525 分钟前
Rancher 管理 Kubernetes 集群与Pod的详解
容器·kubernetes·rancher
可爱又迷人的反派角色“yang”29 分钟前
k8s(二)
linux·运维·docker·云原生·容器·kubernetes·云计算