django.db.utils.OperationalError: SSL connection error
错误通常发生在 Django 尝试与数据库建立 SSL 连接时失败。这可能是由于以下原因之一:
如果是在开发环境且不需要 SSL 连接,可以在数据库配置中禁用 SSL:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql', # 或 mysql 等
'NAME': 'your_db',
'USER': 'your_user',
'PASSWORD': 'your_password',
'HOST': 'localhost',
'PORT': '5432',
'OPTIONS': {
'sslmode': 'disable', # 禁用 SSL
},
}
}
如果需要 SSL 连接,确保正确配置:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql',
'NAME': 'your_db',
'USER': 'your_user',
'PASSWORD': 'your_password',
'HOST': 'your_host',
'PORT': '5432',
'OPTIONS': {
'sslmode': 'require', # 或 'verify-ca', 'verify-full'
'sslrootcert': '/path/to/ca-certificate.crt', # 如果需要
'sslcert': '/path/to/client-certificate.crt', # 如果需要
'sslkey': '/path/to/client-key.key', # 如果需要
},
}
}
pg_hba.conf
和 postgresql.conf
中的 SSL 设置my.cnf
中的 SSL 相关配置确保所有证书路径正确且 Django 进程有权限访问这些文件。
确保你使用的是最新版本的数据库适配器:
pip install --upgrade psycopg2-binary # PostgreSQL
# 或
pip install --upgrade mysqlclient # MySQL
在应用任何更改后,尝试运行:
python manage.py check
python manage.py migrate
如果问题仍然存在,请提供更详细的错误信息以便进一步诊断。