如何使用 Windows 将我的 Django 项目连接到 MySql?

Posted

技术标签:

【中文标题】如何使用 Windows 将我的 Django 项目连接到 MySql?【英文标题】:How do I connect my Django project to MySql using Windows? 【发布时间】:2020-10-31 13:28:47 【问题描述】:

我正在尝试将我的 Django 项目连接到 mysql 数据库,但遇到了一些问题。我尝试运行 Xampp 控制面板并在那里启动服务,但收到此错误消息...

18:55:35  [mysql]   Attempting to start MySQL app...
18:55:35  [mysql]   Status change detected: running
18:55:39  [mysql]   Status change detected: stopped
18:55:39  [mysql]   Error: MySQL shutdown unexpectedly.
18:55:39  [mysql]   This may be due to a blocked port, missing dependencies,
18:55:39  [mysql]   improper privileges, a crash, or a shutdown by another method.
18:55:39  [mysql]   Press the Logs button to view error logs and check
18:55:39  [mysql]   the Windows Event Viewer for more clues
18:55:39  [mysql]   If you need more help, copy and post this
18:55:39  [mysql]   entire log window on the forums
19:15:09  [mysql]   Problem detected!
19:15:09  [mysql]   Port 3306 in use by "Unable to open process"!
19:15:09  [mysql]   MySQL WILL NOT start without the configured ports free!
19:15:09  [mysql]   You need to uninstall/disable/reconfigure the blocking application
19:15:09  [mysql]   or reconfigure MySQL and the Control Panel to listen on a different port

我也尝试通过终端安装mysqlclient,但是这导致了以下巨大错误...

Collecting mysqlclient
  Using cached mysqlclient-2.0.1.tar.gz (87 kB)
Building wheels for collected packages: mysqlclient
  Building wheel for mysqlclient (setup.py) ... error
  ERROR: Command errored out with exit status 1:
   command: 'c:\users\administrator\appdata\local\programs\python\python38-32\python.exe' -u -c          
'import sys, setuptools, tokenize; sys.argv[0] = '"'"'C:\\Users\\Administrator\\AppData\\Local
\\Temp    \\pip-install-g0c19c79\\mysqlclient\\setup.py'"'"'; __file__='"'"'C:\\Users
\\Administrator\\AppData\\Local\\Temp\\pip-install-g0c19c79\\mysqlclient
\\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 'C:\Users\Administrator\AppData\Local\Temp\pip-wheel-
er48j2t7'
   cwd: C:\Users\Administrator\AppData\Local\Temp\pip-install-g0c19c79\mysqlclient\
  Complete output (29 lines):
  running bdist_wheel
  running build
  running build_py
  creating build
  creating build\lib.win32-3.8
  creating build\lib.win32-3.8\MySQLdb
  copying MySQLdb\__init__.py -> build\lib.win32-3.8\MySQLdb
  copying MySQLdb\_exceptions.py -> build\lib.win32-3.8\MySQLdb
  copying MySQLdb\connections.py -> build\lib.win32-3.8\MySQLdb
  copying MySQLdb\converters.py -> build\lib.win32-3.8\MySQLdb
  copying MySQLdb\cursors.py -> build\lib.win32-3.8\MySQLdb
  copying MySQLdb\release.py -> build\lib.win32-3.8\MySQLdb
  copying MySQLdb\times.py -> build\lib.win32-3.8\MySQLdb
  creating build\lib.win32-3.8\MySQLdb\constants
  copying MySQLdb\constants\__init__.py -> build\lib.win32-3.8\MySQLdb\constants
  copying MySQLdb\constants\CLIENT.py -> build\lib.win32-3.8\MySQLdb\constants
  copying MySQLdb\constants\CR.py -> build\lib.win32-3.8\MySQLdb\constants
  copying MySQLdb\constants\ER.py -> build\lib.win32-3.8\MySQLdb\constants
  copying MySQLdb\constants\FIELD_TYPE.py -> build\lib.win32-3.8\MySQLdb\constants
  copying MySQLdb\constants\FLAG.py -> build\lib.win32-3.8\MySQLdb\constants
  running build_ext
  building 'MySQLdb._mysql' extension
  creating build\temp.win32-3.8
  creating build\temp.win32-3.8\Release
  creating build\temp.win32-3.8\Release\MySQLdb
  C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.24.28314
