0

I want to run an init sql file at the first run of my MySQL:5.7.5 docker run.

I tired to run the following docker run command:

docker run -d -p 13306:3306 \
 -e MYSQL_ALLOW_EMPTY_PASSWORD=yes \
 -e MYSQL_DATABASE=storagedb \
 -v storage.sql:/docker-entrypoint-initdb.d/storage.sql \
 mysql:5.7.5

Then, I run docker exec into this container and use the following command mysql -uroot -p into the mysql server.

Then I run use storagedb command to select the -e MYSQL_DATABASE=storagedb specified Database。

In the end, run show tables , I can't see the table I created in the SQL file。

The sql file's content is:

create database storagedb;
use storagedb;
CREATE TABLE `storage_tbl` (
                               `id` int(11) NOT NULL AUTO_INCREMENT,
                               `commodity_code` varchar(255) DEFAULT NULL,
                               `count` int(11) DEFAULT 0,
                               PRIMARY KEY (`id`),
                               UNIQUE KEY (`commodity_code`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

I try again with a Dockerfile, TheDockerfile's content is:

FROM mysql:5.7
ADD storage.sql /docker-entrypoint-initdb.d/storage.sql
RUN chmod -R 775 /docker-entrypoint-initdb.d
ENV MYSQL_ALLOW_EMPTY_PASSWORD yes

I build this Dockerfile to a new image tmpMySQL, Then i run this image and go into the container.

Now, i can see the database storagedb and the table storage_tbl. why?

I don't want to build a new docker image. I just want to startup a container by a mysql image and run an init .sql file with /docker-entrypoint-initdb.d!

Your guidance would be appreciated, Thank you very much!

BiGajiu
  • 1
  • 2
  • as I can see from the run command you are using storage have you tried to empty this storage before re-trying again? – Kerolos William Sep 08 '22 at 15:08
  • https://stackoverflow.com/questions/42248198/how-to-mount-a-single-file-in-a-volume – STerliakov Sep 08 '22 at 16:38
  • @KerolosWilliam Do you mean the ``-v`` param : ``-v storage.sql:/docker-entrypoint-initdb.d/storage.sql \`` ? This '``storage``' just is a .sql file name. Are you mean delete this -v param ? – BiGajiu Sep 09 '22 at 01:00

0 Answers0