Execute initialization sql when docker mysql starts

Execute initialization sql when docker mysql starts

1. Pull the Mysql image

docker pull mysql:5.7

2. Check the mysql image

docker inspect mysql:5.7
"Entrypoint": [
 "docker-entrypoint.sh"
 ],

3. Create a directory for MySQL plugin locally

##Mount to the container/docker-entrypoint-initdb.d; 01_create_database.sql will be executed when MySQL starts
/root/mysql-5.7/init-data
  01_create_database.sql
  ##content
  create database test_database DEFAULT CHARSET utf8 COLLATE utf8_general_ci;
  grant all privileges on `test_database`.* to 'test_user'@'%' identified by '123456';
  flush privileges;

##Mount to the container /var/lib/mysql
/root/mysql-5.7/mysql

4. Start mysql

docker run -p 33336:3306 -v /root/mysql-5.7/mysql:/var/lib/mysql -v /root/mysql-5.7/init-data:/docker-entrypoint-initdb.d -e MYSQL_ROOT_PASSWORD=123456 --name mysql_5.7 -d mysql/mysql:5.7

5. Enter the container, log in to mysql, and check that the library test_database has been created

docker exec -ti <containerID> sh
mysql -uroot -p123456
show databases;

Summarize

The above is what I introduced to you about how to execute initialization sql when docker mysql starts. I hope it will be helpful to you. If you have any questions, please leave me a message and I will reply to you in time. I would also like to thank everyone for their support of the 123WORDPRESS.COM website!
If you find this article helpful, please feel free to reprint it and please indicate the source. Thank you!

You may also be interested in:
  • Docker starts MySQL configuration implementation process
  • How to automatically execute SQL statements when MySQL in Docker starts
  • Solution to MySQL initialization and startup failure in Docker
  • How to quickly start MySQL testing using Docker on Mac
  • How to initialize the Mysql database when the Docker container starts
  • Docker starts the mysql service implementation steps

<<:  Several magical uses of JS ES6 spread operator

>>:  Detailed installation process of mysql5.7.21 under win10

Recommend

How to define data examples in Vue

Preface In the development process, defining vari...

MySQL implements an example method of logging in without a password

Specific method: Step 1: Stop the mysql service /...

A brief analysis of MySQL cardinality statistics

1. What is the cardinality? Cardinality refers to...

Example of how to set up a multi-column equal height layout with CSS

Initially, multiple columns have different conten...

Solution to IDEA not being able to connect to MySQL port number occupation

I can log in to MYSQL normally under the command ...

Detailed examples of how to use the box-shadow property in CSS3

There are many attributes in CSS. Some attributes...

Detailed steps to upgrade mysql8.0.11 to mysql8.0.17 under win2008

Upgrade background: In order to solve the vulnera...

js implements a simple countdown

This article example shares the specific code of ...

How to solve the problem that the project in eclipse cannot be added to tomcat

1. Right-click the project and select properties ...

How to use ECharts in WeChat Mini Programs using uniapp

Today, we use uniapp to integrate Echarts to disp...

A brief discussion on common operations of MySQL in cmd and python

Environment configuration 1: Install MySQL and ad...

Solution to span width not being determined in Firefox or IE

Copy code The code is as follows: <html xmlns=...

Summary of MySQL injection bypass filtering techniques

First, let’s look at the GIF operation: Case 1: S...