Perl製のmysqldiffというツールを試してみる

CentOS7にMySQLをインストールしてみる
の続き

Perlで作られたmysqldiffというツールを試してみる。

【参考】
DBテーブルの差分を出力するMySQL::diffをインストールする - 創作メモ帳
http://sousaku-memo.net/php-system/983

俺の知見 - インフラエンジニアでも覚えておくと嬉しいかもしれない MySQL の join と mysqldiff を試してみる。 - Qiita
http://qiita.com/inokappa/items/05abbf78fe70973ac2ac

インストール

とりあえずrootになる。

$ su -

今回はとりあえず/usr/local/srcに置く。

# cd /usr/local/src
# wget http://search.cpan.org/CPAN/authors/id/A/AS/ASPIERS/MySQL-Diff-0.43.tar.gz
# tar zxvf MySQL-Diff-0.43.tar.gz

パスを通さないと動かないようなので、下記のように一行追加する。
(readonlyなので強制的に書き込む)

# vi MySQL-Diff-0.43/bin/mysqldiff
#!/usr/bin/perl -w

use lib '/usr/local/src/MySQL-Diff-0.43/lib';	←追加

=head1 NAME
・・・

Slurpというものが必要みたいなのでインストールする。

# yum -y install perl-File-Slurp

シンボリックリンクをはる。

# ln -s /usr/local/src/MySQL-Diff-0.43/bin/mysqldiff /usr/local/bin/mysqldiff

インストール終了。

# exit

mysqldiffが入った。

$ mysqldiff
Usage: mysqldiff [ options ] <database1> <database2>

Options:
  -?,  --help             show this help
  -A,  --apply            interactively patch database1 to match database2
  -B,  --batch-apply      non-interactively patch database1 to match database2
  -d,  --debug[=N]        enable debugging [level N, default 1]
  -o,  --only-both        only output changes for tables in both databases
  -k,  --keep-old-tables  don't output DROP TABLE commands
  -n,  --no-old-defs      suppress comments describing old definitions
  -t,  --table-re=REGEXP  restrict comparisons to tables matching REGEXP
  -i,  --tolerant         ignore DEFAULT, AUTO_INCREMENT, COLLATE, and formatting changes

  -h,  --host=...         connect to host
  -P,  --port=...         use this port for connection
  -u,  --user=...         user for login if not current user
  -p,  --password[=...]   password to use when connecting to server
  -s,  --socket=...       socket to use when connecting to server

for <databaseN> only, where N == 1 or 2,
       --hostN=...        connect to host
       --portN=...        use this port for connection
       --userN=...        user for login if not current user
       --passwordN[=...]  password to use when connecting to server
       --socketN=...      socket to use when connecting to server

Databases can be either files or database names.
If there is an ambiguity, the file will be preferred;
to prevent this prefix the database argument with `db:'.

確認

$ mysql -u root

データベースを2つ用意する。

create database db1;
create database db2;

一つ目のデータベースに下記のテーブルを作成する。

create table db1.aaa (
  c1 int,
  c2 varchar(8),
  c3 int
);

create table db1.bbb (
  c int
);

二つ目のデータベースに下記のテーブルを作成する。

create table db2.aaa (
  c1 bigint primary key,
  c2 varchar(16),
  c4 int
);

create table db2.ccc (
  c int
);

mysqldiffを実行すると、ALTER文等が生成される。

$ mysqldiff -u root db1 db2
## mysqldiff 0.43
##
## Run on Sun Feb 22 15:53:28 2015
## Options: user=root, debug=0
##
## ---   db: db1 (user=root)
## +++   db: db2 (user=root)

ALTER TABLE aaa DROP COLUMN c3; # was int(11) DEFAULT NULL
ALTER TABLE aaa CHANGE COLUMN c2 c2 varchar(16) DEFAULT NULL; # was varchar(8) DEFAULT NULL
ALTER TABLE aaa CHANGE COLUMN c1 c1 bigint(20) NOT NULL; # was int(11) DEFAULT NULL
ALTER TABLE aaa ADD COLUMN c4 int(11) DEFAULT NULL;
ALTER TABLE aaa ADD PRIMARY KEY (c1);
DROP TABLE bbb;

CREATE TABLE ccc (
  c int(11) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;