【日常记录-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 01】Docker 简介
运维·docker·容器
开挖掘机上班2 小时前
mysql已经安装,但是通过rpm -q 没有找mysql相关的已安装包
数据库·mysql
花月C2 小时前
Mysql-定时删除数据库中的验证码
数据库·后端·mysql·spring
jugt2 小时前
CentOS 7.9安装Nginx1.24.0时报 checking for LuaJIT 2.x ... not found
linux·运维·centos
21号 13 小时前
9.进程间通信
linux·运维·服务器
阿福不是狗6 小时前
Python使用总结之Mac安装docker并配置wechaty
python·macos·docker
@小红花6 小时前
MySQL数据库从0到1
数据库·mysql·oracle
[听得时光枕水眠]7 小时前
MySQL基础(三)DQL(Data Query Language,数据查询语言)
数据库·mysql·oracle
叶落闲庭9 小时前
【k8s】k8s集群搭建
云原生·容器·kubernetes