当前位置:网站首页>Initialize MySQL database when docker container starts
Initialize MySQL database when docker container starts
2022-07-06 01:30:00 【a_ small_ cherry】
brief introduction : Docker It's used more and more in development , Recently, I made a Spring Boot application , To facilitate deployment Mysql Also on the Docker Run in . So how to initialize SQL Scripts and data ? Let's take a look .

Image download 、 Domain name resolution 、 Time synchronization please click Alibaba open source mirror site
1. Preface
Docker It's used more and more in development , Recently, I made a Spring Boot application , To facilitate deployment Mysql Also on the Docker Run in . So how to initialize SQL Scripts and data ?
I have two traditional solutions here . The first option is to manually import after the container is started , too low No, No . The second one is Spring Boot Client connection Mysql Initializes the database when the container , You can refer to flyway Database version control , But it depends on the capabilities of the client . Can we do it Mysql When the container starts, it initializes the database itself ? Certainly. ! Today, let's demonstrate . See the end of the text for all codes .
2. principle
When Mysql When the container is first started , Will be in /docker-entrypoint-initdb.d Scan under Directory .sh,.sql,.sql.gz Files of type . If these types of files exist , They will be executed to initialize a database . These files are executed in alphabetical order . By default, they initialize the... Declared when the container is started MYSQL_DATABASE Variable defined in the database , For example, the following command initializes a REGION_DB database :

If your startup command does not specify a database, it must be in the database DDL The database is declared and specified in the script . Otherwise, the following exception will be implemented :

Then we will use this mechanism to realize Docker Initialize the database when the container starts .
3. Customize Dockerfile
We write our own Dockerfile To meet our needs , Here we use Mysql:5.7 For example . There may be some differences between different versions , You need to read the official documents in detail . The script is as follows :

- First step , Introduce the official Mysql:5.7 Docker Mirror image .
- The second step , No practical significance , Mainly the author 、 Organizational information .
- The third step , Very important ! I didn't configure the third line , Results after running the container, it was found that all the Chinese characters of the initialization data were garbled . Therefore, you need to modify before initializing the database Mysql Coding and other configurations , I'll change the time zone here to +8:00.
- Step four , Copy the script that contains the database ./sql Folder to mirror /tmp/sql Next .
- Step five , Use mv The command copies all the files in the folder in step 4 .sql File copy to /docker-entrypoint-initdb.d Next , In this way, we can make use of 2. The mechanism of the chapter initializes the database .
- Step six , Delete the used temporary directory .
Then you can build a custom image by using the build image command Mysql Mirror image :

adopt mysql:5.7c Mirror starts with a name of mysql-service The container of ,root The password for 123456, And persist the data to the host D:/mysql/data Next :
docker run --name mysql-service -v d:/mysql/data:/var/lib/mysql -p 3306:3306 -e MYSQL_ROOT_PASSWORD=123456 -d mysql:5.7c Tips: : You can go through SHOW VARIABLES LIKE 'character%' See if the character set is changed to utf8mb4, It can also be done through SHOW VARIABLES LIKE '%time_zone%' Check whether the time zone is East 8 .
4. summary
Today, we customize a program that can initialize the database Mysql Mirror image , It is convenient for us to deploy . You can also refer to this idea to customize other things you need Docker Mirror image .
In this paper, from :Docker Initialize when the container starts Mysql database - Alicloud developer community
边栏推荐
- Hcip---ipv6 experiment
- [pat (basic level) practice] - [simple mathematics] 1062 simplest fraction
- Netease smart enterprises enter the market against the trend, and there is a new possibility for game industrialization
- Folio.ink 免费、快速、易用的图片分享工具
- IP storage and query in MySQL
- 【全网最全】 |MySQL EXPLAIN 完全解读
- Who knows how to modify the data type accuracy of the columns in the database table of Damon
- [the most complete in the whole network] |mysql explain full interpretation
- 现货白银的一般操作方法
- File upload vulnerability test based on DVWA
猜你喜欢

Electrical data | IEEE118 (including wind and solar energy)

Basic operations of database and table ----- delete data table
![[detailed] several ways to quickly realize object mapping](/img/e5/70c7f8fee4556d14f969fe33938971.gif)
[detailed] several ways to quickly realize object mapping

Unity | 实现面部驱动的两种方式

Cookie concept, basic use, principle, details and Chinese transmission

【已解决】如何生成漂亮的静态文档说明页

Ordinary people end up in Global trade, and a new round of structural opportunities emerge
![[understanding of opportunity-39]: Guiguzi - Chapter 5 flying clamp - warning 2: there are six types of praise. Be careful to enjoy praise as fish enjoy bait.](/img/3c/ec97abfabecb3f0c821beb6cfe2983.jpg)
[understanding of opportunity-39]: Guiguzi - Chapter 5 flying clamp - warning 2: there are six types of praise. Be careful to enjoy praise as fish enjoy bait.

A picture to understand! Why did the school teach you coding but still not

伦敦银走势中的假突破
随机推荐
A picture to understand! Why did the school teach you coding but still not
MATLB|实时机会约束决策及其在电力系统中的应用
【Flask】官方教程(Tutorial)-part2:蓝图-视图、模板、静态文件
基於DVWA的文件上傳漏洞測試
Test de vulnérabilité de téléchargement de fichiers basé sur dvwa
XSS learning XSS lab problem solution
2022年广西自治区中职组“网络空间安全”赛题及赛题解析(超详细)
有谁知道 达梦数据库表的列的数据类型 精度怎么修改呀
Spir - V premier aperçu
LeetCode 322. Change exchange (dynamic planning)
[Jiudu OJ 09] two points to find student information
Use of crawler manual 02 requests
Redis-字符串类型
Leetcode skimming questions_ Sum of squares
Huawei converged VLAN principle and configuration
SPIR-V初窥
MATLB | real time opportunity constrained decision making and its application in power system
【全网最全】 |MySQL EXPLAIN 完全解读
C web page open WinForm exe
Threedposetracker project resolution