衝動的にLightsailとDjangoでWebサイトを作る その2
前回からの続き。
構成(再掲)
- レンタルサーバー:AWS Lightsail
- OS:Ubuntu 20.04.2
- データベース:MySQL 8.0.25
- サーバーソフト:Apache 2.4.41
- 言語:Python 3.8.5
- フレームワーク:Django 3.2.2
- ドメイン取得:AWS Route 53
mysqlclientがインストールできない
PythonからMySQLを操作する際、mysqlclientというライブラリが必要になる。
他にも何種類かあるらしいが、mysqlclientはDjangoで推奨されている。
データベース | Django ドキュメント | Django
ところがpip install mysqlclientを実行すると以下のようなエラーが出る。
ERROR: Command errored out with exit status 1: command: /usr/bin/python3 -c 'import sys, setuptools, tokenize; sys.argv[0] = '"'"'/tmp/pip-install-be8gqoox/mysqlclient/setup.py'"'"'; __file__='"'"'/tmp/pip-install-be8gqoox/mysqlclient/setup.py'"'"';f=getattr(tokenize, '"'"'open'"'"', open)(__file__);code=f.read().replace('"'"'\r\n'"'"', '"'"'\n'"'"');f.close();exec(compile(code, __file__, '"'"'exec'"'"'))' egg_info --egg-base /tmp/pip-install-be8gqoox/mysqlclient/pip-egg-info cwd: /tmp/pip-install-be8gqoox/mysqlclient/ Complete output (15 lines): /bin/sh: 1: mysql_config: not found /bin/sh: 1: mariadb_config: not found /bin/sh: 1: mysql_config: not found Traceback (most recent call last): File "<string>", line 1, in <module> File "/tmp/pip-install-be8gqoox/mysqlclient/setup.py", line 15, in <module> metadata, options = get_config() File "/tmp/pip-install-be8gqoox/mysqlclient/setup_posix.py", line 70, in get_config libs = mysql_config("libs") File "/tmp/pip-install-be8gqoox/mysqlclient/setup_posix.py", line 31, in mysql_config raise OSError("{} not found".format(_mysql_config_path)) OSError: mysql_config not found mysql_config --version mariadb_config --version mysql_config --libs ---------------------------------------- ERROR: Command errored out with exit status 1: python setup.py egg_info Check the logs for full command output.
下記記事を参考に同様の手順を試したところ、Ubuntu20.04でもインストールに成功した。どうやら開発ツールも導入しておく必要があるらしい。
というか本記事を書く段階で改めて調べたら、mysqlclientのgithubリポジトリに、「pip installする前に以下のコマンドを実行して、development headers and librariesを導入しろ」としっかり記載があった。
# Debian / Ubuntu $ sudo apt-get install python3-dev default-libmysqlclient-dev build-essential
落ち着いてちゃんと公式のREADME読むの大事(自戒)
mysqlclientからMySQLに接続できない
MySQL8.0.25では、
create user test@localhost identified by 'test';
を実行すると、caching_sha2_passwordという認証プラグインを使用するユーザーが作成される。しかし、caching_sha2_passwordを使用するユーザーでmysqlclientからMySQLへログインしようとすると、エラーで弾かれる。
同様の問題はmysqlclientのissueにも報告されている。
接続するためには、認証プラグインにmysql_native_passwordを使用したユーザーを作成する必要がある。
create user test@localhost identified with mysql_native_password by 'test';
2つの認証プラグインの違いについては、正直今の知識ではよくわからない。
mod_wsgiがインストールできない
ApacheとDjango(というかPython)を連携して使用するためには、mod_wsgiというモジュールが必要になる。
しかしこれもmysqlclientと同様、ただpip install mod-wsgiを実行しただけではエラーになってインストールできない。
事前に
sudo apt install apache2-dev
の実行が必要。
続く
本日はここまで。