🛸
【Python】MariaDB連携
mariaDBでの動作確認
参照:
sudo apt-get install apt-transport-https curl
sudo curl -o /etc/apt/trusted.gpg.d/mariadb_release_signing_key.asc 'https://mariadb.org/mariadb_release_signing_key.asc'
sudo sh -c "echo 'deb https://mirrors.xtom.jp/mariadb//ubuntu focal main' >>/etc/apt/sources.list"
sudo apt-get update
sudo apt-get install mariadb-server
sudo apt info mariadb-server
sudo apt install mariadb-server
sudo mariadb
これ表示できれば成功
MariaDB [(none)]> select version();
+-------------------------------------+
| version() |
+-------------------------------------+
| 10.6.8-MariaDB-1:10.6.8+maria~focal |
+-------------------------------------+
1 row in set (0.000 sec)
データベースを作成
CREATE DATABASE db_test;
Python
- Pythonで仮想環境に入る
pip install mysql-connector-python
import mysql.connector
# コネクション作成
conn = mysql.connector.connect(
host='localhost',
user='root',
password='12345',
database='db_test'
)
# 接続状況確認
print(conn.is_connected())
# カーソル作成
cur = conn.cursor()
# 1件取得
cur.execute("select version()")
print(cur.fetchone())
# DB操作終了
cur.close()
接続成功
True
('10.4.17-MariaDB',)
Discussion