【Mac】Django アプリ作成 & MySQL 初回 migrate まで

  1. Python 仮想環境の作成
  2. Django プロジェクト&アプリケーションの作成
  3. MySQL へ初回 migrate
  4. 管理ユーザーの作成

Python 仮想環境の作成

任意の場所に Python 仮想環境を作成、起動します。

% python3 -m venv justjam
% cd justjam
% source bin/activate

Django プロジェクト&アプリケーションの作成

仮想環境内で django をインストール、そしてプロジェクトとアプリケーションを作成します。

% pip install --upgrade pip
% pip install django
% django-admin startproject justjam_proj
% cd justjam_proj
% python manage.py startapp api
# settings.py

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'api.apps.ApiConfig', # 追記分
]

runserver を実行し、ブラウザで動作確認します。

% python manage.py runserver

MySQL へ初回 migrate

注)カスタムユーザーモデルを使用する場合は、以下のステップに進む前に設定を完了しておく必要があります。

settings.py の DATABASES を変更し MySQL 対応にします。

# settings.py
DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.mysql',
        'NAME': 'justjam',
    }
}

MySQL でデータベースを作成し、migrate を実行します。

% mysql
mysql> create database justjam;
mysql> exit
% pip install mysql
% python manage.py migrate

Django のテーブルが作成されていることを確認します。

% mysql
mysql> show tables
    -> ;
+----------------------------+
| Tables_in_justjam          |
+----------------------------+
| auth_group                 |
| auth_group_permissions     |
| auth_permission            |
| auth_user                  |
| auth_user_groups           |
| auth_user_user_permissions |
| django_admin_log           |
| django_content_type        |
| django_migrations          |
| django_session             |
+----------------------------+
10 rows in set (0.00 sec)

mysql> exit;

管理ユーザーの作成

% python manage.py createsuperuser
Username (leave blank to use 'username'): admin
Email address: admin@justjam.com
Password: 
Password (again): 
Superuser created successfully.

【Django】独自 ID の自動インクリメントが行われていなかった

問題

Django でモデルを扱う際、デフォルトで作成される id ではないプライマリキーで管理をしたかったので、なんとなくネットでささっと見た情報で 「models.AutoField(primary_key=True)」を設定していました。

ただ、データベース上での設定が正しくできていなかった様で自動インクリメントが機能していませんでした。

そのため Django の admin 画面からデータを登録する際、すでに存在する値が id として登録されていました。

ちなみに、models.py では「models.AutoField(primary_key=True)」となっていたもののテーブル上ではカラムがプライマリキーになっていなかったため、エラーにもなっていませんでした。

修正作業

この時点でのテーブルは下記の状態。「id」カラムを修正していきます。

mysql> describe ig_mst_product;
+-------------------+--------------------------+------+-----+---------+-------+
| Field             | Type                     | Null | Key | Default | Extra |
+-------------------+--------------------------+------+-----+---------+-------+
| id                | int(6) unsigned zerofill | NO   |     | 000000  |       |
| product_nm        | varchar(60)              | YES  |     | NULL    |       |
| product_category  | varchar(20)              | YES  |     | NULL    |       |
| brand_cd          | int(6) unsigned zerofill | YES  |     | NULL    |       |
| product_url       | text                     | YES  |     | NULL    |       |
| product_image_url | text                     | YES  |     | NULL    |       |
+-------------------+--------------------------+------+-----+---------+-------+

プライマリキーとして設定

既存の値が再度使われてしまうのはおかしいので MySQL 上でプライマリキーを設定。

mysql> alter table ig_mst_brand add primary key (id);

値の重複の解決

admin 画面からレコードを追加すると「1062, "Duplicate entry '000000' for key 'ig_mst_product.PRIMARY'"」という感じで値の重複が発生。

「000000」を id として使用しようとしているので、おそらく値の自動インクレメントがされていない状態

デフォルト値の設定削除

