環境
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 >>>