django 如何处理这个错误(1049,“Unknown database '/users/ohyunjun/work/astral/mysql'”)

yftpprvb  于 2023-07-01  发布在  Go
关注(0)|答案(4)|浏览(226)

在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'")
piztneat

piztneat1#

'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',
    }
}
k4emjkb1

k4emjkb12#

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

3pmvbmvn

3pmvbmvn3#

使用Docker启动MySQL
我更改了数据库名称,然后有同样的错误。
移除docker的卷缓存后,一切正常!

docker volume --help
docker volume ls
docker volume  rm xx yy
mfpqipee

mfpqipee4#

首先,您需要在Mysql数据库中创建数据库,并settings.py在mysql connectivuty代码中写入www.example.com中的名称
settings.py
DATABASES = { 'default': { 'ENGINE': 'django.db.backends.mysql', 'NAME': 'name_of database', 'USER': 'root', 'PASSWORD': 'sp153426', 'HOST': '127.0.0.1', 'PORT': '3306', } }
这里的name_of_database -是你的数据库中已经需要的
使用SQL查询创建

`CREATE DATABASE name_of Database;`

相关问题