デフォルト値として「000000」を設定していたのでとりあえずなくしてみます。

mysql> alter table ig_mst_product alter id drop default;

auto_increment の設定

その上で MySQL でカラムに auto_increment を設定しようとしたところ、また値の重複で拒否されました。

mysql> alter table テーブル名 modify カラム名 int(6) unsigned zerofill not null auto_increment;
ERROR 1062 (23000): ALTER TABLE causes auto_increment resequencing, resulting in duplicate entry '000001' for key 'テーブル名.PRIMARY'

この時点で id カラムには「000000」〜「000069」まで存在していたにもかかわらず、なぜか「000001」を使おうとして重複エラーが返ってきている状況。

調べてみたところ、既存データの対象のカラムの中に 0 もしくは -1 以下の値が入っているとこのエラーになる様です。既存データで「000000」が存在したのでこれを別の値に変更して再度実行したら成功しました。

mysql> alter table ig_mst_product modify id int(6) unsigned zerofill not null auto_increment;
Query OK, 69 rows affected, 2 warnings (0.04 sec)
Records: 69  Duplicates: 0  Warnings: 2

正しい設定をまとめると…

Id として使用するカラムがデータベース上で下記の条件を満たす必要がある様です。

  • デフォルト値なし
  • プライマリキーとして設定
  • AUTO_INCREMENT を設定
    • 既存データで 0 もしくはそれ以下の値が入っていないことを確認

一応 Before / After も載せておきます。Key, Default, Extra が変わっています。

Before

mysql> describe ig_mst_product;
+-------------------+--------------------------+------+-----+---------+-------+
| Field             | Type                     | Null | Key | Default | Extra |
+-------------------+--------------------------+------+-----+---------+-------+
| id                | int(6) unsigned zerofill | NO   |     | 000000  |       |
| product_nm        | varchar(60)              | YES  |     | NULL    |       |
| product_category  | varchar(20)              | YES  |     | NULL    |       |
| brand_cd          | int(6) unsigned zerofill | YES  |     | NULL    |       |
| product_url       | text                     | YES  |     | NULL    |       |
| product_image_url | text                     | YES  |     | NULL    |       |
+-------------------+--------------------------+------+-----+---------+-------+

After

mysql> describe ig_mst_product;
+-------------------+--------------------------+------+-----+---------+----------------+
| Field             | Type                     | Null | Key | Default | Extra          |
+-------------------+--------------------------+------+-----+---------+----------------+
| id                | int(6) unsigned zerofill | NO   | PRI | NULL    | auto_increment |
| product_nm        | varchar(60)              | YES  |     | NULL    |                |
| product_category  | varchar(20)              | YES  |     | NULL    |                |
| brand_cd          | int(6) unsigned zerofill | YES  |     | NULL    |                |
| product_url       | text                     | YES  |     | NULL    |                |
| product_image_url | text                     | YES  |     | NULL    |                |
+-------------------+--------------------------+------+-----+---------+----------------+

Mac brew install mysqlclient でエラー pip install mysql で成功

環境

OS: macOS Big Sur バージョン 11.2.2
Python: Python 3.8.3

brew install mysqlclient でエラー

Python 仮想環境で「brew install mysqlclient」を実行したら下記のエラーが発生しました。結論から言うと「pip install mysqlclient」の間違いだったことに後で気づきました。

が、それに気づく前に「pip install mysql」を実行して無事解決したので書いておきます。

エラーログ

==> Searching for similarly named formulae...
These similarly named formulae were found:
mysql-client                                                                                  mysql-client@5.7
To install one of them, run (for example):
  brew install mysql-client
Error: No available formula or cask with the name "mysqlclient".
==> Searching for a previously deleted formula (in the last month)...
Error: No previously deleted formula found.
==> Searching taps on GitHub...
Error: No formulae found in taps.

とりあえず一番初めのエラー「Error: No available formula or cask with the name "mysqlclient".」が気になる。

