在 cPanel 上安装 mysqlclient 到 Django 项目时出现错误

4

我已经试图解决这个问题几天了。我试图将我的Django项目放在cPanel上的venv中,并安装mysqlclient。因此,在在Cpanel上设置好我的Python(版本=3.7.8)后,我使用终端安装了Django版本3.1.7和mysqlclient,命令为pip install djangopip install mysqlclient。但是,当我尝试安装mysqlclient时,出现了以下错误。

  Using cached mysqlclient-2.0.3.tar.gz (88 kB)
Building wheels for collected packages: mysqlclient
  Building wheel for mysqlclient (setup.py) ... error
  ERROR: Command errored out with exit status 1:
   command: /home/canggihmallmy/virtualenv/django_test/3.7/bin/python3.7_bin -u -c 'import sys, setuptools, tokenize; sys.argv[0] = '"'"'/tmp/pip-install-ct08p_k4/mysqlclient_5cd61bc8b4de40efb5731cfe082b4d65/setup.py'"'"'; __file__='"'"'/tmp/pip-install-ct08p_k4/mysqlclient_5cd61bc8b4de40efb5731cfe082b4d65/setup.py'"'"';f=getattr(tokenize, '"'"'open'"'"', open)(__file__);code=f.read().replace('"'"'\r\n'"'"', '"'"'\n'"'"');f.close();exec(compile(code, __file__, '"'"'exec'"'"'))'bdist_wheel -d /tmp/pip-wheel-scx4wswm
       cwd: /tmp/pip-install-ct08p_k4/mysqlclient_5cd61bc8b4de40efb5731cfe082b4d65/
  Complete output (43 lines):
  mysql_config --version
  ['10.0.38']
  mysql_config --libs
  ['-L/usr/lib64', '-lmysqlclient', '-lpthread', '-lz', '-lm', '-ldl', '-lssl', '-lcrypto']
  mysql_config --cflags
  ['-I/usr/include/mysql', '-I/usr/include/mysql/..']
  ext_options:
    library_dirs: ['/usr/lib64']
    libraries: ['mysqlclient', 'pthread', 'm', 'dl']
    extra_compile_args: ['-std=c99']
    extra_link_args: []
    include_dirs: ['/usr/include/mysql', '/usr/include/mysql/..']
    extra_objects: []
    define_macros: [('version_info', "(2,0,3,'final',0)"), ('__version__', '2.0.3')]
  /opt/alt/python37/lib64/python3.7/distutils/dist.py:274: UserWarning: Unknown distribution option: 'long_description_content_type'
    warnings.warn(msg)
  running bdist_wheel
  running build
  running build_py
  creating build
  creating build/lib.linux-x86_64-3.7
  creating build/lib.linux-x86_64-3.7/MySQLdb
  copying MySQLdb/__init__.py -> build/lib.linux-x86_64-3.7/MySQLdb
  copying MySQLdb/_exceptions.py -> build/lib.linux-x86_64-3.7/MySQLdb
  copying MySQLdb/connections.py -> build/lib.linux-x86_64-3.7/MySQLdb
  copying MySQLdb/converters.py -> build/lib.linux-x86_64-3.7/MySQLdb
  copying MySQLdb/cursors.py -> build/lib.linux-x86_64-3.7/MySQLdb
  copying MySQLdb/release.py -> build/lib.linux-x86_64-3.7/MySQLdb
  copying MySQLdb/times.py -> build/lib.linux-x86_64-3.7/MySQLdb
  creating build/lib.linux-x86_64-3.7/MySQLdb/constants
  copying MySQLdb/constants/__init__.py -> build/lib.linux-x86_64-3.7/MySQLdb/constants
  copying MySQLdb/constants/CLIENT.py -> build/lib.linux-x86_64-3.7/MySQLdb/constants
  copying MySQLdb/constants/CR.py -> build/lib.linux-x86_64-3.7/MySQLdb/constants
  copying MySQLdb/constants/ER.py -> build/lib.linux-x86_64-3.7/MySQLdb/constants
  copying MySQLdb/constants/FIELD_TYPE.py -> build/lib.linux-x86_64-3.7/MySQLdb/constants
  copying MySQLdb/constants/FLAG.py -> build/lib.linux-x86_64-3.7/MySQLdb/constants
  running build_ext
  building 'MySQLdb._mysql' extension
  creating build/temp.linux-x86_64-3.7
  creating build/temp.linux-x86_64-3.7/MySQLdb
  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=(2,0,3,'final',0) -D__version__=2.0.3 -I/usr/include/mysql -I/usr/include/mysql/.. -I/opt/alt/python37/include/python3.7m -c MySQLdb/_mysql.c -o build/temp.linux-x86_64-3.7/MySQLdb/_mysql.o -std=c99
  unable to execute 'gcc': Permission denied
  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: /home/canggihmallmy/virtualenv/django_test/3.7/bin/python3.7_bin -u -c 'import sys, setuptools, tokenize; sys.argv[0] = '"'"'/tmp/pip-install-ct08p_k4/mysqlclient_5cd61bc8b4de40efb5731cfe082b4d65/setup.py'"'"'; __file__='"'"'/tmp/pip-install-ct08p_k4/mysqlclient_5cd61bc8b4de40efb5731cfe082b4d65/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-1wxueu88/install-record.txt --single-version-externally-managed --compile --install-headers /home/canggihmallmy/virtualenv/django_test/3.7/include/site/python3.7/mysqlclient
         cwd: /tmp/pip-install-ct08p_k4/mysqlclient_5cd61bc8b4de40efb5731cfe082b4d65/
    Complete output (43 lines):
    mysql_config --version
    ['10.0.38']
    mysql_config --libs
    ['-L/usr/lib64', '-lmysqlclient', '-lpthread', '-lz', '-lm', '-ldl', '-lssl', '-lcrypto']
    mysql_config --cflags
    ['-I/usr/include/mysql', '-I/usr/include/mysql/..']
    ext_options:
      library_dirs: ['/usr/lib64']
      libraries: ['mysqlclient', 'pthread', 'm', 'dl']
      extra_compile_args: ['-std=c99']
      extra_link_args: []
      include_dirs: ['/usr/include/mysql', '/usr/include/mysql/..']
      extra_objects: []
      define_macros: [('version_info', "(2,0,3,'final',0)"), ('__version__', '2.0.3')]
    /opt/alt/python37/lib64/python3.7/distutils/dist.py:274: UserWarning: Unknown distribution option: 'long_description_content_type'
      warnings.warn(msg)
    running install
    running build
    running build_py
    creating build
    creating build/lib.linux-x86_64-3.7
    creating build/lib.linux-x86_64-3.7/MySQLdb
    copying MySQLdb/__init__.py -> build/lib.linux-x86_64-3.7/MySQLdb
    copying MySQLdb/_exceptions.py -> build/lib.linux-x86_64-3.7/MySQLdb
    copying MySQLdb/connections.py -> build/lib.linux-x86_64-3.7/MySQLdb
    copying MySQLdb/converters.py -> build/lib.linux-x86_64-3.7/MySQLdb
    copying MySQLdb/cursors.py -> build/lib.linux-x86_64-3.7/MySQLdb
    copying MySQLdb/release.py -> build/lib.linux-x86_64-3.7/MySQLdb
    copying MySQLdb/times.py -> build/lib.linux-x86_64-3.7/MySQLdb
    creating build/lib.linux-x86_64-3.7/MySQLdb/constants
    copying MySQLdb/constants/__init__.py -> build/lib.linux-x86_64-3.7/MySQLdb/constants
    copying MySQLdb/constants/CLIENT.py -> build/lib.linux-x86_64-3.7/MySQLdb/constants
    copying MySQLdb/constants/CR.py -> build/lib.linux-x86_64-3.7/MySQLdb/constants
    copying MySQLdb/constants/ER.py -> build/lib.linux-x86_64-3.7/MySQLdb/constants
    copying MySQLdb/constants/FIELD_TYPE.py -> build/lib.linux-x86_64-3.7/MySQLdb/constants
    copying MySQLdb/constants/FLAG.py -> build/lib.linux-x86_64-3.7/MySQLdb/constants
    running build_ext
    building 'MySQLdb._mysql' extension
    creating build/temp.linux-x86_64-3.7
    creating build/temp.linux-x86_64-3.7/MySQLdb
    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=(2,0,3,'final',0) -D__version__=2.0.3 -I/usr/include/mysql -I/usr/include/mysql/.. -I/opt/alt/python37/include/python3.7m -c MySQLdb/_mysql.c -o build/temp.linux-x86_64-3.7/MySQLdb/_mysql.o -std=c99
    unable to execute 'gcc': Permission denied
    error: command 'gcc' failed with exit status 1
    ----------------------------------------
