我已经通过 XAMPP 运行 MySQL,并且还安装了 MySQLdb for python。但是,我想不出一种使用 XAMPP 的 MySQL for Python 的方法。每次执行 python manage.py runserver
都会显示错误:
..2.4c1-py2.7-win32.egg.tmp\MySQLdb\connections.py",第 187 行,在 __init__ 中
_mysql_exceptions.OperationalError: (2003, “无法连接到‘localhost’ (10061) 上的 MySQL 服务器”)
我是 Python 新手,这些是 settings.py
文件中的设置:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql', # Add 'postgresql_psycopg2', 'mysql', 'sqlite3' or 'oracle'.
'NAME': 'tester', # Or path to database file if using sqlite3.
'USER': 'root', # Not used with sqlite3.
'PASSWORD': 'password', # Not used with sqlite3.
'HOST': '', # Set to empty string for localhost. Not used with sqlite3.
'PORT': '', # Set to empty string for default. Not used with sqlite3.
}
}
我正在使用 Django
。如果我使用 SQLite3 而不是 MySQL,它工作正常。但我想使用 MySQL。
编辑#1
MySQL 正在使用端口:3306
。我如何让它们工作?
最佳答案
经过 5-6 小时的尝试,我终于让它工作了。
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql', # Add 'postgresql_psycopg2', 'mysql', 'sqlite3' or 'oracle'.
'NAME': 'tester', # Or path to database file if using sqlite3.
'USER': 'root', # Not used with sqlite3.
'PASSWORD': 'password', # Not used with sqlite3.
'HOST': '127.0.0.1', # Set to empty string for localhost. Not used with sqlite3.
'PORT': '3306', # Set to empty string for default. Not used with sqlite3.
}
}
在cmd中运行命令:python manage.py runserver
,打开网页。
关于python - 通过 Xampp 在 Windows 上用于 Python 的 MySQL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13969077/