製品

Percona

pt-fk-error-logger

コマンド

pt-fk-error-logger [ オプション ] [ DSN ]

【必須項目】

  • ・ [ オプション ] : -p パスワード
  • ・ [ DSN ] : h=ホスト名, D=データベース名

【主なオプション】

  • ・ –dest : 検出したエラー情報を格納するテーブルを指定します(デフォルトでは標準出力となります)
  • ・ –run-time 数字 : 動作する期間を指定します(デフォルトでは Ctrl – C するまで動作し続けます)

目的

外部キー制約に関するエラー情報を標準出力します

設定ファイル

上記の必須項目を、設定ファイルにまとめておきます
ただし、DSNはコマンドライン上で直接指定する必要があるため、ここでは書きません

# touch /etc/percona-toolkit/pt-fk-error-logger.conf
# vi /etc/percona-toolkit/pt-fk-error-logger.conf

・設定ファイルの作成

# touch /etc/percona-toolkit/pt-fk-error-logger.conf

・設定ファイルの編集

# vi /etc/percona-toolkit/pt-fk-error-logger.conf

# config for pt-fk-error-logger
user=root
password=パスワード

・MySQL のユーザ名

user=root

・MySQL のパスワードを記載

password=パスワード

シナリオ

以下のような親テーブルを用意します

mysql> desc hoge;
+------------+--------------+------+-----+---------+----------------+
| Field      | Type         | Null | Key | Default | Extra          |
+------------+--------------+------+-----+---------+----------------+
| id         | int(11)      | NO   | PRI | NULL    | auto_increment |
| title      | varchar(255) | YES  |     | NULL    |                |
| body       | text         | YES  |     | NULL    |                |
| created_at | datetime     | YES  |     | NULL    |                |
| updated_at | datetime     | YES  |     | NULL    |                |
| test       | int(11)      | YES  |     | NULL    |                |
+------------+--------------+------+-----+---------+----------------+
6 rows in set (0.00 sec)
mysql>

外部キーをつけるために titleカラムにインデックスを作成します

mysql> CREATE INDEX idx_title ON hoge(title);

以下のCREATE文を実行し、子テーブルを作成します

mysql> CREATE TABLE hogehoge (id int(11) AUTO_INCREMENT, title varchar(255),
    -> created_at datetime, updated_at datetime, PRIMARY KEY(id),
    -> FOREIGN KEY(title) REFERENCES hoge(title) ) ENGINE=InnoDB;
Query OK, 0 rows affected (0.14 sec)

pt-fk-error-loggerコマンドを実行します

# pt-fk-error-logger D=test

以下のSQL文を実行し、外部キー制約エラーを発生させます

mysql> INSERT INTO hoge (title) VALUE ( "First" );
mysql> INSERT INTO hogehoge (title) VALUE ( "Second" );

結果

外部キー制約に関するエラーが標準出力されます

[Et@localhost ~]$ sudo pt-fk-error-logger D=test
2017-04-04 07:47:17 7f9aeabe1700 Transaction:
TRANSACTION 310550, ACTIVE 0 sec inserting
mysql tables in use 1, locked 1
3 lock struct(s), heap size 1184, 1 row lock(s), undo log entries 1
MySQL thread id 2, OS thread handle 0x7f9aeabe1700, query id 27 localhost root update
INSERT INTO hogehoge(title) VALUE (""Second"")
Foreign key constraint fails for table `test`.`hogehoge`:
,
  CONSTRAINT `hogehoge_ibfk_1` FOREIGN KEY (`title`) REFERENCES `hoge` (`title`)
Trying to add in child table, in index `title` tuple:
DATA TUPLE: 2 fields;
 0: len 6; hex 5365636f6e64; asc Second;;
 1: len 4; hex 80000001; asc     ;;
 
But in parent table `test`.`hoge`, in index `idx_title`,
the closest match we can find is record:
PHYSICAL RECORD: n_fields 2; compact format; info bits 0
 0: len 7; hex 7469746c655f31; asc title_1;;
 1: len 4; hex 80000001; asc     ;;

良い点

外部キー制約に関するエラーを監視することが出来ます

その他

標準出力の場合、最新のエラーを1件しか表示することが出来ません
そのため、エラー情報を保持するためには出力内容をMySQL内のテーブルに格納する必要があります

以下のようなテーブルを作成します

mysql> CREATE TABLE foreign_key_errors (ts datetime NOT NULL,
              error text NOT NULL, PRIMARY KEY (ts));

以下のコマンドを実行すると、エラー情報が foreign_key_errors テーブルに書き込まれます

# pt-fk-error-logger h=localhost --dest h=localhost,D=blog_test,t=foreign_key_errors