2
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

MySQL+DjangoでSSL通信

Posted at

DjangoでWebアプリ開発する際にMySQLサーバーとSSL通信する方法をまとめます

SSL証明書の取得

SSL証明書を取得します.例えば,AWSのRDSでMySQLを使っているなら
https://docs.aws.amazon.com/AmazonRDS/latest/UserGuide/ssl-certificate-rotation-mysql.html
を参照.

証明書をWebサーバの/usr/local/ssl/内等に置きます.証明書自体の権限は644,証明書までのディレクトリの権限は755にする必要が有るので注意.

MySQLサーバと通信できるかテスト

SSL通信出来る状態になっていれば

mysql -h ホスト名 -u ユーザー名 -p --ssl-ca=/usr/local/ssl/証明書.pem --ssl-mode=VERIFY_IDENTITY

で接続可能です.

Djangoの設定

Djangoのsettings.pyを書き換えます.具体的には,DATABASES={}内のOPTIONSに'ssl'を追加します.

settings.py
DATABASES = {
    'default': {
        ...
        'OPTIONS': {
                   ...
                   'ssl': {'ca' : '/usr/local/ssl/証明書.pem'},  # 追加
                   },
        ...
    }
}

以上です!

2
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?