MariaDB Connector/Python を Ubuntu 19.10 Eoan Ermine で使う


概要

  • Ubuntu 19.10 Eoan Ermine に MariaDB Connector/Python をインストールする
  • Python による MariaDB Connector/Python を使用したサンプルコードを示す

今回の環境

  • Ubuntu 19.10 Eoan Ermine
  • MariaDB 10.3.22
  • Python 3.7.5
  • MariaDB Connector/Python 0.9.54-beta

MariaDB Connector/Python とは

MariaDB Connector/Python は MariaDB や MySQL にアクセスするための MariaDB 公式ライブラリ。 Python DB API 2.0 (PEP-249) に準拠している。

Python - MariaDB Knowledge Base

MariaDB Connector/Python enables python programs to access MariaDB and MySQL databases, using an API which is compliant with the Python DB API 2.0 (PEP-249). It is written in C and uses MariaDB Connector/C client library for client server communication.

MariaDB Connector/Python は現時点 (2020年3月6日現在) ではまだベータ版。

MariaDB Connector/Python 0.9.54-beta Release Notes - MariaDB Knowledge Base

This is a beta release of the MariaDB Connector/Python and not intended for production use.

Do not use beta releases in production!

MariaDB Connector/Python 0.9.52 Alpha リリース

これまでは MySQL Connector が流用されていましたが,他のプログラミング言語と同様に MariaDB Corporation から正式に Connector が提供されることになります。

MariaDB Connector/Python 0.9.52 Alpha リリース

使用方法は既存の MySQL Connector と同様です。

pip3 コマンドのインストール

MariaDB Connector/Python を pip3 でインストールするため、pip3 をインストールしておく。

$ sudo apt install python3-pip

Ubuntu の pip3 コマンドは独自のカスタマイズが施されているとのこと。

pip - python.jp

Debian パッケージの pipコマンド (python3-pip/python-pip) は独自の修正が加えられており、特権ユーザではない、一般ユーザとして pip install コマンドを実行すると、自動的に --user オプションが指定されたのもとして実行します。

MariaDB Connector/Python のインストール

pip3 コマンドで MariaDB Connector/Python のパッケージである mariadb をインストールする。

$ pip3 install --pre mariadb

インストール時に以下のようなエラーが発生した場合は mariadb_config が足りていないので、

    /bin/sh: 1: mariadb_config: not found
    Traceback (most recent call last):
      File "<string>", line 1, in <module>
      File "/tmp/pip-install-o123mtwk/mariadb/setup.py", line 24, in <module>
        cfg = get_config(options)
      File "/tmp/pip-install-o123mtwk/mariadb/mariadb_posix.py", line 49, in get_config
        cc_version = mariadb_config(config_prg, "cc_version")
      File "/tmp/pip-install-o123mtwk/mariadb/mariadb_posix.py", line 27, in mariadb_config
        "mariadb_config not found.\nPlease make sure, that MariaDB Connector/C is installed on your system, edit the configuration file 'site.cfg' and set the 'mariadb_config'\noption, which should point to the mariadb_config utility.")
    OSError: mariadb_config not found.
    Please make sure, that MariaDB Connector/C is installed on your system, edit the configuration file 'site.cfg' and set the 'mariadb_config'
    option, which should point to the mariadb_config utility.

    ----------------------------------------
Command "python setup.py egg_info" failed with error code 1 in /tmp/pip-install-o123mtwk/mariadb/

Ubuntu の libmariadb-dev パッケージ (mariadb_config を同梱している) をインストールしてから、再度 pip3 コマンドで mariadb パッケージをインストールする。

$ sudo apt install libmariadb-dev
$ pip3 install --pre mariadb

Ubuntu – eoan の libmariadb-dev パッケージに関する詳細

MariaDB database development files

Ubuntu – パッケージのファイル一覧: libmariadb-dev/eoan/amd64

/usr/bin/mariadb_config

MariaDB Connector/Python のドキュメント

MariaDB のドキュメントだけではわかりにくい。必要に応じて GitHub にあるソースコードや MySQL Connector/Python のドキュメントも参考にする。

insert, select, update のサンプルコード

以下のような Python スクリプトを動作させることができる。

import time
import mariadb

# connect db
conn = mariadb.connect(host="localhost",
                       user="your_app",
                       password="your_app_password",
                       database="test_db")
cursor = conn.cursor(named_tuple=True)

# insert
insert_sql = "INSERT INTO test_table (name, created_at) VALUES (%s, %s)"
name = "寿司ビール🍣🍺𩸽🐟🐠🐡文字化けしないで"
created_at = mariadb.TimestampFromTicks(time.time())
cursor.execute(insert_sql, (name, created_at))
conn.commit()

# select
select_sql = "SELECT id, name, created_at FROM test_table"
cursor.execute(select_sql)
for row in cursor:
  print(f"{row.id}: {row.name} ({row.created_at})")
  target_id = row.id

# update
update_sql = "UPDATE test_table set name=%s WHERE id=%s"
cursor.execute(update_sql, ("Alice", target_id))
conn.commit()

conn.close()

参考資料