Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to make mysql in docker execute sql statement automatically when it starts?

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly introduces how to make the mysql in docker automatically execute the sql statement when it starts, it has a certain reference value, and interested friends can refer to it. I hope you can learn a lot after reading this article.

When creating a mysql container with docker, we sometimes expect that the database and table will be automatically built and the initialization data will be automatically entered after the container is started, that is, we can directly connect to the database in the container and use the data in the container after startup.

In fact, the official image of mysql supports this capability. The specified sql script or shell script is automatically executed when the container starts. Let's take a look at the Dockerfile of the official image of mysql, as shown below:

ENTRYPOINT has been set, and the script / entrypoint.sh will be called. Let's pull the image mysql:8 locally, start it with docker run, and take a look at the contents of the entrypoint.sh script. There is a section that traverses all the .sh and .sql suffixes from a fixed directory, and then executes them, as shown below:

Now that we've figured out the principle, let's put it into practice:

When building a disconf environment on docker, we need to build a mysql database, and execute four sql files in turn to initialize the database, table, and data. We can do this in two ways:

Copy the four sql files to the / docker-entrypoint-initdb.d directory, so that the four sql will be executed automatically when the container run, but from the screenshot script, the execution order in multiple files cannot be specified. If the script that creates the database is executed later than the script that creates the table, it will cause the table to fail, so this way of copying sql does not meet our needs (but If you synthesize four files into one sql in order, you can meet the requirements.

Make a sh file and execute the sql according to our own needs, as follows:

#! / bin/bashmysql-uroot-p$MYSQL_ROOT_PASSWORD

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report