【问题标题】:Connecting to MySQL database in django using workbench使用工作台连接到 django 中的 MySQL 数据库
【发布时间】:2014-05-05 21:25:26
【问题描述】:

我有一个 django 项目连接到 MySQL 数据库,如下所示:

settings.py

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.mysql',
        'NAME': 'my_db',
        'USER': 'username',
        'PASSWORD': 'mypassword',
        'HOST': '',
        'PORT': '',
    }
}

在运行 python manage.py syncdb 时,它会创建数据库,在 runserver 上,应用程序运行良好,我可以使用管理面板正确地将数据输入模型。

但是,在尝试使用 MySQL Workbench 连接到数据库时,它给了我 Can't connect to MySQL server on '127.0.0.1' (111) 错误。

我创建的数据库如下:

Grant all on my_db.* to 'username'@'localhost' identified by 'mypassword';
flush privileges;

为什么即使服务器运行正常,Workbench 也会向我显示该错误?提前致谢!

编辑:使用了pythong这个词。

【问题讨论】:

  • 您是否尝试在服务器运行时通过 Workbench 连接数据库?也许有某种锁定机制。
  • @noisy,事情是,我可以在服务器运行时使用 localhost/phpmyadmin 连接到它。同样的事情应该在工作台上工作,但它给了我这个问题。不知道为什么。

标签: python mysql database django


【解决方案1】:

我有类似的问题,但通过将 #port 和 #host 更新到配置文件中得到了解决。

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.mysql', # Add 'postgresql_psycopg2', 'mysql', 'sqlite3' or 'oracle'.
        'NAME': 'vres',                      # Or path to database file if using sqlite3.
        # The following settings are not used with sqlite3:
        'USER': 'root',
        'PASSWORD': 'root',
        'HOST': '127.0.0.1',                      # Empty for localhost through domain sockets or '127.0.0.1' for localhost through TCP.
        'PORT': '3306',                      # Set to empty string for default.
    }
}

【讨论】:

  • 能否请您详细说明您的答案,添加更多关于您提供的解决方案的描述?
  • 查看上面提到的数据库设置,很明显没有定义 HOST: 和 PORT 参数的值,我建议您尝试显式添加 mysql 连接的默认主机 IP 和默认端口地址。请首先确保您的系统没有运行任何其他 mysql 服务,例如phymyadmin/wamp/xamp
【解决方案2】:

可能是您的 MySQL 服务器只监听 localhost。 在您的 my.cnf 文件中,注释掉 bind-address

#bind-address = 127.0.0.1

重启你的 MySQL 服务器,看看你是否可以连接到 Workbench。

【讨论】:

    猜你喜欢
    • 2015-05-21
    • 2016-07-12
    • 1970-01-01
    • 2018-03-17
    • 1970-01-01
    • 2015-03-10
    • 2015-05-11
    • 2011-12-13
    • 2016-05-24
    相关资源
    最近更新 更多