【日常记录-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
相关推荐
Johny_Zhao5 小时前
Docker + CentOS 部署 Zookeeper 集群 + Kubernetes Operator 自动化运维方案
linux·网络安全·docker·信息安全·zookeeper·kubernetes·云计算·系统运维
zwjapple6 小时前
docker-compose一键部署全栈项目。springboot后端,react前端
前端·spring boot·docker
一心0927 小时前
ubuntu 20.04.6 sudo 源码包在线升级到1.9.17p1
运维·ubuntu·sudo·漏洞升级
好好学习啊天天向上7 小时前
世上最全:ubuntu 上及天河超算上源码编译llvm遇到的坑,cmake,ninja完整过程
linux·运维·ubuntu·自动性能优化
你想考研啊7 小时前
三、jenkins使用tomcat部署项目
运维·tomcat·jenkins
tan180°8 小时前
MySQL表的操作(3)
linux·数据库·c++·vscode·后端·mysql
代码老y8 小时前
Docker:容器化技术的基石与实践指南
运维·docker·容器
典学长编程8 小时前
Linux操作系统从入门到精通!第二天(命令行)
linux·运维·chrome
DuelCode9 小时前
Windows VMWare Centos Docker部署Springboot 应用实现文件上传返回文件http链接
java·spring boot·mysql·nginx·docker·centos·mybatis
幽络源小助理9 小时前
SpringBoot基于Mysql的商业辅助决策系统设计与实现
java·vue.js·spring boot·后端·mysql·spring