django mysql连接问题

Posted

技术标签:

【中文标题】django mysql连接问题【英文标题】:django mysql connection issue 【发布时间】:2015-09-19 03:32:20 【问题描述】:

我正在尝试将 Django 与 mysql 数据库连接,因此我将 settings.py 文件更改如下:

DATABASES = 
    'default': 
        'ENGINE': 'django.db.backends.mysql',
        'NAME': 'mysite',
        'USER': 'root',
        'PASSWORD': '1234',
        'HOST': 'localhost',   
        'PORT': '3306',
    

当我尝试运行服务器时,我得到了这个:

![在此处输入图像描述][1] 所以我flowed了getting-error-loading-mysqldb-module-no-module-named-mysqldb的帖子,现在当我这样做时:pip install MySql=python我收到了这个错误:

$ pip install MySQL-python
Collecting MySQL-python
  Using cached MySQL-python-1.2.5.zip
Building wheels for collected packages: MySQL-python
  Running setup.py bdist_wheel for MySQL-python
  Complete output from command c:\Python27\python.exe -c "import setuptools;__file__='c:\\users\\rafa\\appdata\\local\\temp\\pip-build-sfr5rp\\MySQL-python\\setup.py';exec(compile(open(__file__).read(
).replace('\r\n', '\n'), __file__, 'exec'))" bdist_wheel -d c:\users\rafa\appdata\local\temp\tmpxuok6wpip-wheel-:
  running bdist_wheel
  running build
  running build_py
  creating build
  creating build\lib.win32-2.7
  copying _mysql_exceptions.py -> build\lib.win32-2.7
  creating build\lib.win32-2.7\MySQLdb
  copying MySQLdb\__init__.py -> build\lib.win32-2.7\MySQLdb
  copying MySQLdb\converters.py -> build\lib.win32-2.7\MySQLdb
  copying MySQLdb\connections.py -> build\lib.win32-2.7\MySQLdb
  copying MySQLdb\cursors.py -> build\lib.win32-2.7\MySQLdb
  copying MySQLdb\release.py -> build\lib.win32-2.7\MySQLdb
  copying MySQLdb\times.py -> build\lib.win32-2.7\MySQLdb
  creating build\lib.win32-2.7\MySQLdb\constants
  copying MySQLdb\constants\__init__.py -> build\lib.win32-2.7\MySQLdb\constants
  copying MySQLdb\constants\CR.py -> build\lib.win32-2.7\MySQLdb\constants
  copying MySQLdb\constants\FIELD_TYPE.py -> build\lib.win32-2.7\MySQLdb\constants
  copying MySQLdb\constants\ER.py -> build\lib.win32-2.7\MySQLdb\constants
  copying MySQLdb\constants\FLAG.py -> build\lib.win32-2.7\MySQLdb\constants
  copying MySQLdb\constants\REFRESH.py -> build\lib.win32-2.7\MySQLdb\constants
  copying MySQLdb\constants\CLIENT.py -> build\lib.win32-2.7\MySQLdb\constants
  running build_ext
  building '_mysql' extension
  creating build\temp.win32-2.7
  creating build\temp.win32-2.7\Release
  C:\Users\Rafa\AppData\Local\Programs\Common\Microsoft\Visual C++ for Python\9.0\VC\Bin\cl.exe /c /nologo /Ox /MD /W3 /GS- /DNDEBUG -Dversion_info=(1,2,5,'final',1) -D__version__=1.2.5 "-IC:\Program
Files (x86)\MySQL\MySQL Connector C 6.0.2\include" -Ic:\Python27\include -Ic:\Python27\PC /Tc_mysql.c /Fobuild\temp.win32-2.7\Release\_mysql.obj /Zl
  _mysql.c
  _mysql.c(42) : fatal error C1083: Cannot open include file: 'config-win.h': No such file or directory
  error: command 'C:\\Users\\Rafa\\AppData\\Local\\Programs\\Common\\Microsoft\\Visual C++ for Python\\9.0\\VC\\Bin\\cl.exe' failed with exit status 2

  ----------------------------------------
←[31m  Failed building wheel for MySQL-python←[0m
Failed to build MySQL-python
Installing collected packages: MySQL-python
  Running setup.py install for MySQL-python
    Complete output from command c:\Python27\python.exe -c "import setuptools, tokenize;__file__='c:\\users\\rafa\\appdata\\local\\temp\\pip-build-sfr5rp\\MySQL-python\\setup.py';exec(compile(getattr(
tokenize, 'open', open)(__file__).read().replace('\r\n', '\n'), __file__, 'exec'))" install --record c:\users\rafa\appdata\local\temp\pip-p1pfvy-record\install-record.txt --single-version-externally-m
anaged --compile:
    running install
    running build
    running build_py
    copying MySQLdb\release.py -> build\lib.win32-2.7\MySQLdb
    running build_ext
    building '_mysql' extension
    C:\Users\Rafa\AppData\Local\Programs\Common\Microsoft\Visual C++ for Python\9.0\VC\Bin\cl.exe /c /nologo /Ox /MD /W3 /GS- /DNDEBUG -Dversion_info=(1,2,5,'final',1) -D__version__=1.2.5 "-IC:\Progra
m Files (x86)\MySQL\MySQL Connector C 6.0.2\include" -Ic:\Python27\include -Ic:\Python27\PC /Tc_mysql.c /Fobuild\temp.win32-2.7\Release\_mysql.obj /Zl
    _mysql.c
    _mysql.c(42) : fatal error C1083: Cannot open include file: 'config-win.h': No such file or directory
    error: command 'C:\\Users\\Rafa\\AppData\\Local\\Programs\\Common\\Microsoft\\Visual C++ for Python\\9.0\\VC\\Bin\\cl.exe' failed with exit status 2

    ----------------------------------------
←[31mCommand "c:\Python27\python.exe -c "import setuptools, tokenize;__file__='c:\\users\\rafa\\appdata\\local\\temp\\pip-build-sfr5rp\\MySQL-python\\setup.py';exec(compile(getattr(tokenize, 'open', o
pen)(__file__).read().replace('\r\n', '\n'), __file__, 'exec'))" install --record c:\users\rafa\appdata\local\temp\pip-p1pfvy-record\install-record.txt --single-version-externally-managed --compile" f
ailed with error code 1 in c:\users\rafa\appdata\local\temp\pip-build-sfr5rp\MySQL-python←[0m

如果有人可以帮助我,我很感激。

【问题讨论】:

将您的错误 text 复制并粘贴到问题中,而不是屏幕截图。这样更容易阅读和帮助。 ***.com/questions/1972259/… 请检查我的编辑@Andy 【参考方案1】:

如果您使用的是 Windows,我建议您安装 Mysql-python 二进制文件 (.exe),而不是使用 pip 编译源文件,或者使用与您的平台和架构相匹配的***。

查看链接: https://pypi.python.org/pypi/MySQL-python/1.2.5

【讨论】:

或者可能将 python-dev 安装到您的 mingwin 环境中。【参考方案2】:

install from this link

然后输入

python manage.py sqlmigrate application_name 0001 python manage.py runserver

【讨论】:

【参考方案3】:

根权限

(env)> sudo pip install mysql-python

作为根

(env)> pip install mysql-python

【讨论】:

以上是关于django mysql连接问题的主要内容,如果未能解决你的问题,请参考以下文章

Django:池化 MySQL 数据库连接

如何在django中连接mysql [重复]

django 和 mysql 啥是“最大连接数”?

mysql服务开着,但是django却连不上

Django连接数据mysql

python测试开发django-10.django连接mysql