Docker Alpine:加载 MySQLdb 模块时出错

2023-06-25数据库问题
14

本文介绍了Docker Alpine:加载 MySQLdb 模块时出错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

我正在使用 MariaDB 构建基于 AlpineDjango 应用程序的映像,但我不知道应该添加哪个依赖项我的 Dockerfile 以便我的应用程序可以正确连接到数据库.

I am building an Alpine based image of a Django application with MariaDB and I can't figure out which dependency I should add to my Dockerfile so that my app could properly connect to the DB.

django.core.exceptions.ImproperlyConfigured: Error loading MySQLdb module.
Did you install mysqlclient?

好吧,我以为我做到了.从我读到的在本文中,这个讨论,mariadb-devAlpine 相当于基于 Debian 的系统中的 default-libmysqlclient-dev.此外,Alpine 中的 mysql-client 包 只是一个虚拟的包(包含 mariadb-devmariadb-client 等).这是Dockerfile:

Well, I thought I did. From what I read in this article, in this discussion, mariadb-dev is in Alpine the equivalent of default-libmysqlclient-dev in Debian based system. Furthermore, the mysql-client package in Alpine is merely a dummy package (containing mariadb-dev, mariadb-client, etc etc). Here is the Dockerfile:

# pull official base image
FROM python:3.7-alpine

# set environment variables
ENV PYTHONDONTWRITEBYTECODE 1
ENV PYTHONUNBUFFERED 1

# set work directory
WORKDIR /usr/src/cms

# install mysqlclient
RUN apk update 
    && apk add --virtual build-deps gcc python3-dev musl-dev 
    && apk add --no-cache mariadb-dev
    && apk del build-deps

# install dependencies
RUN pip install --upgrade pip
RUN pip install pipenv
COPY ./Pipfile /usr/src/cms/Pipfile
RUN pipenv install --skip-lock --system --dev

# copy entrypoint.sh
COPY ./entrypoint.sh /usr/src/cms/entrypoint.sh

# copy project
COPY . /usr/src/cms/

# run entrypoint.sh
ENTRYPOINT ["/usr/src/cms/entrypoint.sh"]

我尝试添加 mariadb-client,以使用 mysql-client 代替.我还尝试添加 RUN pip install django-mysql.似乎什么都没有改变.我成功地构建了 Postgres Django 应用程序,没有任何问题,但是,当谈到 MySQl 与 MariaDB//Debian 与 Alpine 时,我感到困惑.有什么见解吗?

I tried to add mariadb-client, to use mysql-client instead. I also tried to add RUN pip install django-mysql. Nothing seems to change. I successfully build Postgres Django apps without any problem but, when it comes to MySQl vs MariaDB // Debian vs Alpine, I feel confused. Any insight?

推荐答案

您似乎缺少 MySQLdb Python 模块,您应该为该模块安装 mysqlclient Python包:pip install mysqlclient.

It seems you're missing the MySQLdb Python module, for which you should install the mysqlclient Python package: pip install mysqlclient.

在 Alpine 上,pip 将从源代码构建 mysqlclient,因此您需要 gccmusl-dev 用于此设置步骤,因此你需要将 apk del build-deps 推迟到 Python 模块安装之后.

On Alpine, pip will build mysqlclient from source, so you'll need gcc and musl-dev for this setup step, hence you'll need to postpone apk del build-deps to after Python modules are installed.

固定的 Dockerfile 片段:

Fixed Dockerfile snippet:

RUN apk update 
    && apk add --virtual build-deps gcc python3-dev musl-dev 
    && apk add --no-cache mariadb-dev

...

RUN pip install mysqlclient  

RUN apk del build-deps

这篇关于Docker Alpine:加载 MySQLdb 模块时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

The End

相关推荐

Mysql目录里的ibtmp1文件过大造成磁盘占满的解决办法
ibtmp1是非压缩的innodb临时表的独立表空间,通过innodb_temp_data_file_path参数指定文件的路径,文件名和大小,默认配置为ibtmp1:12M:autoextend,也就是说在文件系统磁盘足够的情况下,这个文件大小是可以无限增长的。 为了避免ibtmp1文件无止境的暴涨导致...
2025-01-02 数据库问题
151

SQL 子句“GROUP BY 1"是什么意思?意思是?
What does SQL clause quot;GROUP BY 1quot; mean?(SQL 子句“GROUP BY 1是什么意思?意思是?)...
2024-04-16 数据库问题
62

MySQL groupwise MAX() 返回意外结果
MySQL groupwise MAX() returns unexpected results(MySQL groupwise MAX() 返回意外结果)...
2024-04-16 数据库问题
13

MySQL SELECT 按组最频繁
MySQL SELECT most frequent by group(MySQL SELECT 按组最频繁)...
2024-04-16 数据库问题
16

为什么 Mysql 的 Group By 和 Oracle 的 Group by 行为不同
Why Mysql#39;s Group By and Oracle#39;s Group by behaviours are different(为什么 Mysql 的 Group By 和 Oracle 的 Group by 行为不同)...
2024-04-16 数据库问题
13

MySQL GROUP BY DateTime +/- 3 秒
MySQL GROUP BY DateTime +/- 3 seconds(MySQL GROUP BY DateTime +/- 3 秒)...
2024-04-16 数据库问题
14