LoginSignup
0
0

More than 1 year has passed since last update.

MySQLでdumpしたビューがテーブルとして登録されてしまう件を解消する

Posted at

概要

  • RDSのMySQL(5.7.33-log)からdumpしたデータをインポートしたらビューがテーブルとして登録されてしまった

解決方法

  • ビューはビュー、テーブルはテーブルで別々にdumpする

解決例

私は業務の都合上PHPで実施しなければいけなかったので、こんな感じで実施しました。

sample.php
$ignore_views = [];
$sql = 'select TABLE_NAME from information_schema.TABLES where TABLE_SCHEMA = "schemaname" and TABLE_TYPE = "VIEW";';
$select_view_cmd = "/usr/bin/mysql -uusername -ppassword -hhostname schemaname -e'" . $sql . "'";
$views = [];

exec($select_view_cmd, $views, $result_code);
$view_tables = '';
if ($result_code === 0) {
    foreach ($views as $view) {
        if ($view === "TABLE_NAME") {
            continue;
        }
        $ignore_views[] = "--ignore-table=schemaname." . $view;
        $view_tables .= ' ' . $view;
    }
}
if (!empty($view_tables)) {
    $create_view_cmd = "/usr/bin/mysqldump --skip-tz-utc --single-transaction --no-tablespaces -hhostname -uusername -ppassword schemaname" . $view_tables . " > create_view.sql";
    exec($create_view_cmd);
}

$ignore_table = '';
if (!empty($ignore_views)) {
    $ignore_table = implode(' ', $ignore_views);
}
$dump_cmd = "/usr/bin/mysqldump --skip-tz-utc --single-transaction --routines --events -hhostname -uusername -ppassword schemaname " . $ignore_table . " > dump.sql";
exec($dump_cmd);

0
0
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
0
0