Raspberry Pi OS(64bit版)にMaria DBをインストールする k3sインストールの前準備

目的

k3sの外部DBとしてMariaDBを利用する目的でRaspberry Pi OSにMaria DBをインストールします。

手順

・Maria DBのインストール
・初期パスワードの設定
・DBの作成
・接続用ユーザの作成
・接続用ユーザへの権限の付与

手順詳細

・Maria DBのインストール

$ sudo apt-get update
$ sudo apt-get install -y mariadb-server 

・初期パスワードの設定

mysql_secure_installationを使って初期セットアップを行います。

$  sudo /usr/bin/mysql_secure_installation

初期状態では、rootのパスワード設定はないはずなので、空欄のままenter。

Enter current password for root (enter for none):
OK, successfully used password, moving on...

rootのパスワードを設定します。

Set root password? [Y/n] Y

New password:
Re-enter new password:
Password updated successfully!
Reloading privilege tables..
... Success!

By default, a MariaDB installation has an anonymous user, allowing anyone
to log into MariaDB without having to have a user account created for
them.  This is intended only for testing, and to make the installation
go a bit smoother.  You should remove them before moving into a
production environment.

あとは匿名ユーザを削除したり、

Remove anonymous users? [Y/n] Y
... Success!

rootユーザのリモートログインを禁止したりします。

Disallow root login remotely? [Y/n] Y

... Success!

さらにテスト用のDBも削除します。

Remove test database and access to it? [Y/n] Y
- Dropping test database...
... Success!
- Removing privileges on test database...
... Success!

権限の再読込を行って諸々の設定変更を有効にします。

Reload privilege tables now? [Y/n] Y
... Success!

接続確認をします。

$ sudo mariadb
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 61
Server version: 10.3.23-MariaDB-0+deb10u1 Debian 10
Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
MariaDB [(none)]>

無事に接続できました。

リモートから接続できるように設定を変更します。
/etc/mysql/mariadb.conf.d/50-server.cnfを開いて変更します。

bind-address = 127.0.0.1

となっている部分を、

bind-address = 0.0.0.0

に変更します。

・DBの作成

CREATE DATABASE文でk3sデータベースを作成します。

$ sudo mariadb
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 62
Server version: 10.3.23-MariaDB-0+deb10u1 Debian 10

Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

MariaDB [(none)]> CREATE DATABASE k3s;
Query OK, 1 row affected (0.001 sec)

SHOW DATABASESで、確認します。

MariaDB [(none)]> SHOW DATABASES;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| k3s                |
| mysql              |
| performance_schema |
+--------------------+
4 rows in set (0.001 sec)

無事、k3sデータベースを作成できていることが確認できました。

・接続用ユーザの作成と権限の付与

CREATE USER文と、GRANT文でk3sデータベースに対するすべての権限を保有するk3sユーザを作成します。

MariaDB [(none)]> CREATE USER 'k3s'@'%' IDENTIFIED BY '任意のパスワード';
uery OK, 0 rows affected (0.001 sec)

MariaDB [(none)]> GRANT ALL PRIVILEGES ON k3s.* TO 'k3s'@'%';
MariaDB [(none)]> FLUSH PRIVILEGES;
MariaDB [(none)]> select host,user from mysql.user;
+-----------+------+
| host      | user |
+-----------+------+
| %         | k3s  |
| localhost | root |
+-----------+------+
2 rows in set (0.001 sec)

k3sユーザで接続してみて確認します。

$ mariadb -u k3s -p設定したパスワード -h サーバのIPアドレス
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 68
Server version: 10.3.23-MariaDB-0+deb10u1 Debian 10
Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
MariaDB [(none)]> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| k3s                |
+--------------------+
2 rows in set (0.001 sec)

information_schemaとk3sデータベース以外は見えないので権限的にも特に問題なさそうです。


この記事が気に入ったらサポートをしてみませんか?