【问题标题】:MySQL for Python on Windows via Xampp通过 Xampp 在 Windows 上使用 MySQL for Python
【发布时间】:2012-12-07 18:58:21
【问题描述】:

我通过 XAMPP 运行 MySQL,并且还安装了 MySQLdb for python。但是,我想不出一种将我的 XAMPP 的 MySQL 用于 Python 的方法。每次我执行python manage.py runserver 时都会显示错误:

..2.4c1-py2.7-win32.egg.tmp\MySQLdb\connections.py", line 187, in __init__ _mysql_exceptions.OperationalError: (2003, "Can't connect to MySQL server on 'localhost' (10061)")

我是 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。如何让它们工作?

【问题讨论】:

  • 这似乎是一个微不足道的问题,但在尝试连接到数据库之前,您是否检查过 MySQL 服务器实际上已启动并在您的系统上运行?
  • 是的。我检查了那个。当我执行python manage.py runserver 命令时,MySQL 正在运行。
  • 你误解了 Django 的文档。它从来没有说它在端口 3306 上查找 MySQL。端口 8000 是 Web 应用程序本身默认由开发服务器提供服务的地方。您应该注意到,在您上面发布的代码中,PORT 有一个(空)条目。你试过填写吗?
  • @DanielRoseman:是的,我填写了。我尝试了33068000。还是没用。

标签: python mysql django xampp mysql-python


【解决方案1】:

你能检查一下端口吗,因为mysql使用默认端口为3306(如果没有手动更改)

【讨论】:

  • 是的。我刚刚发现。 MySQL 正在使用端口3306。但是 Django 的文档使用了8000。我如何让它们工作?
【解决方案2】:

您很可能需要添加一个运行 mysql 的端口。这可能是 xampp 将 mysql 设置为标准 3306 以外的另一个端口。

你能在django之外连接mysql吗?

我猜你应该在 xampp 目录中找到一些 mysql.exe 文件。如果您通过终端导航到该目录,然后运行 ​​mysql.exe -uroot -p 。添加密码,很可能是空密码。

【讨论】:

  • 谢谢。我发现错误可能是由于端口。如何更改端口?MySQL 使用端口 3306。但 Django 的文档使用 8000。如何让它们工作?
  • 8000 用于 djangos wsgi。所以 localhost:8000 将指向 django。如果你 manage.py runserver 0.0.0.0 你得到没有端口的本地主机。如果你运行 manage.py runserver 0.0.0.0:9999 你会得到 localhost:9999
  • 所以我现在运行python manage.py runserver 0.0.0.0:3306,并导航到 localhost:3306-->仍然没有显示网页。
  • 3306 是数据库使用的标准端口。 0.0.0.0:XXXX 是网络服务器使用的端口。在上面的 DATABASES 中,您有一个名为 PORT 的键。您需要添加mysql运行的端口。试试我上面建议的通过 cms 打开数据库的建议。
  • 我已将 XAMPP MySQL 端口更改为 8000。 [它的工作]。现在再次尝试python manage.py runserverpython manage.py runserver 0.0.0.0:8000 [我都尝试过],仍然无法打开页面。
【解决方案3】:

经过 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,打开网页。

【讨论】:

    猜你喜欢
    • 2011-03-19
    • 1970-01-01
    • 2013-07-23
    • 2021-11-09
    • 2018-05-02
    相关资源
    最近更新 更多