[英]Cannot install mysqlclient in a docker mysql container
我在 Docker conatiner hat 中安裝mysqlclient
時遇到了一些問題,該帽子是由 mysql 優化的 Docker 圖像 ( mysql/mysql-server
) 制作的。 該容器在 Oracle Linux Sysrem (CentOS) 上運行。
錯誤看起來像這樣
Installing collected packages: mysqlclient, raven, redis, xmltodict, responses
Running setup.py install for mysqlclient ... error
Complete output from command /tracking_api/tracking/bin/python3.6 -u -c "import setuptools, tokenize;__file__='/tmp/pip-install-z_jafqw3/mysqlclient/setup.py';f=getattr(tokenize, 'open', open)(__file__);code=f.read().replace('\r\n', '\n');f.close();exec(compile(code, __file__, 'exec'))" install --record /tmp/pip-record-vqelrf7r/install-record.txt --single-version-externally-managed --compile --install-headers /tracking_api/tracking/include/site/python3.6/mysqlclient:
running install
running build
running build_py
creating build
creating build/lib.linux-x86_64-3.6
copying _mysql_exceptions.py -> build/lib.linux-x86_64-3.6
creating build/lib.linux-x86_64-3.6/MySQLdb
copying MySQLdb/__init__.py -> build/lib.linux-x86_64-3.6/MySQLdb
copying MySQLdb/compat.py -> build/lib.linux-x86_64-3.6/MySQLdb
copying MySQLdb/connections.py -> build/lib.linux-x86_64-3.6/MySQLdb
copying MySQLdb/converters.py -> build/lib.linux-x86_64-3.6/MySQLdb
copying MySQLdb/cursors.py -> build/lib.linux-x86_64-3.6/MySQLdb
copying MySQLdb/release.py -> build/lib.linux-x86_64-3.6/MySQLdb
copying MySQLdb/times.py -> build/lib.linux-x86_64-3.6/MySQLdb
creating build/lib.linux-x86_64-3.6/MySQLdb/constants
copying MySQLdb/constants/__init__.py -> build/lib.linux-x86_64-3.6/MySQLdb/constants
copying MySQLdb/constants/CLIENT.py -> build/lib.linux-x86_64-3.6/MySQLdb/constants
copying MySQLdb/constants/CR.py -> build/lib.linux-x86_64-3.6/MySQLdb/constants
copying MySQLdb/constants/ER.py -> build/lib.linux-x86_64-3.6/MySQLdb/constants
copying MySQLdb/constants/FIELD_TYPE.py -> build/lib.linux-x86_64-3.6/MySQLdb/constants
copying MySQLdb/constants/FLAG.py -> build/lib.linux-x86_64-3.6/MySQLdb/constants
copying MySQLdb/constants/REFRESH.py -> build/lib.linux-x86_64-3.6/MySQLdb/constants
running build_ext
building '_mysql' extension
creating build/temp.linux-x86_64-3.6
gcc -pthread -Wno-unused-result -Wsign-compare -DDYNAMIC_ANNOTATIONS_ENABLED=1 -DNDEBUG -O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector-strong --param=ssp-buffer-size=4 -grecord-gcc-switches -m64 -mtune=generic -D_GNU_SOURCE -fPIC -fwrapv -fPIC -Dversion_info=(1,3,12,'final',0) -D__version__=1.3.12 -I/usr/include/mysql -I/tracking_api/tracking/include -I/usr/include/python3.6m -c _mysql.c -o build/temp.linux-x86_64-3.6/_mysql.o -m64
unable to execute 'gcc': No such file or directory
error: command 'gcc' failed with exit status 1
----------------------------------------
Command "/tracking_api/tracking/bin/python3.6 -u -c "import setuptools, tokenize;__file__='/tmp/pip-install-z_jafqw3/mysqlclient/setup.py';f=getattr(tokenize, 'open', open)(__file__);code=f.read().replace('\r\n', '\n');f.close();exec(compile(code, __file__, 'exec'))" install --record /tmp/pip-record-vqelrf7r/install-record.txt --single-version-externally-managed --compile --install-headers /tracking_api/tracking/include/site/python3.6/mysqlclient" failed with error code 1 in /tmp/pip-install-z_jafqw3/mysqlclient/
我注意到這個問題是因為容器中沒有gcc
引起的。 使用yum install gcc
gcc
,我收到另一個錯誤,如下所示
Installing collected packages: mysqlclient, raven, redis, xmltodict, responses
Running setup.py install for mysqlclient ... error
Complete output from command /tracking_api/tracking/bin/python3.6 -u -c "import setuptools, tokenize;__file__='/tmp/pip-install-90ek02_q/mysqlclient/setup.py';f=getattr(tokenize, 'open', open)(__file__);code=f.read().replace('\r\n', '\n');f.close();exec(compile(code, __file__, 'exec'))" install --record /tmp/pip-record-rdg1c3go/install-record.txt --single-version-externally-managed --compile --install-headers /tracking_api/tracking/include/site/python3.6/mysqlclient:
running install
running build
running build_py
creating build
creating build/lib.linux-x86_64-3.6
copying _mysql_exceptions.py -> build/lib.linux-x86_64-3.6
creating build/lib.linux-x86_64-3.6/MySQLdb
copying MySQLdb/__init__.py -> build/lib.linux-x86_64-3.6/MySQLdb
copying MySQLdb/compat.py -> build/lib.linux-x86_64-3.6/MySQLdb
copying MySQLdb/connections.py -> build/lib.linux-x86_64-3.6/MySQLdb
copying MySQLdb/converters.py -> build/lib.linux-x86_64-3.6/MySQLdb
copying MySQLdb/cursors.py -> build/lib.linux-x86_64-3.6/MySQLdb
copying MySQLdb/release.py -> build/lib.linux-x86_64-3.6/MySQLdb
copying MySQLdb/times.py -> build/lib.linux-x86_64-3.6/MySQLdb
creating build/lib.linux-x86_64-3.6/MySQLdb/constants
copying MySQLdb/constants/__init__.py -> build/lib.linux-x86_64-3.6/MySQLdb/constants
copying MySQLdb/constants/CLIENT.py -> build/lib.linux-x86_64-3.6/MySQLdb/constants
copying MySQLdb/constants/CR.py -> build/lib.linux-x86_64-3.6/MySQLdb/constants
copying MySQLdb/constants/ER.py -> build/lib.linux-x86_64-3.6/MySQLdb/constants
copying MySQLdb/constants/FIELD_TYPE.py -> build/lib.linux-x86_64-3.6/MySQLdb/constants
copying MySQLdb/constants/FLAG.py -> build/lib.linux-x86_64-3.6/MySQLdb/constants
copying MySQLdb/constants/REFRESH.py -> build/lib.linux-x86_64-3.6/MySQLdb/constants
running build_ext
building '_mysql' extension
creating build/temp.linux-x86_64-3.6
gcc -pthread -Wno-unused-result -Wsign-compare -DDYNAMIC_ANNOTATIONS_ENABLED=1 -DNDEBUG -O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector-strong --param=ssp-buffer-size=4 -grecord-gcc-switches -m64 -mtune=generic -D_GNU_SOURCE -fPIC -fwrapv -fPIC -Dversion_info=(1,3,12,'final',0) -D__version__=1.3.12 -I/usr/include/mysql -I/tracking_api/tracking/include -I/usr/include/python3.6m -c _mysql.c -o build/temp.linux-x86_64-3.6/_mysql.o -m64
_mysql.c:29:19: fatal error: mysql.h: No such file or directory
#include "mysql.h"
^
compilation terminated.
error: command 'gcc' failed with exit status 1
----------------------------------------
Command "/tracking_api/tracking/bin/python3.6 -u -c "import setuptools, tokenize;__file__='/tmp/pip-install-90ek02_q/mysqlclient/setup.py';f=getattr(tokenize, 'open', open)(__file__);code=f.read().replace('\r\n', '\n');f.close();exec(compile(code, __file__, 'exec'))" install --record /tmp/pip-record-rdg1c3go/install-record.txt --single-version-externally-managed --compile --install-headers /tracking_api/tracking/include/site/python3.6/mysqlclient" failed with error code 1 in /tmp/pip-install-90ek02_q/mysqlclient/
我意識到這是由於mysql.h
不存在造成的。 我在這個問題中發現,我可以檢查文件是否存在。 但是, /use/local/
目錄下好像沒有mysql
文件夾。
這就是造成我困惑的原因。 有誰知道為什么會這樣? 當 docker 圖像本身是 mysql 優化時,為什么沒有mysql.h
文件? 提前致謝。
您需要安裝mysql-community-devel
, mysql.h
在此軟件包中。
注意:首先需要卸載mysql-community-server-minimal
,並在mysqlclient
安裝完成后重新安裝它。 否則,您將遇到以下沖突:
事務檢查錯誤:安裝mysql-community-devel-8.0.13-1.el7.x86_64中的文件/ usr / bin / mysql_config與軟件包mysql-community-server-minimal-8.0.13-1.el7中的文件沖突。 x86_64的
步驟如下:(省略pip安裝)
yum remove -y mysql-community-server-minimal
yum install -y gcc
yum install -y mysql-community-devel
pip install mysqlclient
yum remove -y mysql-community-devel
yum install -y mysql-community-server-minimal
mysqlclient install的輸出:
# pip install mysqlclient Collecting mysqlclient Using cached https://files.pythonhosted.org/packages/ec/fd/83329b9d3e14f7344d1cb31f128e6dbba70c5975c9e57896815dbb1988ad/mysqlclient-1.3.13.tar.gz Installing collected packages: mysqlclient Running setup.py install for mysqlclient ... done Successfully installed mysqlclient-1.3.13 You are using pip version 8.1.2, however version 18.1 is available. You should consider upgrading via the 'pip install --upgrade pip' command.
最后,您的最后一個問題:
當docker鏡像本身經過mysql優化時,為什么沒有mysql.h文件
從dockerhub,您可以在這里找到mysql/mysqlserver
dockerfile的鏈接:
FROM oraclelinux:7-slim ARG MYSQL_SERVER_PACKAGE=mysql-community-server-minimal-8.0.13 ARG MYSQL_SHELL_PACKAGE=mysql-shell-8.0.13 # Install server RUN yum install -y https://repo.mysql.com/mysql-community-minimal-release-el7.rpm \ https://repo.mysql.com/mysql-community-release-el7.rpm \ && yum-config-manager --enable mysql80-server-minimal \ && yum install -y \ $MYSQL_SERVER_PACKAGE \ $MYSQL_SHELL_PACKAGE \ libpwquality \ && yum clean all \ && mkdir /docker-entrypoint-initdb.d VOLUME /var/lib/mysql COPY docker-entrypoint.sh /entrypoint.sh COPY healthcheck.sh /healthcheck.sh ENTRYPOINT ["/entrypoint.sh"] HEALTHCHECK CMD /healthcheck.sh EXPOSE 3306 33060 CMD ["mysqld"]
從上面可以看到,它僅使用預構建的mysql服務器軟件包 ,沒有編譯mysql服務器的源代碼,因此不需要mysql.h
,僅在進行源代碼構建時使用。
但是,當您使用pip安裝mysqlclient
時,由於mysqlclient
python軟件包具有C extension
依賴關系,因此它將必須使用gcc
進行構建,然后它需要mysql.h
,這給您帶來麻煩。 安裝mysql.h
,一切正常。
你可以簡單地做
yum install -y --downloadonly --downloaddir=manual-install mysql-devel && \
rpm -i --force manual-install/*.rpm
這會在不安裝的情況下下載 mysql-devel 包,然后直接使用 rpm 命令進行安裝,這允許強制安裝
想知道為什么 yum 和 microdnf 都有 --force 參數
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.