简体   繁体   English

如何通过 SSL 连接将 Django 连接到 MySQL 数据库?

[英]How to connect Django to a MySQL database over an SSL connection?

I'm trying to connect Django to a MySQL database which is accessible through an SSL connection.我正在尝试将 Django 连接到可通过 SSL 连接访问的 MySQL 数据库。 How do I configure this?我该如何配置?

My first guess would be setting the 'OPTIONS' property of the database definition.我的第一个猜测是设置数据库定义的 'OPTIONS' 属性。 However, I can't find info on what possible options to use.但是,我找不到有关使用哪些可能选项的信息。 The option 'ssl': '/map/to/ca-cert.pem' does not work.选项'ssl': '/map/to/ca-cert.pem'不起作用。

The following command seems to work:以下命令似乎有效:

mysql -h url.to.host -u lizard -p --ssl-ca=./ca-cert.pem

Edit: Ok I'm looking at the python-mysqldb documentation... maybe I can find the answer there.编辑:好的,我正在查看 python-mysqldb 文档...也许我可以在那里找到答案。

Django uses the Python MySQLdb library to interface with MySQL. Django 使用 Python MySQLdb库与 MySQL 交互。 Looking at the MySQLdb connection documentation , it looks like the ssl option requires a dictionary argument.查看MySQLdb 连接文档,看起来ssl选项需要字典参数。 So this might work:所以这可能有效:

'OPTIONS': {'ssl': {'key': '/map/to/ca-cert.pem'}}

The MySQL client must be provided with three keys: MySQL 客户端必须提供三个键:

  • CA cert CA证书
  • client cert客户证书
  • client key客户密钥

See the MySQL documentation for the instructions for creating these keys and setting up the server.有关创建这些密钥和设置服务器的说明,请参阅MySQL 文档

NOTE: There is an open issue that seems to be related to using openssl v1.0.1 to create the certificates for mysql 5.5.x注意:有一个未解决的问题似乎与使用 openssl v1.0.1 为 mysql 5.5.x 创建证书有关

This is an example entry for the Django settings file:这是 Django 设置文件的示例条目:

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.mysql',  
        'NAME': '<DATABASE NAME>',                     
        'USER': '<USER NAME>',
        'PASSWORD': '<PASSWORD>',
        'HOST': '<HOST>', 
        'PORT': '3306',
        'OPTIONS':  {
            'ssl': {'ca': '<PATH TO CA CERT>',
            'cert': '<PATH TO CLIENT CERT>',
            'key': '<PATH TO CLIENT KEY>'
            }
        }
    }
}

I was getting a "SSL connection error: SSL_CTX_set_default_verify_paths failed') "error when running python manage.py migrate运行python manage.py migrate时出现“SSL 连接错误:SSL_CTX_set_default_verify_paths 失败”)错误

I used pip to install django-mysql-ssl package.我使用 pip 安装 django-mysql-ssl 包。 It still wasn't working.它仍然没有工作。 I had to change "ca" to "ssl-ca" and now it works.我不得不将“ca”更改为“ssl-ca”,现在它可以工作了。

'OPTIONS':  {
                    'ssl': {'ssl-ca': '<PATH TO CA CERT>',

                            }
                      }

I'm not sure if it is actually using encryption, but it no longer throws an error.我不确定它是否真的在使用加密,但它不再抛出错误。 I am running local django app connected to an AWS mariaDB instance.我正在运行连接到 AWS mariaDB 实例的本地 django 应用程序。

Edit: django-mysql-ssl package is not required starting from Django 1.8, as the functionality is built-in now.编辑:从 Django 1.8 开始不需要 django-mysql-ssl 包,因为该功能现在是内置的。 See Dependencies section in the package description here请参阅此处的包描述中的依赖项部分

Dependencies依赖关系

This application is confirmed to work with Django 1.5.此应用程序已确认可与 Django 1.5 一起使用。 It should also work with Django 1.6-1.7.它也应该适用于 Django 1.6-1.7。 This plugin is not necessary for Django 1.8, as the capability is built into the core.这个插件对于 Django 1.8 不是必需的,因为该功能内置于核心中。

In case of SSL encrypted connection will probably need cipher parameter在 SSL 加密连接的情况下可能需要cipher参数

DATABASE = {
    'default': {
        'ENGINE': 'django.db.backends.mysql',
        'NAME': 'db_name',
        'USER': 'user',
        'PASSWORD': 'pass',
        'HOST': 'host',
        'PORT': '3306',
        'OPTIONS': {
            'ssl': {
                'ca': 'path/to/pem',
                'key': 'path/to/pem',
                'cert': 'path/to/pem',
                'cipher': 'AES128-SHA' #| 'AES128-SHA256' | 'DHE-RSA-AES256-SHA'
            }
        }
    }
}

MySQL passes a default cipher list to the SSL library. MySQL 将默认密码列表传递给 SSL 库。 More details can be found here:更多详情可在这找到:

  1. https://dev.mysql.com/doc/refman/5.7/en/encrypted-connection-protocols-ciphers.html#encrypted-connection-cipher-configuration https://dev.mysql.com/doc/refman/5.7/en/encrypted-connection-protocols-ciphers.html#encrypted-connection-cipher-configuration
  2. https://dev.mysql.com/doc/refman/5.7/en/encrypted-connection-protocols-ciphers.html#encrypted-connection-protocol-negotiation https://dev.mysql.com/doc/refman/5.7/en/encrypted-connection-protocols-ciphers.html#encrypted-connection-protocol-negotiation

Can't leave comments for @Drew answer, so let it be new one.不能对@Drew 的回答发表评论,所以让它成为新的。

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

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