目的
・Ubuntu 18.04に mariadb10.5 をインストールする
・デモDB nation をインストールする
・python + mysql-connector-python を使ったサンプルコードを書いてみる
Repositoriesを追加する
https://mariadb.com/kb/en/mariadb-package-repository-setup-and-usage/より
$ curl -LsS https://downloads.mariadb.com/MariaDB/mariadb_repo_setup | sudo bash
[info] Repository file successfully written to /etc/apt/sources.list.d/mariadb.list
[info] Adding trusted package signing keys...
[info] Running apt-get update...
[info] Done adding trusted package signing keys
Repositoryに以下の3サイトが登録される
※ワーニング表示抑制のため [arch=amd64] を追加している
$ sudo cat /etc/apt/sources.list.d/mariadb.list
# MariaDB Server
# To use a different major version of the server, or to pin to a specific minor version, change URI below.
deb [arch=amd64] http://downloads.mariadb.com/MariaDB/mariadb-10.5/repo/ubuntu bionic main
# MariaDB MaxScale
# To use the latest stable release of MaxScale, use "latest" as the version
# To use the latest beta (or stable if no current beta) release of MaxScale, use "beta" as the version
deb [arch=amd64] http://downloads.mariadb.com/MaxScale/2.4/ubuntu bionic main
# MariaDB Tools
deb [arch=amd64] http://downloads.mariadb.com/Tools/ubuntu bionic main
mariadbをインストールする
$ sudo apt update
$ sudo apt install mariadb-server mariadb-client
※サービス名は /lib/systemd/system/mariadb.service
文字コードを設定する
/etc/mysql/mariadb.conf.d/50-server.cnf
character-set-server = utf8mb4
#collation-server = utf8mb4_general_ci 修正前
collation-server = utf8mb4_bin 修正後
#bind-address = 127.0.0.1 修正前
bind-address = 0.0.0.0 修正後
/etc/mysql/mariadb.conf.d/50-client.cnf
default-character-set = utf8mb4 コメントアウト
※修正後、再起動する
$ sudo systemctl restart mariadb
$ mysql -u root -p
Enter password:
~
MariaDB [(none)]> show variables like "chara%";
+--------------------------+----------------------------+
| Variable_name | Value |
+--------------------------+----------------------------+
| character_set_client | utf8mb4 |
| character_set_connection | utf8mb4 |
| character_set_database | utf8mb4 |
| character_set_filesystem | binary |
| character_set_results | utf8mb4 |
| character_set_server | utf8mb4 |
| character_set_system | utf8 |
| character_sets_dir | /usr/share/mysql/charsets/ |
+--------------------------+----------------------------+
8 rows in set (0.001 sec)
※アクセス用ユーザーを追加する
MariaDB [(none)]>GRANT ALL PRIVILEGES ON *.* TO demo IDENTIFIED BY 'passwd' WITH GRANT OPTION;
デモDB nation をインストールする
MariaDB Sample DatabaseよりDLした nation.zip を解凍する
$ mysql -u root -p
Enter password:
~
MariaDB [(none)]> source /path/to/nation.sql
~
MariaDB [nation]> use nation;
Database changed
MariaDB [nation]> show tables;
+-------------------+
| Tables_in_nation |
+-------------------+
| continents |
| countries |
| country_languages |
| country_stats |
| guests |
| languages |
| region_areas |
| regions |
| vips |
+-------------------+
mysql-connector-python をインストールする
$ sudo pip3 install mysql-connector-python
サンプルコード
# Windows Add env PYTHONIOENCODING = UTF-8 & restart vscode
# coding:utf-8
import mysql.connector as mydb
# コネクションの作成
conn = mydb.connect(
host='192.168.5.xxx',
port='3306',
user='demo',
password='passwd',
database='nation'
)
cur = conn.cursor()
query = "SELECT * FROM guests"
cur.execute(query)
print("all:")
rows = cur.fetchall()
for row in rows:
print(row)
cur.close()
conn.close()
参考にしたサイトはこちら
Ubuntu 18.04 LTS に MariaDB 10.4 をインストール
Ubuntu18.04にMariaDBを導入する
MySQLの文字コードとCollation
MariaDBのインストール/初期設定 [CentOS7]
ユーザーの作成
MySQLで「ERROR 2003 (HY000): Can’t connect to MySQL server」と怒られた時の対処法