調べてみると、「pip install mysql」を実行してその後に「pip install mysqlclient」を実行するという書き込みを発見。

ただ実際は「pip install mysql」だけで成功しました。

pip install mysql で成功

仮想環境内でコマンド「pip install mysql」を実行。

(venv) % pip install mysql
Collecting mysql
  Downloading https://files.pythonhosted.org/packages/bf/5f/b574ac9f70811df0540e403309f349a8b9fa1a25d3653824c32e52cc1f28/mysql-0.0.2.tar.gz
Collecting mysqlclient (from mysql)
  Downloading https://files.pythonhosted.org/packages/3c/df/59cd2fa5e48d0804d213bdcb1acb4d08c403b61c7ff7ed4dd4a6a2deb3f7/mysqlclient-2.0.3.tar.gz (88kB)
     |████████████████████████████████| 92kB 5.7MB/s 
Installing collected packages: mysqlclient, mysql
  Running setup.py install for mysqlclient ... done
  Running setup.py install for mysql ... done
Successfully installed mysql-0.0.2 mysqlclient-2.0.3
WARNING: You are using pip version 19.2.3, however version 21.0.1 is available.
You should consider upgrading via the 'pip install --upgrade pip' command.
(venv) %

「pip install mysql」で mysqlclient もカバーされている?

ログをみるとちらほら「mysqlclient」について書かれているのが見えると思います。

そして「Successfully installed mysql-0.0.2 mysqlclient-2.0.3」の一文も。これってストレートに「mysql と mysqlclient が無事インストールされました。」ってことですよね?

% pip install mysqlclient

なので python を立ち上げて「import MySQLdb」を実行してみたら特にエラーも出ず。

「pip install mysqlclient」を実行しても何も変わらず

ここまで来てやっと「brew install 〜」ではなく「pip install 〜」を実行すべきだったと気づいて「pip install mysqlclient」を実行してみましたが、すでに完了しているとのことで何も変わりませんでした。

(venv) % pip install mysqlclient
Requirement already satisfied: mysqlclient in /Users/ユーザー名/PythonProjects/sample_venv/lib/python3.8/site-packages (2.0.3)
(venv) %

Mac ローカルにインストールした MySQL を使う方法

  1. MySQL を起動する
  2. MySQL に接続する
  3. MySQL を遮断する
  4. MySQL を終了する

1. MySQL を起動する

コマンド「brew services start mysql」でローカルの MySQL を起動します。

% brew services start mysql
==> Successfully started `mysql` (label: homebrew.mxcl.mysql)

2. MySQL に接続する

コマンド「mysql -u root」で MySQL に接続します。

% mysql -u root
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 9
Server version: 8.0.23 Homebrew

Copyright (c) 2000, 2021, Oracle and/or its affiliates.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql>

3. MySQL を遮断する

コマンド「exit」で MySQL のコマンドプロンプトから出ます。

mysql> exit
Bye

4. MySQL を終了する

コマンド「brew services stop mysql」で終了です。

% brew services stop mysql
==> Successfully stopped `mysql` (label: homebrew.mxcl.mysql)

import MySQLdb で実際にクエリを実行できるか確認する

mysqlclient をインストールしたので、実際にクエリを実行できるか簡単に確認します。

今回は WordPress で自動作成されたテーブルで、現在公開されている記事のタイトルを select しました。

% python3
Python 3.9.0 (default, Jan  3 2021, 10:29:59) 
[GCC 4.2.1 20070831 patched [FreeBSD]] on freebsd9
Type "help", "copyright", "credits" or "license" for more information.

