博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Python框架之Django的数据库配置问题
阅读量:5902 次
发布时间:2019-06-19

本文共 6101 字,大约阅读时间需要 20 分钟。

准备开始python的环境,用django框架其默认的数据库是sqlite3 ,要改用mysql来存储数据

在执行migrate的时候出现问题(错误见末尾):

Unhandled exception in thread started by <function check_errors.<locals>.wrapper

 at 0x031BD6F0>

Traceback (most recent call last):

  File "C:\Python36\lib\site-packages\django\db\backends\base\base.py", line 213

, in ensure_connection

    self.connect()

  File "C:\Python36\lib\site-packages\django\db\backends\base\base.py", line 189

, in connect

    self.connection = self.get_new_connection(conn_params)

  File "C:\Python36\lib\site-packages\django\db\backends\mysql\base.py", line 27

4, in get_new_connection

    conn = Database.connect(**conn_params)

  File "C:\Python36\lib\site-packages\MySQLdb\__init__.py", line 86, in Connect

    return Connection(*args, **kwargs)

  File "C:\Python36\lib\site-packages\MySQLdb\connections.py", line 204, in __in

it__

    super(Connection, self).__init__(*args, **kwargs2)

_mysql_exceptions.OperationalError: (1045, "Access denied for user 'ODBC'@'local

host' (using password: YES)")

The above exception was the direct cause of the following exception:

Traceback (most recent call last):

  File "C:\Python36\lib\site-packages\django\utils\autoreload.py", line 227, in

wrapper

    fn(*args, **kwargs)

  File "C:\Python36\lib\site-packages\django\core\management\commands\runserver.

py", line 125, in inner_run

    self.check(display_num_errors=True)

  File "C:\Python36\lib\site-packages\django\core\management\base.py", line 359,

 in check

    include_deployment_checks=include_deployment_checks,

  File "C:\Python36\lib\site-packages\django\core\management\base.py", line 346,

 in _run_checks

    return checks.run_checks(**kwargs)

  File "C:\Python36\lib\site-packages\django\core\checks\registry.py", line 81,

in run_checks

    new_errors = check(app_configs=app_configs)

  File "C:\Python36\lib\site-packages\django\core\checks\model_checks.py", line

30, in check_all_models

    errors.extend(model.check(**kwargs))

  File "C:\Python36\lib\site-packages\django\db\models\base.py", line 1282, in c

heck

    errors.extend(cls._check_fields(**kwargs))

  File "C:\Python36\lib\site-packages\django\db\models\base.py", line 1357, in _

check_fields

    errors.extend(field.check(**kwargs))

  File "C:\Python36\lib\site-packages\django\db\models\fields\__init__.py", line

 909, in check

    errors = super(AutoField, self).check(**kwargs)

  File "C:\Python36\lib\site-packages\django\db\models\fields\__init__.py", line

 219, in check

    errors.extend(self._check_backend_specific_checks(**kwargs))

  File "C:\Python36\lib\site-packages\django\db\models\fields\__init__.py", line

 322, in _check_backend_specific_checks

    return connections[db].validation.check_field(self, **kwargs)

  File "C:\Python36\lib\site-packages\django\db\backends\mysql\validation.py", l

ine 49, in check_field

    field_type = field.db_type(self.connection)

  File "C:\Python36\lib\site-packages\django\db\models\fields\__init__.py", line

 640, in db_type

    return connection.data_types[self.get_internal_type()] % data

  File "C:\Python36\lib\site-packages\django\utils\functional.py", line 35, in _

_get__

    res = instance.__dict__[self.name] = self.func(instance)

  File "C:\Python36\lib\site-packages\django\db\backends\mysql\base.py", line 17

4, in data_types

    if self.features.supports_microsecond_precision:

  File "C:\Python36\lib\site-packages\django\utils\functional.py", line 35, in _

_get__

    res = instance.__dict__[self.name] = self.func(instance)

  File "C:\Python36\lib\site-packages\django\db\backends\mysql\features.py", lin

e 53, in supports_microsecond_precision

    return self.connection.mysql_version >= (5, 6, 4) and Database.version_info

>= (1, 2, 5)

  File "C:\Python36\lib\site-packages\django\utils\functional.py", line 35, in _

_get__

    res = instance.__dict__[self.name] = self.func(instance)

  File "C:\Python36\lib\site-packages\django\db\backends\mysql\base.py", line 38

5, in mysql_version

    with self.temporary_connection() as cursor:

  File "C:\Python36\lib\contextlib.py", line 82, in __enter__

    return next(self.gen)

  File "C:\Python36\lib\site-packages\django\db\backends\base\base.py", line 591

, in temporary_connection

    cursor = self.cursor()

  File "C:\Python36\lib\site-packages\django\db\backends\base\base.py", line 254

, in cursor

    return self._cursor()

  File "C:\Python36\lib\site-packages\django\db\backends\base\base.py", line 229

, in _cursor

    self.ensure_connection()

  File "C:\Python36\lib\site-packages\django\db\backends\base\base.py", line 213

, in ensure_connection

    self.connect()

  File "C:\Python36\lib\site-packages\django\db\utils.py", line 94, in __exit__

    six.reraise(dj_exc_type, dj_exc_value, traceback)

  File "C:\Python36\lib\site-packages\django\utils\six.py", line 685, in reraise

    raise value.with_traceback(tb)

  File "C:\Python36\lib\site-packages\django\db\backends\base\base.py", line 213

, in ensure_connection

    self.connect()

  File "C:\Python36\lib\site-packages\django\db\backends\base\base.py", line 189

, in connect

    self.connection = self.get_new_connection(conn_params)

  File "C:\Python36\lib\site-packages\django\db\backends\mysql\base.py", line 27

4, in get_new_connection

    conn = Database.connect(**conn_params)

  File "C:\Python36\lib\site-packages\MySQLdb\__init__.py", line 86, in Connect

    return Connection(*args, **kwargs)

  File "C:\Python36\lib\site-packages\MySQLdb\connections.py", line 204, in __in

it__

    super(Connection, self).__init__(*args, **kwargs2)

django.db.utils.OperationalError: (1045, "Access denied for user 'ODBC'@'localho

st' (using password: YES)")

这里很奇怪为什么我配置的用户是 admin_py 怎么会显示 ‘ODBC’@‘localhost’?

有人说是账号密码错误,通过报错看来的和账号密码错误相关,但是手工去登陆mysql是OK的啊!

折腾了一会儿,仔细看官方文档配置:

DATABASES = {    'default': {        'ENGINE': 'django.db.backends.postgresql',        'NAME': 'mydatabase',        'USER': 'mydatabaseuser',        'PASSWORD': 'mypassword',        'HOST': '127.0.0.1',        'PORT': '5432',    }}

我自己的配置:

DATABASES = {    'default': {        'ENGINE': 'django.db.backends.mysql',        'HOST': '127.0.0.1',        'PORT': 3306,        'NAME': 'platform_admin_py',        'USERNAME': 'admin_py',        'PASSWORD': 'admin',    }}

发现了没有?用户名的的那个KEY不对! 我承认我这是从网上看别人的配置写的,偷懒了,这就是代价

     本文转自andylhz 51CTO博客,原文链接:http://blog.51cto.com/andylhz2009/1923552,如需转载请自行联系原作者

你可能感兴趣的文章
struts2入门
查看>>
SpringMVC知识点梳理—够你面试掰活的(超详细)
查看>>
OC-iOS企业版账号的上线流程
查看>>
Android动画:行云流水的矢量图标动画
查看>>
nginx
查看>>
VUE使用ESLint
查看>>
JEESZ-Redis分布式缓存安装和使用
查看>>
Jsoup下载抖音文件限制1M的问题解决
查看>>
Python邮件发送,看这篇就够
查看>>
科学家研究出环保的一次性电池 减轻生态环境的负担
查看>>
关于vue的mixins
查看>>
JavaScript中常见数据结构及算法
查看>>
Android Gradle 干货分享
查看>>
图片轮播组件实现
查看>>
贵阳a货翡翠,太原a货翡翠
查看>>
java springcloud版b2b2c社交电商spring cloud分布式微服务(十五)Springboot整合RabbitMQ...
查看>>
AEC——征信数据管理系统
查看>>
(十六)spring cloud微服务架构b2b2c电子商务-SOA中springmvc中restful服务的数据权限加密方式分享...
查看>>
react性能优化
查看>>
【更新】全新的数据填报,更强的BI分析,Wyn Enterprise V3.0 Update 2 发布
查看>>