简体   繁体   中英

Django mysql database credentials read_default_file giving error

I am providing mySql credentials from a different conf file to django project. Earlier it was working fine, but now it stopped working after I reinstalled my 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

Also, the location of the conf file is as follows:

 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

The error given is as follows:

 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 <function wrapper at 0x7fa2c1d28cf8>
 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)")

Not able to figure out why is it giving error. If anything is wrong, can anyone suggest some other methods of doing it.

Well if you still want's to use options read_default_file

Then Solution is

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

Try setting the permission of the config file to 644

chmod 644 connection.cnf

The problem is that because the permissions are too open, the file is ignored, and therefore you are not logged in into mysql.

I faced the same problem and it was pointing to the wrong path.

Try printing the path to the terminal

print(os.path.join(BASE_DIR, ''))

And then correct the path to point to the file

在密码字符串中添加双引号对我有用。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM