Python Django 使用pymysql报错 django.db.utils.OperationalError: (1046, ‘No database selected‘)

Python Django 使用pymysql报错 django.db.utils.OperationalError: (1046, ‘No database selected’)

1.问题背景

在构建Django+pymysql+mysql服务时,遇到服务启动报错

django.db.utils.OperationalError: (1046, 'No database selected')

2.问题描述

settings 配置:

DATABASES = {

  'default': {
      'ENGINE': 'django.db.backends.mysql',

      'Name': parser.get("database", 'mysql_database'),

      'USER': parser.get("database", 'mysql_username'),  # usrname

      'PASSWORD': parser.get("database", 'mysql_password'),  # pass word

      'HOST': parser.get("database", 'mysql_host'),  # ip
      # 'HOST': 'localhost',  # ip?

      'PORT': parser.get("database", 'mysql_port'),  # port
      'CONN_MAX_AGE': 60
  },
  # 'default': {
  #     'ENGINE': 'django.db.backends.sqlite3',
  #     'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
  # }
}

启动控制台输出:

System check identified no issues (0 silenced).
Traceback (most recent call last):
  File "D:\work_space_for_python\calculate_for_GridAgent\venv\lib\site-packages\django\db\backends\utils.py", line 82, in _execute
    return self.cursor.execute(sql)
  File "D:\work_space_for_python\calculate_for_GridAgent\venv\lib\site-packages\django\db\backends\mysql\base.py", line 73, in execute
    return self.cursor.execute(query, args)
  File "D:\work_space_for_python\calculate_for_GridAgent\venv\lib\site-packages\pymysql\cursors.py", line 153, in execute
    result = self._query(query)
  File "D:\work_space_for_python\calculate_for_GridAgent\venv\lib\site-packages\pymysql\cursors.py", line 322, in _query
    conn.query(q)
  File "D:\work_space_for_python\calculate_for_GridAgent\venv\lib\site-packages\pymysql\connections.py", line 558, in query
    self._affected_rows = self._read_query_result(unbuffered=unbuffered)
  File "D:\work_space_for_python\calculate_for_GridAgent\venv\lib\site-packages\pymysql\connections.py", line 822, in _read_query_result
    result.read()
  File "D:\work_space_for_python\calculate_for_GridAgent\venv\lib\site-packages\pymysql\connections.py", line 1200, in read
    first_packet = self.connection._read_packet()
  File "D:\work_space_for_python\calculate_for_GridAgent\venv\lib\site-packages\pymysql\connections.py", line 772, in _read_packet
    packet.raise_for_error()
  File "D:\work_space_for_python\calculate_for_GridAgent\venv\lib\site-packages\pymysql\protocol.py", line 221, in raise_for_error
    err.raise_mysql_exception(self._data)
  File "D:\work_space_for_python\calculate_for_GridAgent\venv\lib\site-packages\pymysql\err.py", line 143, in raise_mysql_exception
    raise errorclass(errno, errval)
