mysql-connector-python は、MySQL によって提供される Python コネクタです。 C 言語の標準関数ライブラリに依存せず、より便利に使用できます。このトピックでは、Python を使用してアプリケーションを開発する場合に、mysql-connector-python を使用して LindormTable に接続する方法について説明します。
前提条件
Python 3.8 以降のバージョンがインストールされていること。
インスタンスで MySQL 互換機能が有効になっていること。詳細については、「MySQL 互換機能を有効にする」をご参照ください。
クライアントの IP アドレスが Lindorm インスタンスのホワイトリストに追加されていること。詳細については、「ホワイトリストを構成する」をご参照ください。
手順
バージョン 8.0.11 の mysql-connector-python をインストールします。
pip install mysql-connector-python==8.0.11
コマンドを実行して mysql-connector-python をインストールすることもできます。接続を確立し、パラメータを構成します。
connection = mysql.connector.connect(host='<LindormTable endpoint for MySQL.>', port=33060, user='<Username>', passwd='<Password>', database='<Database name>') cursor = connection.cursor(prepared=True)
パラメータ
passwd
パラメータ
説明
host
コロンとポート番号(
:33060
)が削除された Lindormtable の Mysql エンドポイント。エンドポイントの取得方法の詳細については、「エンドポイントを表示する」をご参照ください。重要アプリケーションが Elastic Compute Service(ECS)インスタンスにデプロイされており、ECS インスタンスが Lindorm インスタンスと同じ VPC 内にある場合は、VPC 経由で Lindorm インスタンスに接続することをお勧めします。それ以外の場合は、インターネット経由で Lindorm インスタンスに接続します。インターネット経由で Lindorm インスタンスに接続するには、インスタンスのパブリックエンドポイントを有効にする必要があります。詳細については、「LindormSearch をアクティブ化する」をご参照ください。
port
MySQL を使用して LindormTable に接続するために使用するポート。このパラメータの値は
33060
に固定されています。user
パスワードを忘れた場合は、LindormTable のクラスタ管理システムでパスワードを変更できます。詳細については、「ユーザーの管理」をご参照ください。
LindormTable に接続するために使用するパスワード。
database
接続先のデータベースの名前。デフォルトでは、クライアントは default という名前のデータベースに接続されます。
LindormTable SQL を使用して LindormTable で操作を実行します。次のコードブロックは、LindormTable SQL を使用してテーブルを作成する例を示しています。
sql_create_table = ("create table if not exists test_python(c1 integer, c2 integer, c3 varchar, primary key(c1))") print(sql_create_table) cursor.execute(sql_create_table)
完全なサンプルコード
次のコードは、Java Low Level REST Client を使用して LindormSearch に接続し、LindormSearch で操作を実行する完全な例を示しています。
#!/usr/bin/python
# -*- coding: UTF-8 -*-
import json
import mysql.connector
# データベース接続を確立します。
# host パラメータを LindormTable の MySQL エンドポイントに設定します。
# port パラメータを、MySQL を使用して LindormTable に接続するために使用するポート(デフォルトでは 33060)に設定します。
# user パラメータを、LindormTable に接続するために使用するユーザー名に設定します。
# passwd を、LindormTable に接続するために使用するパスワードに設定します。
# database パラメータを、LindormTable のデータベース名に設定します。
connection = mysql.connector.connect(host='ld-bp1hn6yq0yb34****-proxy-sql-lindorm-public.lindorm.rds.aliyuncs.com', port=33060,user='root', passwd='test',database='default')
# カーソルを作成し、prepared=True 条件を指定します。
cursor = connection.cursor(prepared=True)
# テーブルを作成します。
sql_create_table = ("create table if not exists test_python(c1 integer, c2 integer, c3 varchar, primary key(c1))")
print(sql_create_table)
cursor.execute(sql_create_table)
# テーブルにデータを挿入します。
sql_upsert = "upsert into test_python(c1, c2, c3) values(?, ?, ?)"
print(sql_upsert)
# テーブルに単一行を挿入します。
cursor.execute(sql_upsert, (1, 1, '1'))
cursor.execute(sql_upsert, (2, 2, json.dumps({"key": "value2"})))
# 複数の行を同時にテーブルに挿入します。
data = [
(3, 3, '3'),
(4, 4, json.dumps({"key": "value4"})),
]
cursor.executemany(sql_upsert, data)
# テーブルからデータを削除します。
sql_delete = "delete from test_python where c1 = ?"
print(sql_delete)
cursor.execute(sql_delete, (3,))
# テーブル内のデータを変更します。
sql_update = "upsert into test_python(c1, c2, c3) values(?, ?, ?)"
print(sql_update)
cursor.execute(sql_update, (1, 2, '2'))
# テーブル内の指定されたデータをクエリします。
sql_select = "select * from test_python where c1 = ?"
print(sql_select)
cursor.execute(sql_select, (4,))
rows = cursor.fetchall()
print(rows)
# テーブル内のすべてのデータをクエリします。
sql_select_all = "select * from test_python"
print(sql_select_all)
cursor.execute(sql_select_all)
rows = cursor.fetchall()
print(rows)
# カーソルを閉じます。
cursor.close()
# データベースへの接続を閉じます。
connection.close()
テーブルに書き込む文字列に二重引用符(")などの特殊文字が含まれている場合は、Lindorm に書き込まれるデータにエスケープ文字が追加されないように、prepared=True
条件(上記のサンプルコードに示すように)を指定することをお勧めします。