異種DBMSエンジン間の同期化
異種データベース間の同期化をテストするため、宛先となるMySQLデータベースを作り、これをdestinationdbと呼ぶことにする。ODBC DSNはmysql-destinationdbである。unixODBCからisqlコマンドを使えば、ODBC経由でデータベースに接続できる。この方法は、ODBCアクセス問題のトラブルシューティングにも便利に使える。MySQLデータベーススキーマには、serialデータ型でなく、単なる整数型を使うことにした。次に示すsqlsyncコマンドでは、userオプションとpasswordオプションを使い、MySQLデータベースDSNに接続している。
$ isql mysql-destinationdb user passwd SQL> create table customers ( cid int primary key, name varchar(100) ) ; SQL> create table info ( id int primary key, cid int references customers(cid), note text ); SQL> quit $ sqlsync_static --src-dsn sourcedb \ --dst-dsn mysql-destinationdb \ --dst-user user --dst-password passwd \ --show-queries ... Creating synchronization queries INSERT INTO `customers` (`cid`, `name`) VALUES (1, 'fred'); INSERT INTO `customers` (`cid`, `name`) VALUES (2, 'john'); INSERT INTO `customers` (`cid`, `name`) VALUES (3, 'peter'); INSERT INTO `customers` (`cid`, `name`) VALUES (5, 'luke'); ... INSERT INTO `info` (`id`, `cid`, `note`) VALUES (1, 1, 'fred is home'); INSERT INTO `info` (`id`, `cid`, `note`) VALUES (2, 2, 'john is with fred'); INSERT INTO `info` (`id`, `cid`, `note`) VALUES (3, 1, 'a second note for him'); INSERT INTO `info` (`id`, `cid`, `note`) VALUES (4, 2, 'another'); INSERT INTO `info` (`id`, `cid`, `note`) VALUES (6, 5, 'lucky');
同期化するテーブルと列は、sqlsyncの--tablesオプションで制限できる。たとえば、「--tables customers[name],info[note]」とすれば、nameフィールドとnoteフィールドだけを同期化できる。これをテストするため、infoテーブルにidフィールドとnoteフィールドしかない新しいデータベースを作り、このデータベースに対して下記の同期化を実行した。
特定の列だけを選択的に同期化するという機能は便利だが、現在のところ、テーブル名マッピング(たとえば、infoテーブルで宛先データベースのlimitedinfoテーブルを同期化すること)はできない。ただ、将来、そのような機能がSqlSyncに追加されない理由はない。
$ psql
ben# create database partdestdb
ben# \c partdestdb
partdestdb# create table info ( id serial primary key, note text );
partdestdb# \q
$ sqlsync_static --src-dsn sourcedb --dst-dsn partdestdb --tables info[id,note] --show-queries
Parsing tables and columns passed from command line
Parsing tables and columns completed
Connected to dsn_src server sourcedb
Connected to dsn_dst server partdestdb
Checking availability of specified tables
Comparing tables info
Comparing tables info completed
Creating synchronization queries
INSERT INTO "info" ("id", "note") VALUES (1, 'fred is home');
INSERT INTO "info" ("id", "note") VALUES (2, 'john is with fred');
INSERT INTO "info" ("id", "note") VALUES (3, 'a second note for him');
INSERT INTO "info" ("id", "note") VALUES (4, 'another');
INSERT INTO "info" ("id", "note") VALUES (6, 'lucky');
Creating synchronization queries completed
データベースのバックアップ方法として、SqlSyncは十分に考慮に値する。レプリケーションツールを用意しているデータベースは多いが、SqlSyncでは、別のデータベースサーバを使ってデータベースのバックアップができる。あるデータベースエンジンから別のデータベースエンジンへの移行で、テスト中はそれぞれのデータベースサーバ上にデータベースを置いておきたい場合などに、便利に使用できる。
SqlSyncは便利だが、完璧ではない。宛先データベースに自動的にスキーマをセットアップしてくれる機能などは、あって決して邪魔にならないと思う。
Ben Martinは、ファイルシステムに携わって10数年。現在、PH.D.も取得し、libferris、各種ファイルシステム、検索ソリューションを中心にコンサルティング事業を展開している。
