python - 与自动旋转密码数据库的 Django 连接

标签 python mysql django passwords

我有一个运行良好的 Django 应用程序。它连接到托管在云端的 MySQL 服务器。

出于安全原因,MySQL 服务器设置为每 30 天自动轮换一次密码。 Django 只有在使用我开发的自定义函数加载 settings.py 时才能访问新密码(这将从 AWS Secrets Manager).

我正在寻找一种方法让 Django 检测连接是否有问题,然后更新对用户透明的密码。

这可能吗?

最佳答案

我能想到的选项:

  1. 您可以使用自定义中间件在每个请求之前检查连接。
  2. 您可以使用 cron 作业定期检查失败的数据库连接,并在发现此类失败时更新设置。

要检查连接,您可以使用方法 django.db.connection.ensure_connection()。如果你看this answer ,你可以尝试这样的事情:

from django.db import connection
from django.db.utils import OperationalError

class Command(BaseCommand):
    def handle(self, *args, **options):
        try:
            # if this succeeds, no further action is needed
            connection.ensure_connection()
        except OperationalError:
            # update the DB password and try again

关于python - 与自动旋转密码数据库的 Django 连接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52924887/

相关文章:

python - ImageField/FileField Django 表单 目前无法修剪文件名的路径

php - 使用 PDO 从表 mySQL 制作模型

python - "user = self.model(useremail=BmUserManager.normalize_email(useremail))"有什么用?它用于创建新的自定义用户吗?

python - 在 Django 中优化查询

python - 如何在 tensorflow 中实现图像(二维数组)序列滑动窗口?

python - F2PY 在 Windows 7 上找不到 intel fortran 编译器

python - 为什么 Python 显示 'ValueError: could not convert string to float' ?

mysql - 来自不同mysql表的最小值和最大值

php - 通过循环将 INSERT 追加到 SQL 中

django - Nginx、Gunicorn、Django、Celery(Redis) : upstream prematurely closed connection 502 gateway