Microsoft MVP성태의 닷넷 이야기
오류 유형: 776. uwsgi-plugin-python3 환경에서 MySQLdb 사용 환경 [링크 복사], [링크+제목 복사]
조회: 7597
글쓴 사람
정성태 (techsharer at outlook.com)
홈페이지
첨부 파일
 

uwsgi-plugin-python3 환경에서 MySQLdb 사용 환경

지난 글에 이어서,

uwsgi-plugin-python3 환경에서 "ModuleNotFoundError: No module named 'django'" 오류 발생
; https://www.sysnet.pe.kr/2/0/12883

결론 먼저 말하면, "uwsgi-plugin-python3" 구성 요소를 설치한 경우 MySQLdb 사용에 문제가 발생했을 때 이를 해결할 수 있는 방법을 모르겠습니다.

일단, pip install로 설치가 안 되었다면 "ModuleNotFoundError: No module named 'MySQLdb'" 예외가 발생할 것입니다. 지난 글에 썼듯이,

파이썬 - MySQLdb 기본 예제 코드
; https://www.sysnet.pe.kr/2/0/12851

"pip install mysqlclient" 명령어를 내리면 될 텐데요, "python:3.8-slim-buster" 이미지에 기반을 둔 컨테이너에서는 이렇게 오류가 발생합니다.

# pip install mysqlclient==2.0.3
Collecting mysqlclient==2.0.3
  Using cached mysqlclient-2.0.3.tar.gz (88 kB)
  Preparing metadata (setup.py) ... error
  ERROR: Command errored out with exit status 1:
   command: /usr/local/bin/python -c 'import io, os, sys, setuptools, tokenize; sys.argv[0] = '"'"'/tmp/pip-install-w1zz1c6a/mysqlclient_467a1bf0e991450692698d99ca84297f/setup.py'"'"'; __file__='"'"'/tmp/pip-install-w1zz1c6a/mysqlclient_467a1bf0e991450692698d99ca84297f/setup.py'"'"';f = getattr(tokenize, '"'"'open'"'"', open)(__file__) if os.path.exists(__file__) else io.StringIO('"'"'from setuptools import setup; setup()'"'"');code = f.read().replace('"'"'\r\n'"'"', '"'"'\n'"'"');f.close();exec(compile(code, __file__, '"'"'exec'"'"'))' egg_info --egg-base /tmp/pip-pip-egg-info-lt0x_9ui
       cwd: /tmp/pip-install-w1zz1c6a/mysqlclient_467a1bf0e991450692698d99ca84297f/
  Complete output (15 lines):
  /bin/sh: 1: mysql_config: not found
  /bin/sh: 1: mariadb_config: not found
  /bin/sh: 1: mysql_config: not found
  Traceback (most recent call last):
    File "<string>", line 1, in <module>
    File "/tmp/pip-install-w1zz1c6a/mysqlclient_467a1bf0e991450692698d99ca84297f/setup.py", line 15, in <module>
      metadata, options = get_config()
    File "/tmp/pip-install-w1zz1c6a/mysqlclient_467a1bf0e991450692698d99ca84297f/setup_posix.py", line 70, in get_config
      libs = mysql_config("libs")
    File "/tmp/pip-install-w1zz1c6a/mysqlclient_467a1bf0e991450692698d99ca84297f/setup_posix.py", line 31, in mysql_config
      raise OSError("{} not found".format(_mysql_config_path))
  OSError: mysql_config not found
  mysql_config --version
  mariadb_config --version
  mysql_config --libs
  ----------------------------------------
WARNING: Discarding https://files.pythonhosted.org/packages/3c/df/59cd2fa5e48d0804d213bdcb1acb4d08c403b61c7ff7ed4dd4a6a2deb3f7/mysqlclient-2.0.3.tar.gz#sha256=f6ebea7c008f155baeefe16c56cd3ee6239f7a5a9ae42396c2f1860f08a7c432 (from https://pypi.org/simple/mysqlclient/) (requires-python:>=3.5). Command errored out with exit status 1: python setup.py egg_info Check the logs for full command output.
ERROR: Could not find a version that satisfies the requirement mysqlclient==2.0.3 (from versions: 1.3.0, 1.3.1, 1.3.2, 1.3.3, 1.3.4, 1.3.5, 1.3.6, 1.3.7, 1.3.8, 1.3.9, 1.3.10, 1.3.11rc1, 1.3.11, 1.3.12, 1.3.13, 1.3.14, 1.4.0rc1, 1.4.0rc2, 1.4.0rc3, 1.4.0, 1.4.1, 1.4.2, 1.4.2.post1, 1.4.3, 1.4.4, 1.4.5, 1.4.6, 2.0.0, 2.0.1, 2.0.2, 2.0.3, 2.1.0rc1, 2.1.0)
ERROR: No matching distribution found for mysqlclient==2.0.3

에러 메시지를 보면 mysql_config, mariadb_config 중의 하나는 있어야 설치가 잘 되는 것 같습니다. 이에 대해 검색해 보면 "libmysqlclient-dev"를 설치하면 된다고 합니다.

$ sudo apt-get install libmysqlclient-dev

그런데, "python:3.8-slim-buster"가 사용하는 데비안에는 이런 패키지가 없다고 합니다. ^^

# apt-get install libmysqlclient-dev
Reading package lists... Done
Building dependency tree
Reading state information... Done
Package libmysqlclient-dev is not available, but is referred to by another package.
This may mean that the package is missing, has been obsoleted, or
is only available from another source
However the following packages replace it:
  libmariadb-dev-compat libmariadb-dev

E: Package 'libmysqlclient-dev' has no installation candidate

대신 메시지를 보면 "libmariadb-dev-compat, "libmariadb-dev" 패키지에 속해 있다고 하는데요, 따라서 "libmariadb-dev"를 설치를 하면 됩니다.

# apt install libmariadb-dev

이후 다시 "pip install mysqlclient==2.0.3" 설치를 시도하면 그래도 오류가 발생하지만 메시지는 바뀌었습니다.

# pip install mysqlclient==2.0.3
Collecting mysqlclient==2.0.3
  Using cached mysqlclient-2.0.3.tar.gz (88 kB)
  Preparing metadata (setup.py) ... done
Building wheels for collected packages: mysqlclient
  Building wheel for mysqlclient (setup.py) ... error
  ERROR: Command errored out with exit status 1:
   command: /usr/local/bin/python -u -c 'import io, os, sys, setuptools, tokenize; sys.argv[0] = '"'"'/tmp/pip-install-s5mutp72/mysqlclient_c01ebe68053541e8b1468e1a5ab5dc37/setup.py'"'"'; __file__='"'"'/tmp/pip-install-s5mutp72/mysqlclient_c01ebe68053541e8b1468e1a5ab5dc37/setup.py'"'"';f = getattr(tokenize, '"'"'open'"'"', open)(__file__) if os.path.exists(__file__) else io.StringIO('"'"'from setuptools import setup; setup()'"'"');code = f.read().replace('"'"'\r\n'"'"', '"'"'\n'"'"');f.close();exec(compile(code, __file__, '"'"'exec'"'"'))' bdist_wheel -d /tmp/pip-wheel-oys2zgga
       cwd: /tmp/pip-install-s5mutp72/mysqlclient_c01ebe68053541e8b1468e1a5ab5dc37/
  Complete output (43 lines):
  mysql_config --version
  /bin/sh: 1: mysql_config: not found
  mariadb_config --version
  ['10.3.31']
  mariadb_config --libs
  ['-L/usr/lib/x86_64-linux-gnu/', '-lmariadb']
  mariadb_config --cflags
  ['-I/usr/include/mariadb', '-I/usr/include/mariadb/mysql']
  ext_options:
    library_dirs: ['/usr/lib/x86_64-linux-gnu/']
    libraries: ['mariadb']
    extra_compile_args: ['-std=c99']
    extra_link_args: []
    include_dirs: ['/usr/include/mariadb', '/usr/include/mariadb/mysql']
    extra_objects: []
    define_macros: [('version_info', "(2,0,3,'final',0)"), ('__version__', '2.0.3')]
  running bdist_wheel
