MySQL Connector/Python 2.1をAmazon Linuxにインストールする

2017.04.21

この記事は公開されてから1年以上経過しています。情報が古い可能性がありますので、ご注意ください。

PythonからMySQLに接続するためのドライバーは多数存在します。 今回はMySQLを開発しているOracle社謹製のConnector/Pythonを使い

  • ピュアPython版
  • C拡張版

のドライバーをソースコードからAmazon Linux上のPython2.7にインストールする方法を紹介します。

MySQL Connector/Python の特徴

  • Python DB-API(2.0)互換
  • Oracle 謹製でアクティブに開発されている
  • 最新のMySQL 5.7を含めたMySQLのほとんどの機能が利用可能
  • Python2/3の両方に対応
  • ピュアPythonで実装されているため、インストールが楽
  • 大きなリザルトセットを返すクエリーで性能面が優れたC拡張版も存在(2.1.1から)

MySQL Connector/Python のバージョンの違い

Connector/Pythonのバージョンによって以下のような違いが存在します。

Connector/Python Version MySQL Server Versions Python Versions Connector Status
2.2 5.7, 5.6, 5.5 3.3 and higher, 2.7 Developer Milestone
2.1 5.7, 5.6, 5.5 3.3 and higher, 2.7, 2.6 Recommended version
2.0 5.7, 5.6, 5.5 3.3 and higher, 2.7, 2.6 GA, Supported
1.2 5.7, 5.6, 5.5 (5.1, 5.0, 4.1) 3.1 and higher, 2.7, 2.6 GA, Supported

参照 https://dev.mysql.com/doc/connector-python/en/connector-python-versions.html

今回は推奨されている 2.1 系をインストールします。

Developer Milestone2.2系ではX Protocolを利用するための X DevAPI対応、Protocol Buffers 3対応など、新しい機能が盛り込まれています。 2.2 系のインストールは別の機会に紹介予定です。

動作検証環境

以下の環境でインストールを行いました。

  • MySQL Connector/Python : 2.1.6
  • Linux : Amazon Linux 2017.03(AMI ID :amzn-ami-hvm-2017.03.0.20170417-x86_64-gp2 (ami-b968bad6))
  • Python : Python 2.7

Python2.7の環境を用意

今回はvirtualenv でPython 2.7用の環境を構築します。

$ virtualenv mysql
New python executable in mysql/bin/python2.7
Also creating executable in mysql/bin/python
Installing setuptools, pip...done.
$ source  mysql/bin/activate
(mysql)$

ソースコードの取得

今回はソースコードからインストールします。 ソースコードは

  • MySQLサイト
  • GitHubサイト

から取得可能です。

MySQL サイト

以下の URL からダウンロードします。

https://dev.mysql.com/downloads/connector/python/

ダウンロードするためには、本サイトへのログインが必要です。

Generally Available(GA) Release → Platform Independent を選ぶと、ソースコードを取得できます。

connector-python

mysql-connector-python-2.1.6.tar.gz というファイル名でダウンロードできます。 ダウンロードしたファイルはAWSのEC2サーバーに転送して下さい。

GitHub サイト

以下の URL から git clone します。

https://github.com/mysql/mysql-connector-python

$ git clone https://github.com/mysql/mysql-connector-python.git
$ cd mysql-connector-python
$ git tag
2.0.0
2.0.1
2.0.2
2.0.3
2.0.4
2.0.5
2.1.1
2.1.2
2.1.3
2.1.4
2.1.5
2.1.6
2.2.0
2.2.1-m2
2.2.2
2.2.3
init
$ git checkout 2.1.6
HEAD is now at 94f2135... Add Python 3.4 and 3.5 as supported versions
$

git がインストールされていない場合は $ git install -y git でインストールして下さい。

ピュアPython版のインストール

次のページの手順に従いインストールします。

https://dev.mysql.com/doc/connector-python/en/connector-python-installation-source.html

Pythonでself-contained にかかれているため、インストールではまる可能性は低いはずです。

$ python setup.py install
...
running install_egg_info
Writing /home/ec2-user/mysql/lib/python2.7/site-packages/mysql_connector_python-2.1.6-py2.7.egg-info

インストールの確認

$ python -c 'import mysql.connector'
$
インストールが確認できました。

C拡張版のインストール

次のページの手順に従いインストールします。

https://dev.mysql.com/doc/connector-python/en/connector-python-cext.html

必要なパッケージのインストール

インストールでは以下のパッケージに依存します

  • mysql
  • mysql-devel
  • gcc
  • gcc-c++

これらをまとめてインストールします

$ sudo yum install -y mysql mysql-devel gcc gcc-c++

インストール

mysql_config のパスを --with-mysql-capi 引数に渡します。

$ whereis mysql_config
mysql_config: /usr/bin/mysql_config /usr/share/man/man1/mysql_config.1.gz
$ python setup.py install --with-mysql-capi=/usr/bin/mysql_config
...
running install_egg_info
Writing /home/ec2-user/mysql/lib64/python2.7/site-packages/mysql_connector_python-2.1.6-py2.7.egg-info

インストールの確認

$ python -c "import  _mysql_connector"
$

インストールが確認できました。

C拡張版インストール時のトラブルシュート

C拡張版のインストールははまりポイントが多数あります。エラーメッセージを例に、解決方法を紹介します

mysql_config がインストールされていない

mysql_config がインストールされていない場合、whereis コマンドの結果が空です

$ whereis mysql_config
mysql_config:
$

mysql をインストールして対応します

$ sudo yum install -y mysql
$ whereis mysql_config
mysql_config: /usr/bin/mysql_config /usr/share/man/man1/mysql_config.1.gz

MySQL C API location is invalid; was /usr/bin/mysql_config というエラーが発生

$ python setup.py install --with-mysql-capi=/usr/bin/mysql_config
...
running build_ext
MySQL C API location is invalid; was /usr/bin/mysql_config

mysql-devel パッケージをインストールして対応します。

$ sudo yum install -y mysql-devel

unable to execute 'gcc': No such file or directory というエラーが発生

$ python setup.py install --with-mysql-capi=/usr/bin/mysql_config
...
unable to execute 'gcc': No such file or directory
error: command 'gcc' failed with exit status 1

gcc パッケージをインストールして対応します。

$ sudo yum install -y gcc

gcc: error trying to exec 'cc1plus': execvp: No such file or directory というエラーが発生

$ python setup.py install --with-mysql-capi=/usr/bin/mysql_config
...
gcc: error trying to exec 'cc1plus': execvp: No such file or directory
error: command 'gcc' failed with exit status 1

gcc-c++ パッケージをインストールして対応します。

$ sudo yum install -y gcc-c++

まとめ

今回はMySQL Connector/Python 2.1系のピュアPython版とC拡張版のAmazon Linuxへのインストール方法を紹介しました。 C拡張版は性能面でのメリットがありますが、その分、ピュアPython版に比べてインストールは煩雑です。

Python版とC拡張版の性能比較や新機能がいろいろ盛り込まれてた2.2系のインストールについても機会があれば調査したいと思います。

参照