python - 通过 Xampp 在 Windows 上用于 Python 的 MySQL

标签 python mysql django xampp mysql-python

我已经通过 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/

相关文章:

Python - 对象没有属性错误

javascript - 使用Ajax插入数据但插入需要时间

MySQL 合并多行并维护列名

php - SELECT QUERY 在一定时间内获得员工的帮助

python - Django 模板中的动态表单字段生成

python - Google App Engine 上的 Django 和 GeoSpatial 查询 - 如何克服 'one inequality per query' 问题?

python - 如何仅从Keras提供的MNIST数据集中选择特定数字?

python - 词典不添加新词条

Python Tkinter 使用 PIL 将 Canvas 保存为图像

python - 为什么我的 Django 应用程序模型的字段值没有改变?