无法将SQL转储加载到容器化的MariaDB中。

huangapple go评论75阅读模式
英文:

can't load sql dump to the containered mariadb

问题

我正在尝试创建一个基于Docker的MariaDB数据库。在构建过程中,我需要加载SQL转储文件。

以下是我的Dockerfile。

FROM alpine:3.18.2

WORKDIR /Database

COPY . /Database

# 安装MySQL及其依赖项

RUN apk update && apk upgrade

RUN apk add --no-cache mysql mysql-client openrc bash

# 启动MySQL服务器

RUN mkdir /run/mysqld

RUN mysql_install_db --user=root

RUN mysqld -u root --data=./data & > /dev/null &

#RUN rc-service mariadb start

# 将数据加载到数据库

RUN mariadb -u root -e "CREATE DATABASE passwords;"

RUN mariadb -u root -p < dump.sql

EXPOSE 3306

ENTRYPOINT ["./entrypoint.sh"]

但是,在构建容器时,我收到以下错误:

ERROR 2002 (HY000): 无法通过套接字'/run/mysqld/mysqld.sock'(2)连接到本地服务器

据我了解,这似乎是在MariaDB未运行时出现的错误。但如果我取消注释 RUN rc-service mariadb start,则会收到另一个错误:

 * 警告: mariadb 正在启动
命令'/bin/sh -c rc-service mariadb start'返回了一个非零代码: 1

我尝试进入容器的shell并手动启动MariaDB,但未成功。

英文:

I am trying to make a dockerized mariadb database. In this database i need to load sql dump while build.

Here is my Dockerfile.

FROM alpine:3.18.2

WORKDIR /Database

COPY . /Database

#install mysql and dependencies

RUN apk update &amp;&amp; apk upgrade

RUN apk add --no-cache mysql mysql-client openrc bash

# start mysql server

RUN mkdir /run/mysqld

RUN mysql_install_db --user=root

RUN mysqld -u root --data=./data &amp;&gt; /dev/null &amp;

#RUN rc-service mariadb start

# load data to database

RUN mariadb -u root -e &quot;CREATE DATABASE passwords;&quot;

RUN mariadb -u root -p &lt; dump.sql

EXPOSE 3306

ENTRYPOINT [&quot;./entrypoint.sh&quot;]

But when I build container, I get this error:

ERROR 2002 (HY000): Can&#39;t connect to local server through socket &#39;/run/mysqld/mysqld.sock&#39; (2)

As i understand it appears when mariadb is not running, but if i uncomment the RUN rc-service mariadb start i will get another error:

 * WARNING: mariadb is already starting
The command &#39;/bin/sh -c rc-service mariadb start&#39; returned a non-zero code: 1

i have tried to enter the container's shell and start mariadb manualy but it was unsuccessful.

答案1

得分: 0

Docker构建阶段是独立的,你不能在RUN阶段运行后台进程并让它们在下一步中继续运行。

rc-servicesystemctlservice等命令无法运行,因为没有正在运行的init或openrc进程,这些命令会连接到这些进程。

生成容器的最简单方法是以初始镜像为基础:

FROM mariadb:lts
COPY dump.sql /docker-entrypoint-initdb.d

这将在启动时初始化数据库。

如果你想要一个真正预生成的镜像:

FROM mariadb:lts
COPY dump.sql /

RUN echo '[mariadb]\ndatadir=/data' > /etc/mysql/conf.d/localdata.cnf ; mkdir /data ; mariadb-install-db ; mariadbd --bootstrap < dump.sql; chown -R mysql: /data ; rm /dump.sql

请注意,这不会设置任何数据库密码。此外,dump.sql 将以以下方式开始:

CREATE DATABASE passwords;
use passwords;
...
create table ....
英文:

Docker build stages are independent and you can't run background process in the RUN stages and have them still running on the next step.

rc-service, systemctl, service etc won't run because there is no init or openrc process running which these commands connect to.

The easiest way to generate a container is to base it on the initial image:

FROM mariadb:lts
COPY dump.sql /docker-entrypoint-initdb.d

This will initialize the database on startup.

If you want a truly pre-generated image:

FROM mariadb:lts
COPY dump.sql /

RUN echo &#39;[mariadb]\ndatadir=/data&#39; &gt; /etc/mysql/conf.d/localdata.cnf ; mkdir /data ; mariadb-install-db ; mariadbd --bootstrap &lt; dump.sql; chown -R mysql: /data ; rm /dump.sql

Note this won't have any database passwords setup. Also the dump.sql will begin with:

CREATE DATABASE passwords;
use passwords;
...
create table ....

huangapple
  • 本文由 发表于 2023年6月25日 21:07:36
  • 转载请务必保留本文链接:https://go.coder-hub.com/76550551.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定