2
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

mysql/mariadb外部表(DBLINK,FEDERATED)の利用方法

Last updated at Posted at 2022-04-13

はじめに

mysql - mariadbでのDBLINKの記事がなかったのでまとめておく。
厳密には記事はあるが、日本語が文字化けしやすく、そこまでカバーされた記事がなかったので残しとく。

前提

・ mysql 8.0
・ mariadb 10.3

mysql に 本体のテーブルが存在し、
mariadb から mysqlのテーブルを参照する

試してないけど、
mysql - mysql / mariadb - mariadbなどでも同じ手順で対応できるはず。

MySQL側

mysql> create table dummyTable ( id int , name varchar(255));
Query OK, 0 rows affected (0.01 sec)

mysql> desc dummyTable;
+-------+--------------+------+-----+---------+-------+
| Field | Type         | Null | Key | Default | Extra |
+-------+--------------+------+-----+---------+-------+
| id    | int          | YES  |     | NULL    |       |
| name  | varchar(255) | YES  |     | NULL    |       |
+-------+--------------+------+-----+---------+-------+
2 rows in set (0.00 sec)

mysql> insert into dummyTable values (1,'あああ');
Query OK, 1 row affected (0.00 sec)

mysql> insert into dummyTable values (2,'aaaa');
Query OK, 1 row affected (0.00 sec)

mysql> select * FROM dummyTable;
+------+-----------+
| id   | name      |
+------+-----------+
|    1 | あああ    |
|    2 | aaaa      |
+------+-----------+
2 rows in set (0.00 sec)

Mariadb側

MariaDB > create table dummyTable (id int ,name varchar(255))
    -> ENGINE=FEDERATED 
    -> default charset=utf8
    -> CONNECTION='mysql://xxxx:xxxx@xxxxx/xxxxxx/dummyTable';
--------------
create table dummyTable (id int ,name varchar(255))
ENGINE=FEDERATED 
default charset=utf8
CONNECTION='mysql://xxxx:xxxx@xxxxx/xxxxxx/dummyTable'
--------------

Query OK, 0 rows affected (0.002 sec)

MariaDB > select * FROM dummyTable;
--------------
select * FROM dummyTable
--------------

+------+-----------+
| id   | name      |
+------+-----------+
|    1 | あああ    |
|    2 | aaaa      |
+------+-----------+
2 rows in set (0.003 sec)

ポイント

ENGINE=FEDERATED
これがDBLINKのEngine
DEFAULT CHARSET=utf8
これをつけないと日本語が化けてしまう。utf8の箇所はそれぞれの環境に合わせること。
show variables like '%char%';
CONNECTION='mysql://xxxx:xxxx@xxxxx/xxxxxx/dummyTable'
CONNECTION='mysql://ユーザ名:パスワード@サーバー名/DB名/テーブル名'

参考文献

2
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?