简体   繁体   English

如何让 Django 从 my.cnf 读取 MySQL 密码?

[英]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.我的主目录中有一个 .my.cnf 文件,因此几乎所有时间,我都可以仅使用我的用户名访问我的 MySQL 数据库。

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.但是,在 django 中,如果我从 settings.py 中的配置中删除密码,如下所示,django 应用程序无法访问数据库。

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.我需要将密码放在 settings.py 文件中似乎不安全。 These tend to be included in git repositories and such and it seems a bad idea.这些往往包含在 git 存储库等中,这似乎是一个坏主意。

So, is there a way to get the python MySQL driver being used by django to use the standard my.cnf files?那么,有没有办法让 django 使用的 python MySQL 驱动程序使用标准的 my.cnf 文件?

Under 'OPTIONS' you can specify a 'read_default_file' like this:在“选项”下,您可以指定一个“read_default_file”,如下所示:

'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文档: https : //docs.djangoproject.com/en/1.7/ref/databases/#connecting-to-the-database

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何将 mysql 默认文件 (my.cnf) 与 sqlalchemy 一起使用? - how to use mysql defaults file (my.cnf) with sqlalchemy? 更改my.cnf文件中的innodb_page_size不会重新启动mysql数据库 - Changing innodb_page_size in my.cnf file does not restart mysql database MySQLPython忽略了我的my.cnf文件。 它从哪里获取信息? - MySQLPython is ignoring my my.cnf file. Where does it get its information? 如何更新 my.cnf 文件以增加 innodb-buffer-pool-size? - How to updated my.cnf file to increase innodb-buffer-pool-size? 如何从 HTML 表单获取密码到我在 Django 中的视图 - How to get password from HTML form to my views in Django 如何从 Django 的注册表单中的密码字段中获取纯密码 - How to get the plain password from the password field in the signup form in Django 如何在Django Rest框架中从电子邮件和密码获取身份验证令牌? - How to get auth token from email and password in django rest framework? 如何循环输入密码/用户名登录页面并从外部文件读取密码/用户名? - How do I loop my password/username login page and read the password/username from an external file? 如何在sympy中获取DNF / CNF表达式的子句? - How to get clauses of the DNF/CNF expression in sympy? Django中的MySQL密码 - MySQL password in Django
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM