MySQL 8 は MySQL 5 で動いていたプログラムをそのまま使うとエラーになってしまいます。
そして、MySQL 8 に対応した mysql-connector-python 8.x.x を使うと、今度は MariaDB に接続できなくなってしまいました。
そして、こんなエラーが出ます。
mysql.connector.errors.ProgrammingError: Character set ‘utf8’ unsupported
いろいろ試してみましたが解決しないのでググってみたら英語の情報で解決方法が出ていました。
つまり、mysql-connector-python ではなく mariadb というモジュールを使うとよさそうです。
まず、pip install mariadb でこのモジュールをインストールします。
インストールが成功したら、こんな感じでテストプログラムを実行してみたらうまく動きました。
#!/usr/bin/python3
import mariadb
conn = mariadb.connect(user='user', password='??????', host='localhost', database='user')
cur = conn.cursor()
cur.execute("select count(*) from Pictures;")
for row in cur.fetchall():
print(row[0])
cur.close
conn.close
なお、古いバージョンの mysql-connector (v2.2)を使えば、ソースを変えずにそのままアクセスできます。
インストール方法は次の通りですが、警告メッセージが表示されます。
pip install mysql-connector
警告メッセージ
DEPRECATION: mysql-connector is being installed using the legacy ‘setup.py install’ method, because it does not have a ‘pyproject.toml’ and the ‘wheel’ package is not installed. pip 23.1 will enforce this behaviour change. A possible replacement is to enable the ‘–use-pep517’ option. Discussion can be found at https://github.com/pypa/pip/issues/8559