---
title: "S3 ストレージエンジン – MariaDB Enterprise Server"
publish_date: 2019-09-17
updated_date: 2023-10-12
author: "MariaDB"
tags:
  - name: "MariaDB Enterprise Server"
    url: "/ja/resources/blog/tag/mariadb-enterprise-server.md"
  - name: "S3"
    url: "/ja/resources/blog/tag/s3.md"
  - name: "Storage Engine"
    url: "/ja/resources/blog/tag/storage-engine-ja.md"
---

# S3 ストレージエンジン – MariaDB Enterprise Server

2019年6月にリリースされた [MariaDB Enterprise Server](https://staging-mdb.com/ja/resources/datasheets/mariadb-enterprise-server/) 10.4 / 10.3 では S3 ストレージエンジンが追加されています。

[Enterprise Documentation / Reference / Included Plugins / S3 Plugin](https://staging-mdb.com/docs/reference/all/plugins/S3/)  
[MariaDB Subscription Services Policies 1.03 – 2019-07-08](https://staging-mdb.com/wp-content/uploads/2019/07/mariadb-subscription-services-v1-03_policy_1035.pdf)

この S3 ストレージエンジンでは，InnoDB / Aria テーブルを Amazon S3 に簡単にバックアップ/アーカイブすることが可能です。今回はこの機能を解説いたします。

### S3 ストレージエンジン / libMariaS3

AWS の各機能を 3rd party のC++プログラムから利用するには，通常 [AWS SDK for C++](https://aws.amazon.com/sdk-for-cpp/) を利用しますが，MariaDB Server の[ライセンス](https://github.com/MariaDB/server/blob/10.4/COPYING)は GPL v2 であり，一方 AWS SDK for C++ は [Apache License 2.0](https://github.com/aws/aws-sdk-cpp/blob/master/LICENSE) であるため不整合が生じます。そこで MariaDB Corporation では [libMariaS3](https://github.com/mariadb-corporation/libmarias3) というライブラリを新規開発，LGPL 2.1 ライセンスで公開しています。  
S3 ストレージエンジンはこのライブラリを用いてAmazon S3 にアクセスするため，ビルド済バイナリが再配布可能となっています(AWS SDK for C++ を用いる AWS KMS プラグインはユーザがソースからビルドする必要があります)。

### テスト環境

執筆時点で最新版の MariaDB Enterprise Server 10.4.7-2 を用いました。

### S3 ストレージエンジンの設定

`/etc/my.cnf.d/server.cnf`に以下のような設定を行います。

```
[mariadb]
s3=ON
#s3-debug=ON
s3-bucket=test-s3-plugin-201909
s3-access-key=XXXXXXXXXXXXXXXXXXXX
s3-secret-key=xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
s3-region=us-east-1

```

ここで，s3-access-key は AWS Access key ID, s3-secret-key は Secret access key を指定します。  
なお，現状 us-east-1 リージョン(N. Virginia)しか利用できません。ap-northeast-1(Tokyo) 等を指定すると Access Denied となります。

### S3 ストレージエンジンのテスト

S3 ストレージエンジンは基本的に InnoDB / Aria テーブルを S3 バケットにバックアップ/アーカイブするためのものです。  
以下の例では，InnoDB テーブルを通常のようにローカルストレージ(@@datadir)上に作成後，`ALTER TABLE ENGINE=s3` で [s3\_bucket](https://staging-mdb.com/kb/en/library/s3-storage-engine-system-variables/#s3_bucket) パラメータで指定した S3 バケットにコピー，ローカルストレージからは .ibd ファイルがなくなります。

```
# mariadb
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 9
Server version: 10.4.7-2-MariaDB-enterprise MariaDB Enterprise Server

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 OR REPLACE DATABASE test;
Query OK, 1 row affected (0.000 sec)

MariaDB [(none)]> USE test;
Database changed

MariaDB [test]> CREATE TABLE t1 (a INT, b INT, KEY(a)) ENGINE=InnoDB;
Query OK, 0 rows affected (1.661 sec)

MariaDB [test]> INSERT INTO t1 SELECT seq,seq+10 FROM seq_1_to_10;
Query OK, 10 rows affected (0.001 sec)
Records: 10  Duplicates: 0  Warnings: 0

MariaDB [test]> SELECT * FROM t1;
+------+------+
| a    | b    |
+------+------+
|    1 |   11 |
|    2 |   12 |
|    3 |   13 |
|    4 |   14 |
|    5 |   15 |
|    6 |   16 |
|    7 |   17 |
|    8 |   18 |
|    9 |   19 |
|   10 |   20 |
+------+------+
10 rows in set (0.000 sec)

MariaDB [test]> ALTER TABLE t1 ENGINE=s3;
Query OK, 10 rows affected (1.857 sec)
Records: 10  Duplicates: 0  Warnings: 0

```

ここで，AWS CLI を用いて test-s3-plugin-201909 バケットを確認してみます。

```
$ aws s3 ls --recursive s3://test-s3-plugin-201909
2019-09-09 00:46:07       8192 test-s3-plugin-201909/test/t1/aria
2019-09-09 00:46:08      16384 test-s3-plugin-201909/test/t1/data/000001
2019-09-09 00:46:07        940 test-s3-plugin-201909/test/t1/frm
2019-09-09 00:46:07       8192 test-s3-plugin-201909/test/t1/index/000001

```

正常にオブジェクトが作成されていることが確認できました。  
この状態で t1 テーブルを確認しますと，

```
MariaDB [test]> SHOW CREATE TABLE t1 \G
*************************** 1. row ***************************
       Table: t1
Create Table: CREATE TABLE `t1` (
  `a` int(11) DEFAULT NULL,
  `b` int(11) DEFAULT NULL,
  KEY `a` (`a`)
) ENGINE=S3 DEFAULT CHARSET=latin1 PAGE_CHECKSUM=1
1 row in set (0.000 sec)

```

`ENGINE=S3` となっていることが確認できます。

t1 に対して SELECT 文によるクエリを実行することはできますが，

```
MariaDB [test]> SELECT * FROM t1;
+------+------+
| a    | b    |
+------+------+
|    1 |   11 |
|    2 |   12 |
|    3 |   13 |
|    4 |   14 |
|    5 |   15 |
|    6 |   16 |
|    7 |   17 |
|    8 |   18 |
|    9 |   19 |
|   10 |   20 |
+------+------+
10 rows in set (0.000 sec)

MariaDB [test]> insert into t1 values (20, 30);
ERROR 1036 (HY000): Table 't1' is read only

```

S3 テーブルは read only であるため，書込はできません。/var/lib/mysql/test (@@datadir/test) ディレクトリを確認しますと，

```
# ls -l /var/lib/mysql/test/
-rw-rw---- 1 mysql mysql  65 Sep  9 00:42 db.opt
-rw-rw---- 1 mysql mysql 940 Sep  9 00:46 t1.frm

```

t1.ibd は存在しません。  
この状態で，`ALTER TABLE t1 ENGINE=InnoDB;` を実行します。

```
MariaDB [test]> ALTER TABLE t1 ENGINE=InnoDB;
Query OK, 0 rows affected (0.019 sec)
Records: 0  Duplicates: 0  Warnings: 0

```

/var/lib/mysql/test には，t1.ibd ファイルが戻ります。

```
# ls -l /var/lib/mysql/test/
-rw-rw---- 1 mysql mysql     65 Sep  9 00:42 db.opt
-rw-rw---- 1 mysql mysql    944 Sep  9 00:59 t1.frm
-rw-rw---- 1 mysql mysql 114688 Sep  9 00:59 t1.ibd

```

t1 テーブルは InnoDB テーブル(`ENGINE=InnoDB`)に戻っています。

```
MariaDB [test]> SHOW CREATE TABLE t1 \G
*************************** 1. row ***************************
       Table: t1
Create Table: CREATE TABLE `t1` (
  `a` int(11) DEFAULT NULL,
  `b` int(11) DEFAULT NULL,
  KEY `a` (`a`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1

```

### まとめ

今回は MariaDB Enterprise Server に追加された S3 ストレージエンジンに関して簡単な検証を行ってみました。  
[Knowledge Base](https://staging-mdb.com/kb/en/library/s3-storage-engine/) では Community Server 10.5 で実装予定となっていて，[GitHub レポジトリ](https://github.com/MariaDB/server)から 10.5 ブランチのコードを clone し，ビルドすれば Community Server 10.5 でも利用可能です(GAではありませんのでサブスクリプション契約をお持ちでもサポート対象外です)。