>>> import MySQLdb
>>> conn = MySQLdb.connect(host='mysqlXXX.db.sakura.ne.jp', db='abcabc_notemite',user='abcabc',passwd='xxxxxxxxxxxx',charset='utf8mb4')
>>> c = conn.cursor()
>>> c.execute('select post_title from nm_posts where post_status = "publish"')
20
>>> c.fetchall()
(('サンプルページ',), ('【Wordpress】ショートコードを別ファイル管理でリスク低減',), ('【WordPress】ショートコードの表示・非表示を一発で切り替えたい',), ('【ターミナル】Mac のログインシェルを zsh に変える',), ('【Python】Macbook に Python3 をインストール',), ('【Python】「pip install requests」でエラー「pip3 install requests」で成功',), ('【Python】Mac に Beautifulsoup4 をインストール',), ('Homebrew を Macbook Pro へインストール',), ('Homebrew を使って Macbook に Wget をインストール',), ('【Wordpress】確かに「--」と書いたのに「–」と表示される件',), ('【8ステップでがんばる】さくらレンタルサーバーでWordPressサイト立ち上げ',), ('【ターミナル】さくらのレンタルサーバーにSSHでログインする方法',), ('さくらレンタルサーバに Python3 をインストールする方法',), ('【Python】さくらレンタルサーバーに requests モジュールをインストール',), ('【Python】Mac に MySQL モジュールをインストールする方法',), ('【2020年10月〜12月】YouTube 急上昇入りランキング',), ('さくらレンタルサーバーに libffi をインストールする',), ('【2020年1月〜】YouTube 急上昇入りランキング',), ('さくらレンタルサーバーで Python3 を再インストール',), ('【Python3】mysqlclient のために _ctypes と格闘した記録',))
>>> 

execute() の実行結果(上記「20」の部分)はクエリの結果として返されたレコード数を表示しています。fetchall() を実行しないとクエリの実行結果は見れないんですね。

【Python3】_ctypes と libffi のインストールに苦しんだ記録

経緯

さくらのレンタルサーバ上で MySQL のデータベースを使えるので、それを Python で操作したいと思ったのが始まりです。

mysqlclient を使えば良いらしかったので「pip3 install mysqlclient」を実行するも、エラーが出て成功しない。

なんとかインストールを終えるまでの過程が文系の私には?だいぶややこしかったのでその記録を書き留めます。

  1. 問題発生1(No module named '_ctypes')
  2. 調査と原因1(libffi が無い)
  3. libffi をインストール(sudo は使えない)
  4. 問題発生2(INFO: Could not locate ffi libs and/or headers)
  5. 調査と原因2(./configure 文の修正)
  6. 解決(mysqlclient インストール成功)

問題発生1(No module named '_ctypes')

上記の通り「pip3 install mysqlclient」を実行したところ、下記のエラーが出てインストールが失敗しました。

ModuleNotFoundError: No module named '_ctypes'

直訳すると「_ctypes モジュールがありません」

_ctypes モジュールとはなんぞや。そしてそれはどこにあるのでしょうか。。。

調査と原因1(libffi が無い)

取り敢えず知識ゼロから出来ることは、情報の海に身を投げてなんとなくわかるまで溺れること。。。調べた結果断片的ですが自分の理解できる範囲で下記の情報が得られました。

  • _ctypes と libffi
    • _ctypes は C 言語で書かれたライブラリを Python から利用するためのモジュールであり、libffiに依存している。
    • ソースからPythonをビルドする際、libffiが見つからない場合は _ctypes のビルドはスキップされる。
    • _ctypes がビルドされていない Python から _ctypes を利用しようとすると、当該エラー(ModuleNotFoundError: No module named '_ctypes')が発生する。
  • 原因
    • つまり、libffi がインストールされていない状態で Python をインストールしたため、_ctypes のビルドがされていなかった。
  • 解決方法
    • 当該エラーを解消するには、libffi をインストールした上で Python を再ビルド・再インストールする必要がある。

libffi をインストール(sudo は使えない)

「sudo apt-get install -y libffi-dev」というコマンドが出てきましたが、さくらのレンタルサーバー(スタンダード)では管理者権限が与えられていないので sudo が使えません。

