MySQLストレージエンジン------Federatedベスト実戦


1.背景
*ローカルMySQLデータベースリモートMySQLデータベースのテーブルのデータにアクセスするには、FEDERATEDストレージエンジンで実行する必要があります. 
*Oracleのデータベースリンク(DBLINK)と似ています.このストレージエンジンを許可するには、MySQLを構築するときに--with-federated-storage-engineを使用してconfigureを使用します. 
*FEDERATEDテーブルを作成すると、サーバはデータベースディレクトリにテーブル定義ファイルを作成する.ファイルはテーブルの名前から始まり、1つあります.frm拡張子 
*実際のデータはリモート・データベース上にあるため、他のファイルは作成されません. 
2.相関特性
*リモートMySQLデータベースのテーブルのデータへのローカルアクセスを許可
*ローカルにデータファイルは保存されません
*MySQLのMySQLへのアクセスのみをサポート
*異機種データベースへのアクセスはサポートされていません
*MySQLデフォルトではFederatedストレージエンジンは起動しません
3.環境
2つのMySQL 5.7インスタンス
   serverA 3306
     serverB    3307
[root@MySQL ~]# mysql -S /tmp/mysql.sock1 -p123456
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 7
Server version: 5.7.18 MySQL Community Server (GPL)

Copyright (c) 2000, 2017, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

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

mysql> select version();
+-----------+
| version() |
+-----------+
| 5.7.18    |
+-----------+
1 row in set (0.00 sec)

[root@MySQL ~]# mysql -S /tmp/mysql.sock2 -p123456
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 6
Server version: 5.7.18 MySQL Community Server (GPL)

Copyright (c) 2000, 2017, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

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

mysql> select version();
+-----------+
| version() |
+-----------+
| 5.7.18    |
+-----------+
1 row in set (0.00 sec)

   
4.Federatedストレージエンジンの設定
*Federatedがオンになっているかどうかを確認[FEDERATEDのSupportステータスNOはエンジンがオンになっていないことを示します]
mysql> show engines;
+--------------------+---------+----------------------------------------------------------------+--------------+------+------------+
| Engine             | Support | Comment                                                        | Transactions | XA   | Savepoints |
+--------------------+---------+----------------------------------------------------------------+--------------+------+------------+
| CSV                | YES     | CSV storage engine                                             | NO           | NO   | NO         |
| MRG_MYISAM         | YES     | Collection of identical MyISAM tables                          | NO           | NO   | NO         |
| MyISAM             | YES     | MyISAM storage engine                                          | NO           | NO   | NO         |
| BLACKHOLE          | YES     | /dev/null storage engine (anything you write to it disappears) | NO           | NO   | NO         |
| InnoDB             | DEFAULT | Supports transactions, row-level locking, and foreign keys     | YES          | YES  | YES        |
| PERFORMANCE_SCHEMA | YES     | Performance Schema                                             | NO           | NO   | NO         |
| ARCHIVE            | YES     | Archive storage engine                                         | NO           | NO   | NO         |
| MEMORY             | YES     | Hash based, stored in memory, useful for temporary tables      | NO           | NO   | NO         |
| FEDERATED          | NO      | Federated MySQL storage engine                                 | NULL         | NULL | NULL       |
+--------------------+---------+----------------------------------------------------------------+--------------+------+------------+
9 rows in set (0.01 sec)

*プロファイル指定Federatedストレージエンジンをオンにする[/etc/my.cnf]
[mysqld]
federated