pymysql.err.OperationalError: (1046, 'No database selected')
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
  File "D:\work_soft\python37\lib\threading.py", line 870, in run
    self._target(*self._args, **self._kwargs)
  File "D:\work_space_for_python\calculate_for_GridAgent\venv\lib\site-packages\django\utils\autoreload.py", line 64, in wrapper
    fn(*args, **kwargs)
  File "D:\work_space_for_python\calculate_for_GridAgent\venv\lib\site-packages\django\core\management\commands\runserver.py", line 121, in inner_run
    self.check_migrations()
  File "D:\work_space_for_python\calculate_for_GridAgent\venv\lib\site-packages\django\core\management\base.py", line 486, in check_migrations
    executor = MigrationExecutor(connections[DEFAULT_DB_ALIAS])
  File "D:\work_space_for_python\calculate_for_GridAgent\venv\lib\site-packages\django\db\migrations\executor.py", line 18, in __init__
    self.loader = MigrationLoader(self.connection)
  File "D:\work_space_for_python\calculate_for_GridAgent\venv\lib\site-packages\django\db\migrations\loader.py", line 53, in __init__
    self.build_graph()
  File "D:\work_space_for_python\calculate_for_GridAgent\venv\lib\site-packages\django\db\migrations\loader.py", line 220, in build_graph
    self.applied_migrations = recorder.applied_migrations()
  File "D:\work_space_for_python\calculate_for_GridAgent\venv\lib\site-packages\django\db\migrations\recorder.py", line 77, in applied_migrations
    if self.has_table():
  File "D:\work_space_for_python\calculate_for_GridAgent\venv\lib\site-packages\django\db\migrations\recorder.py", line 56, in has_table
    tables = self.connection.introspection.table_names(cursor)
  File "D:\work_space_for_python\calculate_for_GridAgent\venv\lib\site-packages\django\db\backends\base\introspection.py", line 52, in table_names
    return get_names(cursor)
  File "D:\work_space_for_python\calculate_for_GridAgent\venv\lib\site-packages\django\db\backends\base\introspection.py", line 47, in get_names
    return sorted(ti.name for ti in self.get_table_list(cursor)
  File "D:\work_space_for_python\calculate_for_GridAgent\venv\lib\site-packages\django\db\backends\mysql\introspection.py", line 69, in get_table_list
    cursor.execute("SHOW FULL TABLES")
  File "D:\work_space_for_python\calculate_for_GridAgent\venv\lib\site-packages\django\db\backends\utils.py", line 98, in execute
    return super().execute(sql, params)
  File "D:\work_space_for_python\calculate_for_GridAgent\venv\lib\site-packages\django\db\backends\utils.py", line 66, in execute
    return self._execute_with_wrappers(sql, params, many=False, executor=self._execute)
  File "D:\work_space_for_python\calculate_for_GridAgent\venv\lib\site-packages\django\db\backends\utils.py", line 75, in _execute_with_wrappers
    return executor(sql, params, many, context)
  File "D:\work_space_for_python\calculate_for_GridAgent\venv\lib\site-packages\django\db\backends\utils.py", line 84, in _execute
    return self.cursor.execute(sql, params)
  File "D:\work_space_for_python\calculate_for_GridAgent\venv\lib\site-packages\django\db\utils.py", line 90, in __exit__
    raise dj_exc_value.with_traceback(traceback) from exc_value
  File "D:\work_space_for_python\calculate_for_GridAgent\venv\lib\site-packages\django\db\backends\utils.py", line 82, in _execute
    return self.cursor.execute(sql)
  File "D:\work_space_for_python\calculate_for_GridAgent\venv\lib\site-packages\django\db\backends\mysql\base.py", line 73, in execute
    return self.cursor.execute(query, args)
  File "D:\work_space_for_python\calculate_for_GridAgent\venv\lib\site-packages\pymysql\cursors.py", line 153, in execute
    result = self._query(query)
  File "D:\work_space_for_python\calculate_for_GridAgent\venv\lib\site-packages\pymysql\cursors.py", line 322, in _query
    conn.query(q)
  File "D:\work_space_for_python\calculate_for_GridAgent\venv\lib\site-packages\pymysql\connections.py", line 558, in query
    self._affected_rows = self._read_query_result(unbuffered=unbuffered)
  File "D:\work_space_for_python\calculate_for_GridAgent\venv\lib\site-packages\pymysql\connections.py", line 822, in _read_query_result
    result.read()
  File "D:\work_space_for_python\calculate_for_GridAgent\venv\lib\site-packages\pymysql\connections.py", line 1200, in read
    first_packet = self.connection._read_packet()
  File "D:\work_space_for_python\calculate_for_GridAgent\venv\lib\site-packages\pymysql\connections.py", line 772, in _read_packet
    packet.raise_for_error()
  File "D:\work_space_for_python\calculate_for_GridAgent\venv\lib\site-packages\pymysql\protocol.py", line 221, in raise_for_error
    err.raise_mysql_exception(self._data)
  File "D:\work_space_for_python\calculate_for_GridAgent\venv\lib\site-packages\pymysql\err.py", line 143, in raise_mysql_exception
    raise errorclass(errno, errval)
django.db.utils.OperationalError: (1046, 'No database selected')
Exception in thread django-main-thread:
Traceback (most recent call last):
  File "D:\work_space_for_python\calculate_for_GridAgent\venv\lib\site-packages\django\db\backends\utils.py", line 82, in _execute
    return self.cursor.execute(sql)
  File "D:\work_space_for_python\calculate_for_GridAgent\venv\lib\site-packages\django\db\backends\mysql\base.py", line 73, in execute
    return self.cursor.execute(query, args)
  File "D:\work_space_for_python\calculate_for_GridAgent\venv\lib\site-packages\pymysql\cursors.py", line 153, in execute
    result = self._query(query)
  File "D:\work_space_for_python\calculate_for_GridAgent\venv\lib\site-packages\pymysql\cursors.py", line 322, in _query
    conn.query(q)
  File "D:\work_space_for_python\calculate_for_GridAgent\venv\lib\site-packages\pymysql\connections.py", line 558, in query
    self._affected_rows = self._read_query_result(unbuffered=unbuffered)
  File "D:\work_space_for_python\calculate_for_GridAgent\venv\lib\site-packages\pymysql\connections.py", line 822, in _read_query_result
    result.read()
  File "D:\work_space_for_python\calculate_for_GridAgent\venv\lib\site-packages\pymysql\connections.py", line 1200, in read
    first_packet = self.connection._read_packet()
  File "D:\work_space_for_python\calculate_for_GridAgent\venv\lib\site-packages\pymysql\connections.py", line 772, in _read_packet
    packet.raise_for_error()
  File "D:\work_space_for_python\calculate_for_GridAgent\venv\lib\site-packages\pymysql\protocol.py", line 221, in raise_for_error
    err.raise_mysql_exception(self._data)
  File "D:\work_space_for_python\calculate_for_GridAgent\venv\lib\site-packages\pymysql\err.py", line 143, in raise_mysql_exception
    raise errorclass(errno, errval)
pymysql.err.OperationalError: (1046, 'No database selected')

3.问题排查

3.1 网络常见答案

  • 1.排查settings配置
  • 2.检查mysql是否存在该数据库
  • 3.检查mysql的访问权限设置
    那这几个明显不能解决我的问题

3.2 解决方案

这个小问题卡我一个小时后,灵机一动,发现了不一样的问题

Python Django 使用pymysql报错 django.db.utils.OperationalError: (1046, ‘No database selected‘)_第1张图片
不知道大家发现了什么吗
--------------------无语中---------------
Name 不像其他key一样全部大写 !!!
最终更改成 NAME 解决了
啊啊啊啊啊啊啊啊啊啊

4. 感想

从成为程序员开始就被大小写坑 ,如今还能被大小写坑一个小时。。。。 吐血

你可能感兴趣的:(数据库,python,django,pymysql,mysql)