\bin\HostX86\x86\cl.exe /c /nologo /Ox /W3 /GL /DNDEBUG /MD -Dversion_info=(2,0,1,'final',0) 
-D__version__=2.0.1 "-IC:\Program Files (x86)\MySQL\MySQL Connector C 6.1\include\mariadb" 
-Ic:\users\administrator\appdata\local\programs\python\python38-32\include -Ic:\users
\administrator\appdata\local\programs\python\python38-32\include "-IC:\Program Files 
(x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.24.28314\include" /TcMySQLdb
/_mysql.c /Fobuild\temp.win32-3.8\Release\MySQLdb/_mysql.obj /Zl /D_CRT_SECURE_NO_WARNINGS
  _mysql.c
  MySQLdb/_mysql.c(29): fatal error C1083: Cannot open include file: 'mysql.h': No such file or 
directory
  error: command 'C:\\Program Files (x86)\\Microsoft Visual Studio\\2019\\Community\\VC\\Tools
\\MSVC\\14.24.28314\\bin\\HostX86\\x86\\cl.exe' failed with exit status 2

  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: 'c:\users\administrator\appdata\local\programs\python\python38-32\python.exe' -u 
-c 'import sys, setuptools, tokenize; sys.argv[0] = '"'"'C:\\Users\\Administrator\\AppData
\\Local\\Temp\\pip-install-g0c19c79\\mysqlclient\\setup.py'"'"'; __file__='"'"'C:\\Users
\\Administrator\\AppData\\Local\\Temp\\pip-install-g0c19c79\\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 'C:\Users\Administrator\AppData\Local\Temp\pip-
record-as59yxwj\install-record.txt' --single-version-externally-managed --compile --install-
headers 'c:\users\administrator\appdata\local\programs\python\python38-32\Include\mysqlclient'
         cwd: C:\Users\Administrator\AppData\Local\Temp\pip-install-g0c19c79\mysqlclient\
    Complete output (29 lines):
    running install
    running build
    running build_py
    creating build
    creating build\lib.win32-3.8
    creating build\lib.win32-3.8\MySQLdb
    copying MySQLdb\__init__.py -> build\lib.win32-3.8\MySQLdb
    copying MySQLdb\_exceptions.py -> build\lib.win32-3.8\MySQLdb
    copying MySQLdb\connections.py -> build\lib.win32-3.8\MySQLdb
    copying MySQLdb\converters.py -> build\lib.win32-3.8\MySQLdb
    copying MySQLdb\cursors.py -> build\lib.win32-3.8\MySQLdb
    copying MySQLdb\release.py -> build\lib.win32-3.8\MySQLdb
    copying MySQLdb\times.py -> build\lib.win32-3.8\MySQLdb
    creating build\lib.win32-3.8\MySQLdb\constants
    copying MySQLdb\constants\__init__.py -> build\lib.win32-3.8\MySQLdb\constants
    copying MySQLdb\constants\CLIENT.py -> build\lib.win32-3.8\MySQLdb\constants
    copying MySQLdb\constants\CR.py -> build\lib.win32-3.8\MySQLdb\constants
    copying MySQLdb\constants\ER.py -> build\lib.win32-3.8\MySQLdb\constants
    copying MySQLdb\constants\FIELD_TYPE.py -> build\lib.win32-3.8\MySQLdb\constants
    copying MySQLdb\constants\FLAG.py -> build\lib.win32-3.8\MySQLdb\constants
    running build_ext
    building 'MySQLdb._mysql' extension
    creating build\temp.win32-3.8
    creating build\temp.win32-3.8\Release
    creating build\temp.win32-3.8\Release\MySQLdb
    C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.24.28314
\bin\HostX86\x86\cl.exe /c /nologo /Ox /W3 /GL /DNDEBUG /MD -Dversion_info=(2,0,1,'final',0) 
-D__version__=2.0.1 "-IC:\Program Files (x86)\MySQL\MySQL Connector C 6.1\include\mariadb" 
-Ic:\users\administrator\appdata\local\programs\python\python38-32\include -Ic:\users
\administrator\appdata\local\programs\python\python38-32\include "-IC:\Program Files 
(x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.24.28314\include" /TcMySQLdb
/_mysql.c /Fobuild\temp.win32-3.8\Release\MySQLdb/_mysql.obj /Zl /D_CRT_SECURE_NO_WARNINGS
    _mysql.c
    MySQLdb/_mysql.c(29): fatal error C1083: Cannot open include file: 'mysql.h': No such file 
or directory
    error: command 'C:\\Program Files (x86)\\Microsoft Visual Studio\\2019\\Community\\VC\\Tools
\\MSVC\\14.24.28314\\bin\\HostX86\\x86\\cl.exe' failed with exit status 2
----------------------------------------
ERROR: Command errored out with exit status 1: 'c:\users\administrator\appdata\local\programs
\python\python38-32\python.exe' -u -c 'import sys, setuptools, tokenize; sys.argv[0] = 
'"'"'C:\\Users\\Administrator\\AppData\\Local\\Temp\\pip-install-g0c19c79\\mysqlclient
\\setup.py'"'"'; __file__='"'"'C:\\Users\\Administrator\\AppData\\Local\\Temp\\pip-install-
g0c19c79\\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 'C:\Users\Administrator\AppData\Local\Temp\pip-
record-as59yxwj\install-record.txt' --single-version-externally-managed --compile --install-
headers 'c:\users\administrator\appdata\local\programs\python\python38-32\Include\mysqlclient' 
Check the logs for full command output.

【问题讨论】:

您可以添加您在settings.py文件中设置的数据库设置吗? DATABASES = 'default': 'ENGINE': 'django.db.backends.sqlite3', 'NAME': os.path.join(BASE_DIR, 'db.sqlite3'), 【参考方案1】:

    您的 MySQL 服务器无法启动,因为 3306 端口被另一个应用程序使用。所以请在启动 MySQL 服务之前检查 3306 端口并杀死使用该端口的进程。

    能否请您下载.whl格式的mysqlclient(预建版)并使用pip安装?

pip install mysqlclient-1.3.13-cp36-cp36m-win32.whl

【讨论】:

感谢您的回复。我能够打开 mysql 工作台并使用端口 3306 开始连接,所以我认为这不是问题所在。我刚刚尝试使用 .whl 进行安装时收到以下错误警告:要求“mysqlclient-1.3.13-cp36-cp36m-win32.whl”看起来像一个文件名,但该文件不存在错误:mysqlclient-1.3。 13-cp36-cp36m-win32.whl 不是这个平台支持的***。 您需要通过互联网下载。请检查此链接。 pypi.org/project/mysqlclient/1.3.12/#files 感谢您的链接。我收到一条警告说 windows 无法打开这种类型的文件... 请将该文件粘贴到您的项目根目录下并运行 pip 命令。该文件不是windows上的可执行文件,因此无法打开或运行。 是的,我确实尝试过。我收到此错误消息:警告:要求“mysqlclient-1.3.13-cp36-cp36m-win32.whl”看起来像一个文件名,但该文件不存在错误:mysqlclient-1.3.13-cp36-cp36m-win32.whl不是此平台上支持的***。【参考方案2】:

您的问题是您的settings.py 中的DATABASE,您需要安装mysqlclient,然后正确设置DATABASE。看看它可能是什么样子:

DATABASES = 
    'default': 
        'ENGINE': 'django.db.backends.mysql',
        'NAME': 'someappname',
        'USER': 'username',
        'PASSWORD': 'yourpassword',
        'HOST': 'yourhost',
        'PORT': 'yourport',        
    

【讨论】:

感谢您的回复,但是我还没有能够成功安装mysqlclient。当我尝试这样做时,我原来的帖子中显示的巨大错误发生了

以上是关于如何使用 Windows 将我的 Django 项目连接到 MySql?的主要内容,如果未能解决你的问题,请参考以下文章

如何将我的 Vue js SPA 部署到我的 Django 服务器中?

如何将我的 Django 项目中的 jupyter-lab 连接到 Django 的模型?

如何在 django 中链接我的 css、js 和图像文件链接

如何将我的 docker Django 实例连接到我的 docker mysql 实例?

如何让 Ivy 将我的依赖项的依赖项捆绑到他们自己的 jar 中?

使用 Django 推送通知