% sudo apt-get install -y libffi-dev
/usr/local/bin/sudo: Permission denied.

この通り、拒否されてしまいます。諸々調べた結果下記の方法でインストール出来ました。

問題発生2(INFO: Could not locate ffi libs and/or headers)

めでたく libffi をインストール出来たので Python をインストールし直しました。が、念のため make のログを確認したところ下記を発見。

INFO: Could not locate ffi libs and/or headers

Failed to build these modules:
_ctypes               _zoneinfo 

直訳すると「ffi libs およびヘッダーが見つからず _ctypes モジュールと _zoneinfo モジュールをビルド出来ませんでした。」とのこと。ショック。

しょうがないのでとりあえず続けて make install しましたが、当然 _ctypes モジュールが無いので「pip3 install mysqlclient」を実行しても前回と全く同じエラーで失敗します。

ModuleNotFoundError: No module named '_ctypes'

調査と原因2(./configure 文の修正)

また海に放り出されました。今回は「INFO: Could not locate ffi libs and/or headers」の文言を頼りに調べました。

こちらのページ(英語)を見たところ、どうやら CPPFLAGS、LDFLAGS、PKG_CONFIG_PATH の設定が必要みたいです。PKG_CONFIG_PATH の設定は libffi のインストールの時に行っていたので、取り敢えず CPPFLAGS と LGFLAGS だけでやってみます。

python-3.9.0.tgz は残して他を消し、再度下記の「./configure」文で Python のインストールを試してみます。

./configure --prefix=$HOME/local/python/ --with-system-ffi LDFLAGS="-L $HOME/local/lib/" CPPFLAGS="-I $HOME/local/include/"
項目雑なメモ
--prefixPython3 をインストールする場所
--with-system-ffilibffi のシステムバージョンへの接続を有効にするスイッチ。
CPPFLAGSCプリプロセッサのオプション
include パスを入れるっぽい?
LDFLAGSリンク用のディレクトリを指定する。
lib フォルダを入れるっぽい?

解決(mysqlclient インストール成功)

上記の「./configure」文のあとで make を実行しました。そしてログを確認すると下記の通り _ctypes のエラーはなくなりました。_zoneinfo は何かよくわかりませんし特に今回必要だとも思ってないので無視します。

Failed to build these modules:
_zoneinfo

今回は「pip3 install mysqlclient」も無事成功しました。

% pip3 install mysqlclient
Collecting mysqlclient
  Using cached mysqlclient-2.0.3.tar.gz (88 kB)
Using legacy 'setup.py install' for mysqlclient, since package 'wheel' is not installed.
Installing collected packages: mysqlclient
    Running setup.py install for mysqlclient ... done
Successfully installed mysqlclient-2.0.3
WARNING: You are using pip version 20.2.3; however, version 20.3.3 is available.
You should consider upgrading via the '/home/xxxxxx/local/python/bin/python3.9 -m pip install --upgrade pip' command.
% 

【Python】Mac に MySQL モジュールをインストールする方法

環境

OS: macOS Catalina バージョン 10.15.4
Python: Python 3.8.3

やりたいこと

Python で MySQLdb モジュールを使える様にしたい。

mysql をインストール

まず Homebrew で MySQL をインストールする必要があるので「brew install mysql」を実行します。

これをしていないとこの後 pip で「mysqlclient」のインストールができません。

