MySQL(MariaDB) で データなしでデータベースコピー


「テストデータベース」

「テスト2」

にテーブル構造だけをコピーしたい。

環境

  • OS: Ubuntu 18.04 LTS
  • MariaDB (mysql Ver 15.1 Distrib 10.1.44-MariaDB, for debian-linux-gnu (x86_64) using readline 5.2)

データ取得

root でシェルから作業する。


# mysqldump -u root -p --no-data テストデータベース > db.skeleton

通常の mysqldump の使い方に --no-data をつけるだけ

新しいデータベース作る

mysql に root でログインして


# mysql -u root -p
Enter password: 
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 521
Server version: 10.1.44-MariaDB-0ubuntu0.18.04.1 Ubuntu 18.04

Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

MariaDB [(none)]> create database テスト2;
Query OK, 1 row affected (0.00 sec)

MariaDB [(none)]> exit
Bye

読み込み

ルートでシェルから


# mysql -u root -p テスト2 < db.skeleton

ユーザ権限設定

「MariaDB on Ubuntu18.04 LTS」
https://qiita.com/nanbuwks/items/c98c51744bd0f72a7087
のように、 webdb ユーザに権限を設定していたので同様に設定する必要がある。

mysql に root でログインして、


# mysql -u root -p


MariaDB [(none)]> GRANT ALL ON テスト2.* TO 'webdb'@'localhost';
Query OK, 0 rows affected (0.00 sec)


MariaDB [(none)]> GRANT ALL ON テスト2.* TO webdb@'%';
Query OK, 0 rows affected (0.00 sec)

これでOK