从Django连接到MySQL

时间:2011-01-31 12:59:30

标签: python mysql django

我的第一个项目与django..i已经尝试了与sqlite的连接。但现在使用django的连接与mysql n m得到以下错误...

D:\project\wogma>manage.py syncdb
Traceback (most recent call last):
  File "D:\project\wogma\manage.py", line 11, in <module>
    execute_manager(settings)

  File "C:\Python26\lib\site-packages\django\core\management\__init__.py", line
340, in execute_manager
    utility.execute()

  File "C:\Python26\lib\site-packages\django\core\management\__init__.py", line
295, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)

  File "C:\Python26\lib\site-packages\django\core\management\base.py", line 192,
 in run_from_argv
    self.execute(*args, **options.__dict__)

  File "C:\Python26\lib\site-packages\django\core\management\base.py", line 218,
 in execute
    self.validate()

  File "C:\Python26\lib\site-packages\django\core\management\base.py", line 246,
 in validate
    num_errors = get_validation_errors(s, app)

  File "C:\Python26\lib\site-packages\django\core\management\validation.py", lin
e 65, in get_validation_errors
    connection.validation.validate_field(e, opts, f)

  File "C:\Python26\lib\site-packages\django\db\backends\mysql\validation.py", l
ine 8, in validate_field
    db_version = connection.get_server_version()

  File "C:\Python26\lib\site-packages\django\db\backends\mysql\base.py", line 27
7, in get_server_version
    self.cursor()

  File "C:\Python26\lib\site-packages\django\db\backends\__init__.py", line 56,
in cursor
    cursor = self._cursor(settings)

  File "C:\Python26\lib\site-packages\django\db\backends\mysql\base.py", line 26
2, in _cursor
    self.connection = Database.connect(**kwargs)

  File "C:\Python26\lib\site-packages\MySQLdb\__init__.py", line 81, in Connect
    return Connection(*args, **kwargs)
  File "C:\Python26\lib\site-packages\MySQLdb\connections.py", line 188, in __in
it__
    super(Connection, self).__init__(*args, **kwargs2)

_mysql_exceptions.OperationalError: (1049, "Unknown database 'd:/project/wogma/w
ogma'")

我在mysql中执行了如下命令..我已经使用create database创建了wogma数据库n我在mysql中授予了priviliges..in。 什么是prb ??

4 个答案:

答案 0 :(得分:7)

您正在尝试在文件路径中指定数据库(这适用于SQLite)。 MySQL需要一个数据库名称(例如'wogma')。你必须按照说明安装MySQL,创建一个新的用户和数据库等。看来你在Windows上,所以我不能帮助你。

答案 1 :(得分:4)

  1. 打开一个sql控制台并输入SHOW DATABASES;
  2. 如果显示wogma只是将其放入您的设置中(您使用的是django 1.1):

    DATABASE_ENGINE ='django.db.backends.mysql'

    DATABASE_NAME ='wogma'

    DATABASE_HOST ='127.0.0.1'

    DATABASE_PORT ='3306'

  3. 如果不显示,只需在此控制台CREATE DATABASE wogma ;中创建它,然后重新制作步骤。

答案 2 :(得分:3)

尝试使用此连接配置:

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.mysql',
        'NAME': 'MY_DATABASE_NAME',
        'USER': 'root',
        'PASSWORD': 'MY_PASSWORD',
    }
}

首先,在你的mysql中创建数据库。

答案 3 :(得分:0)

按照上述答案,以下代码将无效

    DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.mysql',
        'NAME': 'MY_DATABASE_NAME',
        'USER': 'root',
        'PASSWORD': 'MY_PASSWORD',
    }
 }

您将必须下载MySQL Connector。 阅读this帖子以在Django 2.1.x中设置MySQL数据库