(scraping) % brew install mysql
Updating Homebrew...
==> Auto-updated Homebrew!
Updated 1 tap (homebrew/core).
==> New Formulae
acl2                       heksa                      nfpm
act                        httpx                      ngs
airshare                   hy                         notmuch-mutt
alsa-lib                   i686-elf-binutils          numcpp
amp                        i686-elf-gcc               oci-cli
apidoc                     idris2                     omake
arb                        infracost                  openfst
argo                       inja                       oq
argocd                     inko                       ormolu
arrayfire                  ioctl                      ory-hydra
asimov                     jerryscript                osi
asroute                    jimtcl                     osm
athenacli                  jinx                       packetbeat
austin                     jobber                     packr
awsweeper                  jsonnet-bundler            pandoc-include-code
blogc                      k9s                        pandocomatic
bombadillo                 kde-karchive               parallel-hashmap
bond                       kde-kdoctools              periscope
borgbackup                 kde-ki18n                  pipgrip
box2d                      kde-threadweaver           po4a
buildozer                  kona                       podman
c7n                        kondo                      postgresql@12
cadence                    ksync                      prometheus-cpp
carton                     kubie                      promtail
cassowary                  ladspa-sdk                 protoc-gen-go-grpc
castget                    latexindent                protoc-gen-gogo
cbc                        ldpl                       protoc-gen-gogofaster
cdktf                      leaf                       pwncat
cgl                        libaio                     python@3.7
chalk-cli                  libcouchbase@2             python@3.9
charge                     libdmx                     reg
chart-testing              libdrm                     rgf
choose-rust                libfontenc                 rqlite
chrony                     libfs                      rust-analyzer
clair                      libgccjit                  rustscan
clang-format@8             libgnt                     s2n
cli11                      libhandy                   saltwater
cloud-nuke                 libice                     scw@1
cloudformation-cli         libirecovery               sdns
cloudformation-guard       libmnl                     server-go
coconut                    libnetfilter-queue         shallow-backup
colfer                     libnetworkit               sheldon
commitizen                 libnfnetlink               shtools
copilot                    liboqs                     silicon
coredns                    libpciaccess               simdjson
cortex                     libpthread-stubs           skylighting
cpio                       libseccomp                 sleef
cpm                        libsm                      smlpkg
cpr                        libx11                     snap
croaring                   libxau                     so
croc                       libxaw                     solidity
cubejs-cli                 libxaw3d                   sonic
cvs-fast-export            libxcb                     sponge
datasette                  libxcomposite              sqlite-utils
dbdeployer                 libxcursor                 standardese
diskonaut                  libxdamage                 staticcheck
dnsprobe                   libxdmcp                   structurizr-cli
doctest                    libxext                    subfinder
dosbox-staging             libxfixes                  termcolor
dotnet                     libxfont                   terraform-ls
duckscript                 libxft                     terraform@0.12
eksctl                     libxi                      terrascan
eleventy                   libxinerama                tfsec
empty                      libxkbfile                 thanos
envoy                      libxmu                     toot
fava                       libxpm                     torchvision
fblog                      libxrandr                  tre-command
fennel                     libxrender                 trunk
fetch                      libxres                    ugrep
flank                      libxscrnsaver              uptoc
fleet-cli                  libxshmfence               usb.ids
flit                       libxt                      util-macros
folderify                  libxtst                    vapor
font-util                  libxv                      vcpkg
foreman                    libxvmc                    vgrep
fpart                      libxxf86dga                vivid
fpdns                      libxxf86vm                 vlang
functionalplus             litecli                    volk
gateway-go                 localstack                 vsearch
gcc@9                      logcli                     vtk@8.2
git-hooks-go               loki                       webify
git-hound                  lunchy                     wgcf
gitql                      lunchy-go                  wownero
gitui                      macos-trash                wren
gluon                      mandown                    wren-cli
go@1.14                    mariadb@10.4               x86_64-elf-gdb
gofish                     marked                     xcb-proto
golangci-lint              mask                       xdpyinfo
gostatic                   matplotplusplus            xorgproto
gradle-profiler            mhonarc                    xtrans
gravity                    microplane                 yj
grpcui                     naabu                      z.lua
gulp-cli                   nanorc                     zoxide
halide                     nest                       zsh-you-should-use
hashlink                   networkit
hasura-cli                 never
==> Updated Formulae
Updated 4645 formulae.
==> Renamed Formulae
elasticsearch@6.8 -> elasticsearch@6
gst-validate -> gst-devtools
interactive-rebase-tool -> git-interactive-rebase-tool
jfrog-cli-go -> jfrog-cli
kibana@6.8 -> kibana@6
mkl-dnn -> onednn
==> Deleted Formulae
baidupcs-go         elasticsearch@5.6   marathon-swift      unravel
biogeme             highlighting-kate   python              urbit
cargo-completion    i386-elf-grub       residualvm          wpscan
cryptopp            kibana@5.6          sflowtool           xu4
elasticsearch@2.4   lumo                tomee-jax-rs

==> Downloading https://homebrew.bintray.com/bottles/openssl%401.1-1.1.1h.catali
==> Downloading from https://d29vzk4ow07wi7.cloudfront.net/4e5357c0cfd55cfa4ef0b
######################################################################## 100.0%
==> Downloading https://homebrew.bintray.com/bottles/protobuf-3.13.0.catalina.bo
==> Downloading from https://d29vzk4ow07wi7.cloudfront.net/af2990ba0a6e5588ef2af
######################################################################## 100.0%
==> Downloading https://homebrew.bintray.com/bottles/mysql-8.0.21_1.catalina.bot
==> Downloading from https://d29vzk4ow07wi7.cloudfront.net/81e92d0df39edaff415e7
######################################################################## 100.0%
==> Installing dependencies for mysql: openssl@1.1 and protobuf
==> Installing mysql dependency: openssl@1.1
==> Pouring openssl@1.1-1.1.1h.catalina.bottle.tar.gz
==> Caveats
A CA file has been bootstrapped using certificates from the system
keychain. To add additional certificates, place .pem files in
  /usr/local/etc/openssl@1.1/certs

and run
  /usr/local/opt/openssl@1.1/bin/c_rehash

openssl@1.1 is keg-only, which means it was not symlinked into /usr/local,
because macOS provides LibreSSL.

If you need to have openssl@1.1 first in your PATH run:
  echo 'export PATH="/usr/local/opt/openssl@1.1/bin:$PATH"' >> ~/.zshrc

For compilers to find openssl@1.1 you may need to set:
  export LDFLAGS="-L/usr/local/opt/openssl@1.1/lib"
  export CPPFLAGS="-I/usr/local/opt/openssl@1.1/include"

==> Summary
🍺  /usr/local/Cellar/openssl@1.1/1.1.1h: 8,067 files, 18.5MB
==> Installing mysql dependency: protobuf
==> Pouring protobuf-3.13.0.catalina.bottle.tar.gz
==> Caveats
Emacs Lisp files have been installed to:
  /usr/local/share/emacs/site-lisp/protobuf
==> Summary
🍺  /usr/local/Cellar/protobuf/3.13.0: 266 files, 19.8MB
==> Installing mysql
==> Pouring mysql-8.0.21_1.catalina.bottle.tar.gz
==> /usr/local/Cellar/mysql/8.0.21_1/bin/mysqld --initialize-insecure --user=ken
==> Caveats
We've installed your MySQL database without a root password. To secure it run:
    mysql_secure_installation

MySQL is configured to only allow connections from localhost by default

To connect run:
    mysql -uroot

To have launchd start mysql now and restart at login:
  brew services start mysql
Or, if you don't want/need a background service you can just run:
  mysql.server start
