docker initialization sql is performed when mysql is started

  • 2021-01-25 14:53:57
  • OfStack

1. Pull the Mysql image

docker pull mysql:5.7

2. Check mysql mirror


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

3. Locally create the mysql plugin directory


## Mount into the container /docker-entrypoint-initdb.d ; MySQL Executes on startup  01_create_database.sql
/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 into 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 you have created the library test_database


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

conclusion


Related articles: