LoginSignup
1
1

More than 1 year has passed since last update.

MySQL mysqldumpコマンドで取得したdumpファイルの中身を覗いてみる

Posted at

目的

  • データ移行する時やDBのバックアップ時によく使用するMySQLのdumpファイルの中身が気になったので見てみる

概要

  • 自分のMacのローカルにあるMySQL8.Xにてテスト用のDBを作成してdumpを出力し中身をテキストエディターで確認してみる。
  • MySQLバージョン情報

    mysql> select @@version;
    +-----------+
    | @@version |
    +-----------+
    | 8.0.21    |
    +-----------+
    

方法

  • テスト用DBの「test」を用意して適当にテーブルを作ってデータをinsertした。
  • 「test」DBからdumpファイルの出力を行う。

    $ mysqldump -u root -p test > test.sql
    
  • カレントディレクトリに「test.sql」が出力されたのでテキストエディターで見てみる。

  • dumpファイルはヘッダー情報とテーブル情報とフッター情報に分かれているようだ。下記がヘッダー情報である。

    test.sql
    -- MySQL dump 10.13  Distrib 8.0.21, for osx10.15 (x86_64)
    --
    -- Host: localhost    Database: test
    -- ------------------------------------------------------
    -- Server version   8.0.21
    
    /*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
    /*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;
    /*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;
    /*!50503 SET NAMES utf8mb4 */;
    /*!40103 SET @OLD_TIME_ZONE=@@TIME_ZONE */;
    /*!40103 SET TIME_ZONE='+00:00' */;
    /*!40014 SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0 */;
    /*!40014 SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0 */;
    /*!40101 SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='NO_AUTO_VALUE_ON_ZERO' */;
    /*!40111 SET @OLD_SQL_NOTES=@@SQL_NOTES, SQL_NOTES=0 */;
    
  • ヘッダー情報にはMySQLのコメント構文を用いてどのようなDBから出力されたdumpファイルなのかの情報が記載されている。

  • dumpファイルが読み込まれる時に必要な情報であり、読み込み時に自動で適用される。コメントだからといって変更するのはNG

  • ヘッダー情報部分の!40111とかの記載はその後ろのSQL句の実行バージョンを示しているらしい。

  • なので下記の記載はSET TIME_ZONE='+00:00'の構文はMySQLバージョン4.1.1.1以上のサーバーにでないと実行されないという意味になる。

    /*!40103 SET TIME_ZONE='+00:00' */;
    
  • 下記がテーブル情報である。ここでテーブルのカラム情報などが定義され、データがinsert句によって入れられてゆく

    --
    -- Table structure for table `admins`
    --
    
    DROP TABLE IF EXISTS `admins`;
    /*!40101 SET @saved_cs_client     = @@character_set_client */;
    /*!50503 SET character_set_client = utf8mb4 */;
    CREATE TABLE `admins` (
      `id` bigint unsigned NOT NULL AUTO_INCREMENT,
      `name` varchar(255) COLLATE utf8mb4_unicode_ci NOT NULL,
      `email` varchar(255) COLLATE utf8mb4_unicode_ci NOT NULL,
      `email_verified_at` timestamp NULL DEFAULT NULL,
      `password` varchar(255) COLLATE utf8mb4_unicode_ci NOT NULL,
      `remember_token` varchar(100) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
      `created_at` timestamp NULL DEFAULT NULL,
      `updated_at` timestamp NULL DEFAULT NULL,
      PRIMARY KEY (`id`),
      UNIQUE KEY `admins_email_unique` (`email`)
    ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
    /*!40101 SET character_set_client = @saved_cs_client */;
    
    --
    -- Dumping data for table `admins`
    --
    
    LOCK TABLES `admins` WRITE;
    /*!40000 ALTER TABLE `admins` DISABLE KEYS */;
    /*!40000 ALTER TABLE `admins` ENABLE KEYS */;
    UNLOCK TABLES;
    
    --
    -- Table structure for table `failed_jobs`
    --
    
    DROP TABLE IF EXISTS `failed_jobs`;
    /*!40101 SET @saved_cs_client     = @@character_set_client */;
    /*!50503 SET character_set_client = utf8mb4 */;
    CREATE TABLE `failed_jobs` (
      `id` bigint unsigned NOT NULL AUTO_INCREMENT,
      `connection` text COLLATE utf8mb4_unicode_ci NOT NULL,
      `queue` text COLLATE utf8mb4_unicode_ci NOT NULL,
      `payload` longtext COLLATE utf8mb4_unicode_ci NOT NULL,
      `exception` longtext COLLATE utf8mb4_unicode_ci NOT NULL,
      `failed_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
      PRIMARY KEY (`id`)
    ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
    /*!40101 SET character_set_client = @saved_cs_client */;
    
    --
    -- Dumping data for table `failed_jobs`
    --
    
    LOCK TABLES `failed_jobs` WRITE;
    /*!40000 ALTER TABLE `failed_jobs` DISABLE KEYS */;
    /*!40000 ALTER TABLE `failed_jobs` ENABLE KEYS */;
    UNLOCK TABLES;
    
    --
    -- Table structure for table `migrations`
    --
    
    DROP TABLE IF EXISTS `migrations`;
    /*!40101 SET @saved_cs_client     = @@character_set_client */;
    /*!50503 SET character_set_client = utf8mb4 */;
    CREATE TABLE `migrations` (
      `id` int unsigned NOT NULL AUTO_INCREMENT,
      `migration` varchar(255) COLLATE utf8mb4_unicode_ci NOT NULL,
      `batch` int NOT NULL,
      PRIMARY KEY (`id`)
    ) ENGINE=InnoDB AUTO_INCREMENT=5 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
    /*!40101 SET character_set_client = @saved_cs_client */;
    
    --
    -- Dumping data for table `migrations`
    --
    
    LOCK TABLES `migrations` WRITE;
    /*!40000 ALTER TABLE `migrations` DISABLE KEYS */;
    INSERT INTO `migrations` VALUES (1,'2014_10_12_000000_create_users_table',1),(2,'2014_10_12_100000_create_password_resets_table',1),(3,'2019_08_19_000000_create_failed_jobs_table',1),(4,'2020_08_17_132812_create_admins_table',2);
    /*!40000 ALTER TABLE `migrations` ENABLE KEYS */;
    UNLOCK TABLES;
    
    --
    -- Table structure for table `password_resets`
    --
    
    DROP TABLE IF EXISTS `password_resets`;
    /*!40101 SET @saved_cs_client     = @@character_set_client */;
    /*!50503 SET character_set_client = utf8mb4 */;
    CREATE TABLE `password_resets` (
      `email` varchar(255) COLLATE utf8mb4_unicode_ci NOT NULL,
      `token` varchar(255) COLLATE utf8mb4_unicode_ci NOT NULL,
      `created_at` timestamp NULL DEFAULT NULL,
      KEY `password_resets_email_index` (`email`)
    ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
    /*!40101 SET character_set_client = @saved_cs_client */;
    
    --
    -- Dumping data for table `password_resets`
    --
    
    LOCK TABLES `password_resets` WRITE;
    /*!40000 ALTER TABLE `password_resets` DISABLE KEYS */;
    /*!40000 ALTER TABLE `password_resets` ENABLE KEYS */;
    UNLOCK TABLES;
    
    --
    -- Table structure for table `users`
    --
    
    DROP TABLE IF EXISTS `users`;
    /*!40101 SET @saved_cs_client     = @@character_set_client */;
    /*!50503 SET character_set_client = utf8mb4 */;
    CREATE TABLE `users` (
      `id` bigint unsigned NOT NULL AUTO_INCREMENT,
      `name` varchar(255) COLLATE utf8mb4_unicode_ci NOT NULL,
      `email` varchar(255) COLLATE utf8mb4_unicode_ci NOT NULL,
      `email_verified_at` timestamp NULL DEFAULT NULL,
      `password` varchar(255) COLLATE utf8mb4_unicode_ci NOT NULL,
      `remember_token` varchar(100) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
      `created_at` timestamp NULL DEFAULT NULL,
      `updated_at` timestamp NULL DEFAULT NULL,
      PRIMARY KEY (`id`),
      UNIQUE KEY `users_email_unique` (`email`)
    ) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
    /*!40101 SET character_set_client = @saved_cs_client */;
    
    --
    -- Dumping data for table `users`
    --
    
    LOCK TABLES `users` WRITE;
    /*!40000 ALTER TABLE `users` DISABLE KEYS */;
    INSERT INTO `users` VALUES (1,'大川 峻','shunokawa@gmail.com',NULL,'$2y$10$TRNNWA0ZtMyNNnFEHQCYv.bYufvlRJz10hWezZIkb4BTw9Ujb8RHC',NULL,'2020-08-17 04:03:34','2020-08-17 04:03:34');
    /*!40000 ALTER TABLE `users` ENABLE KEYS */;
    UNLOCK TABLES;
    
  • 下記がフッター情報である。一旦dump読み込みのために変更したテーブル情報をもともとのものに戻しているようだ。

    /*!40103 SET TIME_ZONE=@OLD_TIME_ZONE */;
    
    /*!40101 SET SQL_MODE=@OLD_SQL_MODE */;
    /*!40014 SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS */;
    /*!40014 SET UNIQUE_CHECKS=@OLD_UNIQUE_CHECKS */;
    /*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;
    /*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;
    /*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;
    /*!40111 SET SQL_NOTES=@OLD_SQL_NOTES */;
    
    -- Dump completed on 2021-12-17 23:54:55
    
    

参考文献

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