==> Summary
🍺  /usr/local/Cellar/mysql/8.0.21_1: 290 files, 291.2MB
==> `brew cleanup` has not been run in 30 days, running now...
Removing: /Users/xxxxxxxx/Library/Caches/Homebrew/gettext--0.20.2_1.catalina.bottle.tar.gz... (8.4MB)
Removing: /Users/xxxxxxxx/Library/Caches/Homebrew/libidn2--2.3.0.catalina.bottle.tar.gz... (233.6KB)
Removing: /Users/xxxxxxxx/Library/Caches/Homebrew/libunistring--0.9.10.catalina.bottle.tar.gz... (1.4MB)
Removing: /usr/local/Cellar/openssl@1.1/1.1.1g... (8,059 files, 18MB)
Removing: /Users/xxxxxxxx/Library/Caches/Homebrew/openssl@1.1--1.1.1g.catalina.bottle.tar.gz... (5.3MB)
Removing: /Users/xxxxxxxx/Library/Caches/Homebrew/tree--1.8.0.catalina.bottle.tar.gz... (49.5KB)
Removing: /Users/xxxxxxxx/Library/Caches/Homebrew/wget--1.20.3_2.catalina.bottle.tar.gz... (1.4MB)
Removing: /Users/xxxxxxxx/Library/Logs/Homebrew/tree... (64B)
Removing: /Users/xxxxxxxx/Library/Logs/Homebrew/wget... (64B)
Removing: /Users/xxxxxxxx/Library/Logs/Homebrew/libidn2... (64B)
Removing: /Users/xxxxxxxx/Library/Logs/Homebrew/libunistring... (64B)
Removing: /Users/xxxxxxxx/Library/Logs/Homebrew/gettext... (64B)
Removing: /Users/xxxxxxxx/Library/Logs/Homebrew/openssl@1.1... (64B)
Pruned 1 symbolic links and 2 directories from /usr/local
==> Caveats
==> openssl@1.1
A CA file has been bootstrapped using certificates from the system
keychain. To add additional certificates, place .pem files in
  /usr/local/etc/openssl@1.1/certs

and run
  /usr/local/opt/openssl@1.1/bin/c_rehash

openssl@1.1 is keg-only, which means it was not symlinked into /usr/local,
because macOS provides LibreSSL.

If you need to have openssl@1.1 first in your PATH run:
  echo 'export PATH="/usr/local/opt/openssl@1.1/bin:$PATH"' >> ~/.zshrc

For compilers to find openssl@1.1 you may need to set:
  export LDFLAGS="-L/usr/local/opt/openssl@1.1/lib"
  export CPPFLAGS="-I/usr/local/opt/openssl@1.1/include"

==> protobuf
Emacs Lisp files have been installed to:
  /usr/local/share/emacs/site-lisp/protobuf
==> mysql
We've installed your MySQL database without a root password. To secure it run:
    mysql_secure_installation

MySQL is configured to only allow connections from localhost by default

To connect run:
    mysql -uroot

To have launchd start mysql now and restart at login:
  brew services start mysql
Or, if you don't want/need a background service you can just run:
  mysql.server start

長々とログが出ましたが、入力待ちになったら which コマンドで mysql がインストールされたのを確認します。

(scraping) % which mysql
/usr/local/bin/mysql

/usr/local/bin/mysql にインストールされています。

MySQL の動作を確認

MySQL を起動

「mysql.server start」と入力して MySQL を起動してみます。

% mysql.server start
Starting MySQL
.. SUCCESS! 
% 

無事起動しました。

MySQL をシャットダウン

「mysql.server stop」でシャットダウンすることができます。

% mysql.server stop
Shutting down MySQL
.. SUCCESS!
%

mysqlclient をインストール

pip で mysqlclient をインストールします。

(scraping) % pip install mysqlclient
Collecting mysqlclient
  Using cached https://files.pythonhosted.org/packages/a5/e1/e5f2b231c05dc51d9d87fa5066f90d1405345c54b14b0b11a1c859020f21/mysqlclient-2.0.1.tar.gz
Installing collected packages: mysqlclient
  Running setup.py install for mysqlclient ... done
Successfully installed mysqlclient-2.0.1
WARNING: You are using pip version 19.2.3, however version 20.2.3 is available.
You should consider upgrading via the 'pip install --upgrade pip' command.
(scraping) % 

MySQLdb モジュールが使えることを確認

Python で MySQLdb モジュールがインポートできることを確認して完了です。

>>> import MySQLdb
>>>