Django製アプリにmysqlclientを入れたら，EC2へデプロイできなくなった話

## 環境
- AWS CodeStar
- Django: 2.1.4
- PyMySQL: 0.8.0

## 執筆したきっかけ
- どうやらここ数日に，テンプレートアプリが変わった．その結果，mysqlclientを入れてデプロイすると，エラーを吐くようになった
- あくまで備忘録程度です

## mysqlclientを入れるとエラーが出る

下記のライブラリをインストールし，EC2へデプロイするとエラーが出ました．

- Django: 2.1.4
- mysqlclient==1.4.2.post1

CodeDeploy上のエラー

```
The overall deployment failed because too many individual instances failed deployment, too few healthy instances are available for deployment, or some instances in your deployment group are experiencing problems.
```

mysqlコマンドが入っていないことも疑いましたが．`/var/log/aws/codedeploy-agent/codedeploy-agent.log`を見るといろいろ書いてありました

```
2019-06-06 15:15:32 ERROR [codedeploy-agent(2822)]: InstanceAgent::Plugins::CodeDeployPlugin::CommandPoller: Error during perform: InstanceAgent::Plugins::CodeDeployPlugin::ScriptError - Script at specified location: scripts/stop_server run as user root failed with exit code 1 - /opt/codedeploy-agent/lib/instance_agent/plugins/codedeploy/hook_executor.rb:183:in `execute_script'
/opt/codedeploy-agent/lib/instance_agent/plugins/codedeploy/hook_executor.rb:129:in `block (2 levels) in execute'
/opt/codedeploy-agent/lib/instance_agent/plugins/codedeploy/hook_executor.rb:117:in `each'
/opt/codedeploy-agent/lib/instance_agent/plugins/codedeploy/hook_executor.rb:117:in `block in execute'
/opt/codedeploy-agent/lib/instance_agent/plugins/codedeploy/hook_executor.rb:208:in `create_script_log_file_if_needed'
/opt/codedeploy-agent/lib/instance_agent/plugins/codedeploy/hook_executor.rb:115:in `execute'
/opt/codedeploy-agent/lib/instance_agent/plugins/codedeploy/command_executor.rb:158:in `block (3 levels) in map'
/opt/codedeploy-agent/lib/instance_agent/plugins/codedeploy/command_executor.rb:146:in `each'
/opt/codedeploy-agent/lib/instance_agent/plugins/codedeploy/command_executor.rb:146:in `block (2 levels) in map'
/opt/codedeploy-agent/lib/instance_agent/plugins/codedeploy/command_executor.rb:68:in `execute_command'
/opt/codedeploy-agent/lib/instance_agent/plugins/codedeploy/command_poller.rb:114:in `process_command'
/opt/codedeploy-agent/lib/instance_agent/plugins/codedeploy/command_poller.rb:96:in `acknowledge_and_process_command'
/opt/codedeploy-agent/lib/instance_agent/plugins/codedeploy/command_poller.rb:75:in `block in perform'
/opt/codedeploy-agent/vendor/gems/concurrent-ruby-1.0.5/lib/concurrent/executor/ruby_thread_pool_executor.rb:348:in `call'
/opt/codedeploy-agent/vendor/gems/concurrent-ruby-1.0.5/lib/concurrent/executor/ruby_thread_pool_executor.rb:348:in `run_task'
/opt/codedeploy-agent/vendor/gems/concurrent-ruby-1.0.5/lib/concurrent/executor/ruby_thread_pool_executor.rb:337:in `block (3 levels) in create_worker'
/opt/codedeploy-agent/vendor/gems/concurrent-ruby-1.0.5/lib/concurrent/executor/ruby_thread_pool_executor.rb:320:in `loop'
/opt/codedeploy-agent/vendor/gems/concurrent-ruby-1.0.5/lib/concurrent/executor/ruby_thread_pool_executor.rb:320:in `block (2 levels) in create_worker'
/opt/codedeploy-agent/vendor/gems/concurrent-ruby-1.0.5/lib/concurrent/executor/ruby_thread_pool_executor.rb:319:in `catch'
/opt/codedeploy-agent/vendor/gems/concurrent-ruby-1.0.5/lib/concurrent/executor/ruby_thread_pool_executor.rb:319:in `block in create_worker'
/opt/codedeploy-agent/vendor/gems/logging-1.8.2/lib/logging/diagnostic_context.rb:323:in `call'
/opt/codedeploy-agent/vendor/gems/logging-1.8.2/lib/logging/diagnostic_context.rb:323:in `block in create_with_logging_context'
2019-06-06 15:15:32 WARN  [codedeploy-agent(2822)]: InstanceAgent::Plugins::CodeDeployPlugin::CommandPoller: Calling PutHostCommandComplete: "Code Error" 
2019-06-06 15:15:32 INFO  [codedeploy-agent(2822)]: Version file found in /opt/codedeploy-agent/.version with agent version OFFICIAL_1.0-1.1597_rpm.
2019-06-06 15:15:32 INFO  [codedeploy-agent(2822)]: [Aws::CodeDeployCommand::Client 200 0.034369 0 retries] put_host_command_complete(command_status:"Failed",diagnostics:{format:"JSON",payload:"{\"error_code\":5,\"script_name\":\"\",\"message\":\"Script at specified location: scripts/stop_server run as user root failed with exit code 1\
```

## PyMySQLなら動いた
私の環境依存だとは思いますが，上記のエラーが出た後に`mysqlclient`をアンインストールしても同様のエラーが出ます．
結論，新規アプリやインスタンスを作って，PyMySQLをインストールしました．
mysqlclientとの違いは，`manage.py`と`wsgi.py`に下記を追記してください．

```python
import pymysql
pymysql.install_as_MySQLdb()
```

## 感想
根本的な解決にはなっていませんが，とりあえず解決できました．
これ以上追求する予定はありませんが，同じ問題に遭遇した方のためになればと思います．

## 参考
[MySQLとDjangoをつなげる手順](http://jun-networks.hatenablog.com/entry/2018/06/24/042716)