*MySQLを再起動[FEDERATEDでSupportステータスがYESになった場合、エンジンが正常に起動したことを示す]
mysql> show engines;
+--------------------+---------+----------------------------------------------------------------+--------------+------+------------+
| Engine             | Support | Comment                                                        | Transactions | XA   | Savepoints |
+--------------------+---------+----------------------------------------------------------------+--------------+------+------------+
| CSV                | YES     | CSV storage engine                                             | NO           | NO   | NO         |
| MRG_MYISAM         | YES     | Collection of identical MyISAM tables                          | NO           | NO   | NO         |
| MyISAM             | YES     | MyISAM storage engine                                          | NO           | NO   | NO         |
| BLACKHOLE          | YES     | /dev/null storage engine (anything you write to it disappears) | NO           | NO   | NO         |
| InnoDB             | DEFAULT | Supports transactions, row-level locking, and foreign keys     | YES          | YES  | YES        |
| PERFORMANCE_SCHEMA | YES     | Performance Schema                                             | NO           | NO   | NO         |
| ARCHIVE            | YES     | Archive storage engine                                         | NO           | NO   | NO         |
| MEMORY             | YES     | Hash based, stored in memory, useful for temporary tables      | NO           | NO   | NO         |
| FEDERATED          | YES     | Federated MySQL storage engine                                 | NO           | NO   | NO         |
+--------------------+---------+----------------------------------------------------------------+--------------+------+------------+
9 rows in set (0.00 sec)

5.配置
*サーバAにデータベースdbtestAがあり、サーバBにデータベースdbtestBがあり、サーバBのデータベースdbtestBにサーバAのデータベースdbtestA上のテーブルtablettestAのデータテーブルリンクremote_を作成するtabletestAは、一般ユーザーtestで接続されています.
*サーバAデータベースの作成
mysql> create database dbtestA;
Query OK, 1 row affected (0.02 sec)

*サーバA dbtestAデータベースでtabletestAテーブルを作成する
mysql> create table tabletestA(
id INT PRIMARY KEY NOT NULL AUTO_INCREMENT
)ENGINE=INNODB;
Query OK, 0 rows affected (0.11 sec)

*サーバAで一般ユーザーを作成し、dbtestAデータベースに関する権限を付与
mysql> create user 'test'@'127.0.0.1' IDENTIFIED BY '123456';
Query OK, 0 rows affected (0.00 sec)

mysql> grant select on dbtestA.* to 'test'@'127.0.0.1';
Query OK, 0 rows affected (0.00 sec)

*サーバBでデータベースdbtestBを作成
mysql> create database dbtestB;
Query OK, 1 row affected (0.00 sec)

*サーバBはdbtestBでリンクテーブルremote_を作成するtabletestA、一般ユーザーtestを使用
mysql> use dbtestB;
Database changed
mysql> create table remote_tabletestA(
    -> id INT PRIMARY KEY NOT NULL AUTO_INCREMENT
    -> )ENGINE=FEDERATED
    -> CONNECTION='mysql://test:[email protected]:3306/dbtestA/tabletestA';
Query OK, 0 rows affected (0.09 sec)

*サーバA dbtestAライブラリtableaテーブルにデータを挿入
Database changed
mysql> use dbtestA;
Database changed
mysql> insert into tabletestA select NULL;
Query OK, 1 row affected (0.01 sec)
Records: 1  Duplicates: 0  Warnings: 0

mysql> insert into tabletestA select NULL;
Query OK, 1 row affected (0.03 sec)
Records: 1  Duplicates: 0  Warnings: 0

mysql> insert into tabletestA select NULL;
Query OK, 1 row affected (0.01 sec)
Records: 1  Duplicates: 0  Warnings: 0

mysql> select * from tabletestA;
+----+
| id |
+----+
|  1 |
|  2 |
|  3 |
+----+
3 rows in set (0.01 sec)

*ServerB dbtestBライブラリリンクテーブルremote_tabletestA表示
mysql> use dbtestB;
Database changed
mysql> select * from remote_tabletestA;
+----+
| id |
+----+
|  1 |
|  2 |
|  3 |
+----+
3 rows in set (0.01 sec)

*Server Bサーバでのリンク表の表示remote_tablestestA関連ファイル
   .frmテーブル定義ファイル[Federatedリンクライブラリローカルでデータファイルを生成しない]
[root@MySQL ~]# ll /data/mysql_data2/dbtestB/
total 16
-rw-r----- 1 mysql mysql   65 Jun 25 10:40 db.opt
-rw-r----- 1 mysql mysql 8556 Jun 25 10:42 remote_tabletestA.frm

6.まとめ
需要駆動技術では、技術自体に優略の区別はなく、業務の区別しかない.