如何处理这个错误(1049,“未知数据库'/users/ohyunjun/work/astral/mysql'”)

Posted

技术标签:

【中文标题】如何处理这个错误(1049,“未知数据库\'/users/ohyunjun/work/astral/mysql\'”)【英文标题】:How to deal with this ERROR (1049, "Unknown database '/users/ohyunjun/work/astral/mysql'")如何处理这个错误(1049,“未知数据库'/users/ohyunjun/work/astral/mysql'”) 【发布时间】:2014-08-19 03:57:24 【问题描述】:

在 Django settings.py 中,我以这种方式设置数据库选项

DATABASES = 
    'default': 
        'ENGINE': 'django.db.backends.mysql',
        'NAME': os.path.join(BASE_DIR, 'mysql'),
        'USER': 'root',
        'PASSWORD': 'sp153426',
        'HOST': '127.0.0.1',
        'PORT': '3306',
    

我执行了这个命令

python manage.py syncdb

但因此错误而失败

  File "manage.py", line 10, in <module>
    execute_from_command_line(sys.argv)
  File "/Library/Python/2.7/site-packages/django/core/management/__init__.py", line 399, in execute_from_command_line
    utility.execute()
  File "/Library/Python/2.7/site-packages/django/core/management/__init__.py", line 392, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/Library/Python/2.7/site-packages/django/core/management/base.py", line 242, in run_from_argv
    self.execute(*args, **options.__dict__)
  File "/Library/Python/2.7/site-packages/django/core/management/base.py", line 285, in execute
    output = self.handle(*args, **options)
  File "/Library/Python/2.7/site-packages/django/core/management/base.py", line 415, in handle
    return self.handle_noargs(**options)
  File "/Library/Python/2.7/site-packages/django/core/management/commands/syncdb.py", line 57, in handle_noargs
    cursor = connection.cursor()
  File "/Library/Python/2.7/site-packages/django/db/backends/__init__.py", line 160, in cursor
    cursor = self.make_debug_cursor(self._cursor())
  File "/Library/Python/2.7/site-packages/django/db/backends/__init__.py", line 132, in _cursor
    self.ensure_connection()
  File "/Library/Python/2.7/site-packages/django/db/backends/__init__.py", line 127, in ensure_connection
    self.connect()
  File "/Library/Python/2.7/site-packages/django/db/utils.py", line 99, in __exit__
    six.reraise(dj_exc_type, dj_exc_value, traceback)
  File "/Library/Python/2.7/site-packages/django/db/backends/__init__.py", line 127, in ensure_connection
    self.connect()
  File "/Library/Python/2.7/site-packages/django/db/backends/__init__.py", line 115, in connect
    self.connection = self.get_new_connection(conn_params)
  File "/Library/Python/2.7/site-packages/django/db/backends/mysql/base.py", line 435, in get_new_connection
    conn = Database.connect(**conn_params)
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/MySQL_python-1.2.4b4-py2.7-macosx-10.6-intel.egg/MySQLdb/__init__.py", line 81, in Connect
    return Connection(*args, **kwargs)
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/MySQL_python-1.2.4b4-py2.7-macosx-10.6-intel.egg/MySQLdb/connections.py", line 187, in __init__
    super(Connection, self).__init__(*args, **kwargs2)
django.db.utils.OperationalError: (1049, "Unknown database '/users/ohyunjun/work/astral/mysql'")

【问题讨论】:

name 是数据库名称。不是绝对路径。 【参考方案1】:

嘿伙计,它只是要求数据库应该已经在 MySQL 中创建。

【讨论】:

请考虑更新您的答案。如果您从问题的settings.py 文件中读取代码,您可以看到数据库名称是完整的文件系统路径。这显然是个问题。如果您想扩展到其他原因,您会看到 1049 错误,请写一个更好的解释。【参考方案2】:

'NAME' 是您的数据库的名称。使用 MySQL,您也需要手动创建数据库。比方说,如果你运行:

$ mysql -u root -p

mysql> CREATE DATABASE mydb;
Query OK, 1 row affected (0.02 sec)

你的配置应该是:

DATABASES = 
    'default': 
        'ENGINE': 'django.db.backends.mysql',
        'NAME': 'mydb',
        'USER': 'root',
        'PASSWORD': 'sp153426',
        'HOST': '127.0.0.1',
        'PORT': '3306',
    

【讨论】:

如果我已经有一个需要加载的数据库 (.sql) 怎么办?我不想创建一个新的空数据库。

以上是关于如何处理这个错误(1049,“未知数据库'/users/ohyunjun/work/astral/mysql'”)的主要内容,如果未能解决你的问题,请参考以下文章

ConnectionError:如何处理这个错误?

从 watchkit 请求数据时如何处理这个错误

Spring test integration not run 我如何处理这个错误?

Codeigniter:如何处理数据库错误?

我可以/应该如何处理这个 git gc 错误? (rm:无法取消链接包权限被拒绝)

如何处理网络请求失败错误?