...[생략]...
  building 'MySQLdb._mysql' extension
  creating build/temp.linux-x86_64-3.8
  creating build/temp.linux-x86_64-3.8/MySQLdb
  gcc -pthread -Wno-unused-result -Wsign-compare -DNDEBUG -g -fwrapv -O3 -Wall -fPIC -Dversion_info=(2,0,3,'final',0) -D__version__=2.0.3 -I/usr/include/mariadb -I/usr/include/mariadb/mysql -I/usr/local/include/python3.8 -c MySQLdb/_mysql.c -o build/temp.linux-x86_64-3.8/MySQLdb/_mysql.o -std=c99
  unable to execute 'gcc': No such file or directory
  error: command 'gcc' failed with exit status 1
  ----------------------------------------
  ERROR: Failed building wheel for mysqlclient
  Running setup.py clean for mysqlclient
Failed to build mysqlclient
Installing collected packages: mysqlclient
    Running setup.py install for mysqlclient ... error
    ERROR: Command errored out with exit status 1:
     command: /usr/local/bin/python -u -c 'import io, os, sys, setuptools, tokenize; sys.argv[0] = '"'"'/tmp/pip-install-s5mutp72/mysqlclient_c01ebe68053541e8b1468e1a5ab5dc37/setup.py'"'"'; __file__='"'"'/tmp/pip-install-s5mutp72/mysqlclient_c01ebe68053541e8b1468e1a5ab5dc37/setup.py'"'"';f = getattr(tokenize, '"'"'open'"'"', open)(__file__) if os.path.exists(__file__) else io.StringIO('"'"'from setuptools import setup; setup()'"'"');code = f.read().replace('"'"'\r\n'"'"', '"'"'\n'"'"');f.close();exec(compile(code, __file__, '"'"'exec'"'"'))' install --record /tmp/pip-record-5b6anm21/install-record.txt --single-version-externally-managed --compile --install-headers /usr/local/include/python3.8/mysqlclient
         cwd: /tmp/pip-install-s5mutp72/mysqlclient_c01ebe68053541e8b1468e1a5ab5dc37/
    Complete output (43 lines):
    mysql_config --version
    /bin/sh: 1: mysql_config: not found
    mariadb_config --version
    ['10.3.31']
    mariadb_config --libs
    ['-L/usr/lib/x86_64-linux-gnu/', '-lmariadb']
    mariadb_config --cflags
    ['-I/usr/include/mariadb', '-I/usr/include/mariadb/mysql']
    ext_options:
      library_dirs: ['/usr/lib/x86_64-linux-gnu/']
      libraries: ['mariadb']
      extra_compile_args: ['-std=c99']
      extra_link_args: []
      include_dirs: ['/usr/include/mariadb', '/usr/include/mariadb/mysql']
      extra_objects: []
      define_macros: [('version_info', "(2,0,3,'final',0)"), ('__version__', '2.0.3')]
    running install
    running build
    running build_py
    creating build
...[생략]...
    running build_ext
    building 'MySQLdb._mysql' extension
    creating build/temp.linux-x86_64-3.8
    creating build/temp.linux-x86_64-3.8/MySQLdb
    gcc -pthread -Wno-unused-result -Wsign-compare -DNDEBUG -g -fwrapv -O3 -Wall -fPIC -Dversion_info=(2,0,3,'final',0) -D__version__=2.0.3 -I/usr/include/mariadb -I/usr/include/mariadb/mysql -I/usr/local/include/python3.8 -c MySQLdb/_mysql.c -o build/temp.linux-x86_64-3.8/MySQLdb/_mysql.o -std=c99
    unable to execute 'gcc': No such file or directory
    error: command 'gcc' failed with exit status 1
    ----------------------------------------