ERROR: Command errored out with exit status 1: /home/canggihmallmy/virtualenv/django_test/3.7/bin/python3.7_bin -u -c 'import sys, setuptools, tokenize; sys.argv[0] = '"'"'/tmp/pip-install-ct08p_k4/mysqlclient_5cd61bc8b4de40efb5731cfe082b4d65/setup.py'"'"'; __file__='"'"'/tmp/pip-install-ct08p_k4/mysqlclient_5cd61bc8b4de40efb5731cfe082b4d65/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-1wxueu88/install-record.txt --single-version-externally-managed --compile --install-headers /home/canggihmallmy/virtualenv/django_test/3.7/include/site/python3.7/mysqlclient Check the logs for full command output. ```

I have tried some of the guide from , such as `yum install mysql` and sudo commands but to no avail.

Please help. 

嗨,我投票关闭这个问题,因为它不是关于编程的问题,而是与“sysadmin”有关的问题,所以应该放在Serverfault.com上。话虽如此,在您的托管计划中,似乎不允许使用gcc,这对于安装使用C库的Python模块是必需的。请向您的托管提供商确认是否提供解决方法。此外,您可能可以通过使用“仅限Python”的客户端来绕过此问题,请查看https://docs.djangoproject.com/en/3.1/ref/databases/#mysql-db-api-drivers。 - MichielB
注意 @MichielB。我会很快将其删除并转而参考serverfault.com。谢谢。 - codingnoob
@codingnoob 你解决了吗?我卡在这里了。 - Jafoor
1个回答

9

我曾经遇到同样的问题。经过多日的研究,我找到了这个解决方案。我希望这个方法也能解决你的问题。

终端

pip install mysql-connector-python

settings.py

DATABASES = {
    'default': {
        'NAME': 'DATEBASE_NAME',
        'ENGINE': 'mysql.connector.django',
        'USER': 'USERNAME',
        'PASSWORD': 'PASSWORD',
        'OPTIONS': {
          'autocommit': True,
        },
    }
}

终端

python manage.py makemigrations
python manage.py migrate

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接