当前位置:  开发笔记 > 编程语言 > 正文

Django mysql数据库凭据read_default_file提供错误

如何解决《Djangomysql数据库凭据read_default_file提供错误》经验,为你挑选了1个好方法。

我正在从Django项目的另一个conf文件中提供mySql凭据。之前它可以正常工作,但是现在在我重新安装Ubuntu后它停止了工作。

settings.py
 DATABASES = {
     'default': {
         'ENGINE': 'django.db.backends.mysql',
         'OPTIONS': {
           'read_default_file': './connection.cnf',
         }
     }
 }
connection.cnf
 [client]
 database=test
 user=root
 password=rajiv@123
 HOST=localhost
 PORT=3306
 default-charset=utf8

另外,conf文件的位置如下:

 rajiv@ubuntu:~/djangoProjects/test$ ls -l
 total 9
 drwxrwxrwx 1 root root 4096 Dec 28 11:04 test
 drwxrwxrwx 1 root root 4096 Dec 27 14:00 testApp
 -rwxrwxrwx 1 root root  103 Dec  3 00:37 connection.cnf
 -rwxrwxrwx 1 root root  252 Oct 15 11:36 manage.py
给出的错误如下:
 rajiv@ubuntu:~/djangoProjects/test$ python manage.py runserver
 Performing system checks...

 Warning: World-writable config file '/home/rajiv/djangoProjects/test/connection.cnf' is ignored
 Unhandled exception in thread started by 
 Traceback (most recent call last):
   File "/usr/local/lib/python2.7/dist-packages/django/utils/autoreload.py", line 226, in wrapper
     fn(*args, **kwargs)
   File "/usr/local/lib/python2.7/dist-packages/django/core/management/commands/runserver.py", line 116, in inner_run
     self.check(display_num_errors=True)
   File "/usr/local/lib/python2.7/dist-packages/django/core/management/base.py", line 426, in check
     include_deployment_checks=include_deployment_checks,
   File "/usr/local/lib/python2.7/dist-packages/django/core/checks/registry.py", line 75, in run_checks
     new_errors = check(app_configs=app_configs)
   File "/usr/local/lib/python2.7/dist-packages/django/core/checks/model_checks.py", line 28, in check_all_models
     errors.extend(model.check(**kwargs))
   File "/usr/local/lib/python2.7/dist-packages/django/db/models/base.py", line 1170, in check
     errors.extend(cls._check_fields(**kwargs))   
   File "/usr/local/lib/python2.7/dist-packages/django/db/models/base.py", line 1247, in _check_fields
     errors.extend(field.check(**kwargs))
   File "/usr/local/lib/python2.7/dist-packages/django/db/models/fields/__init__.py", line 925, in check
     errors = super(AutoField, self).check(**kwargs)
   File "/usr/local/lib/python2.7/dist-packages/django/db/models/fields/__init__.py", line 208, in check
     errors.extend(self._check_backend_specific_checks(**kwargs))
   File "/usr/local/lib/python2.7/dist-packages/django/db/models/fields/__init__.py", line 317, in _check_backend_specific_checks
     return connections[db].validation.check_field(self, **kwargs)
   File "/usr/local/lib/python2.7/dist-packages/django/db/backends/mysql/validation.py", line 18, in check_field
     field_type = field.db_type(connection)
   File "/usr/local/lib/python2.7/dist-packages/django/db/models/fields/__init__.py", line 625, in db_type
     return connection.data_types[self.get_internal_type()] % data
   File "/usr/local/lib/python2.7/dist-packages/django/db/__init__.py", line 36, in __getattr__
     return getattr(connections[DEFAULT_DB_ALIAS], item)
   File "/usr/local/lib/python2.7/dist-packages/django/utils/functional.py", line 33, in __get__
     res = instance.__dict__[self.name] = self.func(instance)
   File "/usr/local/lib/python2.7/dist-packages/django/db/backends/mysql/base.py", line 184, in data_types
     if self.features.supports_microsecond_precision:
   File "/usr/local/lib/python2.7/dist-packages/django/utils/functional.py", line 33, in __get__
     res = instance.__dict__[self.name] = self.func(instance)
   File "/usr/local/lib/python2.7/dist-packages/django/db/backends/mysql/features.py", line 53, in supports_microsecond_precision
     return self.connection.mysql_version >= (5, 6, 4) and Database.version_info >= (1, 2, 5)
   File "/usr/local/lib/python2.7/dist-packages/django/utils/functional.py", line 33, in __get__
     res = instance.__dict__[self.name] = self.func(instance)
   File "/usr/local/lib/python2.7/dist-packages/django/db/backends/mysql/base.py", line 359, in mysql_version
     with self.temporary_connection():
   File "/usr/lib/python2.7/contextlib.py", line 17, in __enter__
     return self.gen.next()
   File "/usr/local/lib/python2.7/dist-packages/django/db/backends/base/base.py", line 564, in temporary_connection
     cursor = self.cursor()
   File "/usr/local/lib/python2.7/dist-packages/django/db/backends/base/base.py", line 231, in cursor
     cursor = self.make_debug_cursor(self._cursor())
   File "/usr/local/lib/python2.7/dist-packages/django/db/backends/base/base.py", line 204, in _cursor
     self.ensure_connection()
   File "/usr/local/lib/python2.7/dist-packages/django/db/backends/base/base.py", line 199, in ensure_connection
     self.connect()
   File "/usr/local/lib/python2.7/dist-packages/django/db/utils.py", line 95, in __exit__
     six.reraise(dj_exc_type, dj_exc_value, traceback)
   File "/usr/local/lib/python2.7/dist-packages/django/db/backends/base/base.py", line 199, in ensure_connection
     self.connect()
   File "/usr/local/lib/python2.7/dist-packages/django/db/backends/base/base.py", line 171, in connect
     self.connection = self.get_new_connection(conn_params)
   File "/usr/local/lib/python2.7/dist-packages/django/db/backends/mysql/base.py", line 264, in get_new_connection
     conn = Database.connect(**conn_params)
   File "/usr/lib/python2.7/dist-packages/MySQLdb/__init__.py", line 81, in Connect
     return Connection(*args, **kwargs)
   File "/usr/lib/python2.7/dist-packages/MySQLdb/connections.py", line 206, in __init__
     super(Connection, self).__init__(*args, **kwargs2)
 django.db.utils.OperationalError: (1045, "Access denied for user 'rajiv'@'localhost' (using password: NO)")

无法弄清楚为什么会出错。如果有任何问题,谁能建议其他方法。



1> Kjjassy..:

好吧,如果您仍然希望使用选项 read_default_file

那么解决方案是

DATABASES = {
     'default': {
         'ENGINE': 'django.db.backends.mysql',
         'OPTIONS': {
           'read_default_file': os.path.join(BASE_DIR, 'connection.cnf'),
         }
     }
 }

connection.cnf

[client]
database = test
user = root
password = rajiv@123
default-character-set = utf8

https://code.djangoproject.com/ticket/24653

推荐阅读
手机用户2502852037
这个屌丝很懒,什么也没留下!
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有