ERROR: Command errored out with exit status 1: /usr/local/bin/python -u -c 'import io, os, sys, setuptools, tokenize; sys.argv[0] = '"'"'/tmp/pip-install-s5mutp72/mysqlclient_c01ebe68053541e8b1468e1a5ab5dc37/setup.py'"'"'; __file__='"'"'/tmp/pip-install-s5mutp72/mysqlclient_c01ebe68053541e8b1468e1a5ab5dc37/setup.py'"'"';f = getattr(tokenize, '"'"'open'"'"', open)(__file__) if os.path.exists(__file__) else io.StringIO('"'"'from setuptools import setup; setup()'"'"');code = f.read().replace('"'"'\r\n'"'"', '"'"'\n'"'"');f.close();exec(compile(code, __file__, '"'"'exec'"'"'))' install --record /tmp/pip-record-5b6anm21/install-record.txt --single-version-externally-managed --compile --install-headers /usr/local/include/python3.8/mysqlclient Check the logs for full command output.

음... gcc가 있어야 한다는군요. ^^; 그래도 마지막 단계이니 기쁜 마음으로 설치하고,

# apt install build-essential

이후 mysqlclient가 정상적으로 설치됩니다.

# pip install mysqlclient==2.0.3




그런데 실제로 uwsgi-plugin-python3으로 호스팅하는 파이썬 코드에서 사용해 보면 이번에는 다음과 같은 오류가 발생합니다.

NameError: name '_mysql' is not defined

검색해 보면,

NameError: name '_mysql' is not defined after setting change to mysql
; https://stackoverflow.com/questions/63109987/nameerror-name-mysql-is-not-defined-after-setting-change-to-mysql

이 오류는 mysql Native 라이브러리를 찾지 못해서인 것 같습니다.

# find / -path /mnt -prune -o -name mysql 2>/dev/null
/etc/mysql
/mnt
/usr/local/lib/python3.8/site-packages/django/db/backends/mysql
/usr/local/lib/python3.8/site-packages/django/contrib/gis/db/backends/mysql
/usr/include/mariadb/server/mysql
/usr/include/mariadb/mysql

뭔가 구성은 된 것 같은데 ^^... 어쨌든 여기서 막혔습니다. 재미있는 것은, 그냥 python 3.8 shell에서는 아무런 문제 없이 import가 잘 됩니다.

