简体   繁体   中英

how to get django to read MySQL password from my.cnf?

I have a .my.cnf file in my home directory so that, almost all the time, I can access my MySQL databases with just my username.

However, in django, if I remove the password from the configuration in the settings.py, as seen below, the django app cannot access the database.

DATABASES = {
    'default': {
        'NAME': 'calaccess_raw',
        'PASSWORD': 'ZuperZekretPazzword',
        'ENGINE': 'django.db.backends.mysql',
        'USER': 'ray',
        'HOST': 'localhost',
        'PORT': '3306',
        'OPTIONS': {
            'local_infile': 1,
        }
    }
}

It seems insecure that I need to put my password inside the settings.py file. These tend to be included in git repositories and such and it seems a bad idea.

So, is there a way to get the python MySQL driver being used by django to use the standard my.cnf files?

Under 'OPTIONS' you can specify a 'read_default_file' like this:

'OPTIONS': {
    'read_default_file': '/path/to/my.cnf',
},

Give that a try; hope it helps.

Docs: https://docs.djangoproject.com/en/1.7/ref/databases/#connecting-to-the-database

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