在cPanel上的django项目中安装mysqlclient时出错

问题描述 投票:0回答:2

这几天我一直在努力解决这个问题。我正在尝试将我的 Django 项目放入 cPanel 上的 venv 中并安装 mysqlclient。因此,在 Cpanel 上设置 Python(版本 = 3.7.8)后,我使用

pip install django
pip install mysqlclient
从终端安装了 Django 版本 3.1.7 和 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. 
python gcc cpanel mysql-connector
2个回答
10
投票

我也有同样的问题。经过几天的研究,我找到了这个解决方案。我希望这也能解决您的问题。

终端

pip install mysql-connector-python

设置.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

0
投票

即使

mysql-connector-python
套餐没有解决您的任何问题,也可以使用以下步骤解决:

首先,卸载

mysqlclient
mysql-connector-python
(如果存在),使用:

pip uninstall mysqlclient
pip uninstall mysl-connector-python

现在安装

PyMySQL
:

pip install PyMySQL

安装成功后,进入

settings.py
文件,首先导入
pymysql
,然后将其初始化为
MySQLdb
文件顶部的
settings.py
,如下:

...
import pymysql
...

pymysql.install_as_MySQLdb()

现在,假设您已在同一个

settings.py
文件中创建了数据库、创建了用户并且该用户具有连接数据库所需的权限凭据:

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.mysql',
        'NAME': 'your_database_name',
        'USER': 'your_database_username',
        'PASSWORD': 'your_database_access_password',
        'HOST': 'localhost', # for database in the same server
        'PORT': 3306, # default database port
    }
} 

现在,您也许可以将您的项目与数据库连接起来。但您必须确保数据库正在运行并且凭据正确。 现在您可以执行数据库操作,例如:

python manage.py makemigrations
python manage.py migrate

注意,我认为您不再需要此解决方案,但像我这样的人可能会有所帮助。

© www.soinside.com 2019 - 2024. All rights reserved.