# python
Python 3.8.12 (default, Dec  3 2021, 02:23:43)
[GCC 8.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import MySQLdb
>>> MySQLdb.connect("192.168.100.50", "testusr", "testpw", "testdb", charset='utf8', connect_timeout=3)
<_mysql.connection open to '192.168.100.50' at 0x556d823fb3e0>
>>>

하지만 동일한 명령어가 (내부적으로 3.7을 호스팅하는) uwsgi-plugin-python3 구성 요소의 환경에서는 오류가 발생하는 것입니다. 아무래도 이게 해결되려면 uwsgi-plugin-python3가 자체 호스팅하는 python 3.7 환경에 대해 pip install을 적용해 mysqlclient를 설치해야 하지 않을까 싶은데, 그 방법을 모르겠습니다. (혹시 아시는 분은 덧글 부탁드립니다. ^^)




하지만, 저런 노력 필요 없이 그냥 애당초 uwsgi-plugin-python3 구성 요소를 사용하지 않고 "pip install uwsgi"로 설치한 환경에서 동작시키면 잘 됩니다. ^^;




[이 글에 대해서 여러분들과 의견을 공유하고 싶습니다. 틀리거나 미흡한 부분 또는 의문 사항이 있으시면 언제든 댓글 남겨주십시오.]







[최초 등록일: ]
[최종 수정일: 12/20/2021]

Creative Commons License
이 저작물은 크리에이티브 커먼즈 코리아 저작자표시-비영리-변경금지 2.0 대한민국 라이센스에 따라 이용하실 수 있습니다.
by SeongTae Jeong, mailto:techsharer at outlook.com

비밀번호

댓글 작성자
 




1  2  3  4  5  6  7  8  9  [10]  11  12  13  14  15  ...
NoWriterDateCnt.TitleFile(s)
13373정성태6/19/20234398오류 유형: 865. 파이썬 - pymssql 설치 관련 오류 정리
13372정성태6/15/20233111개발 환경 구성: 682. SQL Server TLS 통신을 위해 사용되는 키 길이 확인 방법
13371정성태6/15/20233132개발 환경 구성: 681. openssl - 인증서 버전(V1 / V3)
13370정성태6/14/20233296개발 환경 구성: 680. C# - Ubuntu + Microsoft.Data.SqlClient + SQL Server 2008 R2 연결 방법 - TLS 1.2 지원
13369정성태6/13/20233094개발 환경 구성: 679. PyCharm(을 비롯해 JetBrains에 속한 여타) IDE에서 내부 Window들의 탭이 없어진 경우
13368정성태6/13/20233225개발 환경 구성: 678. openssl로 생성한 인증서를 SQL Server의 암호화 인증서로 설정하는 방법
13367정성태6/10/20233333오류 유형: 864. openssl로 만든 pfx 인증서를 Windows Server 2016 이하에서 등록 시 "The password you entered is incorrect" 오류 발생
13366정성태6/10/20233130.NET Framework: 2128. C# - 윈도우 시스템에서 지원하는 암호화 목록(Cipher Suites) 나열파일 다운로드1
13365정성태6/8/20232896오류 유형: 863. MODIFY FILE encountered operating system error 112(failed to retrieve text for this error. Reason: 15105)
13364정성태6/8/20233678.NET Framework: 2127. C# - Ubuntu + Microsoft.Data.SqlClient + SQL Server 2008 R2 연결 방법 [1]
13363정성태6/7/20233242스크립트: 49. 파이썬 - "Transformers (신경망 언어모델 라이브러리) 강좌" - 1장 2절 코드 실행 결과
13362정성태6/1/20233164.NET Framework: 2126. C# - 서버 측의 요청 제어 (Microsoft.AspNetCore.RateLimiting)파일 다운로드1
13361정성태5/31/20233638오류 유형: 862. Facebook - ASP.NET/WebClient 사용 시 graph.facebook.com/me 호출에 대해 403 Forbidden 오류
13360정성태5/31/20233036오류 유형: 861. WSL/docker - failed to start shim: start failed: io.containerd.runc.v2: create new shim socket
13359정성태5/19/20233354오류 유형: 860. Docker Desktop - k8s 초기화 무한 반복한다면?
13358정성태5/17/20233660.NET Framework: 2125. C# - Semantic Kernel의 Semantic Memory 사용 예제 [1]파일 다운로드1
13357정성태5/16/20233464.NET Framework: 2124. C# - Semantic Kernel의 Planner 사용 예제파일 다운로드1
13356정성태5/15/20233769DDK: 10. Device Driver 테스트 설치 관련 오류 (Code 37, Code 31) 및 인증서 관련 정리
13355정성태5/12/20233685.NET Framework: 2123. C# - Semantic Kernel의 ChatGPT 대화 구현 [1]파일 다운로드1
13354정성태5/12/20233957.NET Framework: 2122. C# - "Use Unicode UTF-8 for worldwide language support" 설정을 한 경우, 한글 입력이 '\0' 문자로 처리
13352정성태5/12/20233569.NET Framework: 2121. C# - Semantic Kernel의 대화 문맥 유지파일 다운로드1
13351정성태5/11/20234072VS.NET IDE: 185. Visual Studio - 원격 Docker container 내에 실행 중인 응용 프로그램에 대한 디버깅 [1]
13350정성태5/11/20233323오류 유형: 859. Windows Date and Time - Unable to continue. You do not have permission to perform this task
13349정성태5/11/20233661.NET Framework: 2120. C# - Semantic Kernel의 Skill과 Function 사용 예제파일 다운로드1
13348정성태5/10/20233570.NET Framework: 2119. C# - Semantic Kernel의 "Basic Loading of the Kernel" 예제
13347정성태5/10/20233933.NET Framework: 2118. C# - Semantic Kernel의 Prompt chaining 예제파일 다운로드1
1  2  3  4  5  6  7  8  9  [10]  11  12  13  14  15  ...