gpt4 book ai didi

mysql - 如何在dockerfile中还原MySQL数据库

转载 作者:行者123 更新时间:2023-12-02 19:47:57 24 4
gpt4 key购买 nike

我正在尝试将MySQL数据库还原到具有Apache和MySQL服务的ubuntu docker容器。这是我的 docker 文件
从ubuntu

RUN apt-get update -y
ENV DATABASE_SERVER 'IP'
ENV DEBIAN_FRONTEND=noninteractive
RUN apt-get install apache2 -y && apt-get install php7.4 -y && apt-get install mysql-server >
ENV APACHE_RUN_USER www-data
ENV APACHE_RUN_GROUP www-data
ENV APACHE_LOG_DIR /var/log/apache2
ENV APACHE_RUN_DIR /var/www/html/
COPY ./startup.sh /var/www/
COPY ./db_test.php /var/www/html
COPY ./my_sql_secure.sh /var/www/
COPY ./backup.sql /var/www/html/
RUN bash /var/www/my_sql_secure.sh
COPY ./restoredb.sh /var/www/
CMD bash /var/www/startup.sh
EXPOSE 80
这是我的startup.sh
apache2 -DFOREGROUND | service mysql start | mysql -uroot sentrifugo < /var/www/html/backup.sql
如果我在没有“mysql -uroot sentrifugo 据我所知,CMD仅接受两个命令,并在startup.sh替换它之后运行restored.b。
我只想还原mysql数据库并在前台运行mysql和apache。我不能按照我的要求使用docker-compose。
有人可以告诉我可以做些什么。
在此先多谢

最佳答案

它实际上取决于您正在构建FROM的图像,假设它是官方MySQL图像,则只需将COPY backup.sql放入种子文件夹:

When a container is started for the first time, a new database with the specified name will be created and initialized with the provided configuration variables. Furthermore, it will execute files with extensions .sh, .sql and .sql.gz that are found in /docker-entrypoint-initdb.d. Files will be executed in alphabetical order. You can easily populate your mysql services by mounting a SQL dump into that directory and provide custom images with contributed data. SQL files will be imported by default to the database specified by the MYSQL_DATABASE variable.


(来自 MySQL page on DockerHub)
因此,更改:
COPY ./backup.sql /var/www/html/
...至:
COPY ./backup.sql /docker-entrypoint-initdb.d/
如果您使用的是自定义图像,则可能是这样,您可能想要一个 ENTRYPOINT 在启动时执行该导入脚本。
另外,您的 startup.sh可能会进行一些更改:
service mysql start && \
mysql -uroot sentrifugo < /var/www/html/backup.sql;
apache2 -DFOREGROUND
这将首先启动MySQL服务,然后填充数据库。然后,最后启动Apache。
最后一件事, CMD的首选形式是exec形式。这会使您的 CMD看起来像这样:
CMD ["/bin/bash","/var/www/startup.sh"]
(引用: https://docs.docker.com/engine/reference/builder/#cmd)

关于mysql - 如何在dockerfile中还原MySQL数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62538876/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com