tom__bo’s Blog

MySQL!! MySQL!! @tom__bo

database_mixでMySQLの情報を1ファイルにまとめる

概要

MySQLたちの管理をしていると、サーバの設定やユーザ、テーブル定義をリストアップしたいと思うことは多いと思います。特にLLMに投げて簡易的な調査を行ったり、運用作業の補足に使うにも必要な情報が1つにまとまっていると便利です。 Database用のMCPサーバを活用するのも良いと思いますが、セキュリティ的な懸念(どういったクエリを投げるのか)や、価格的な懸念(どのくらいtokenを使うのか)が気になります。筆者はこれらを管理しながら使うのは面倒で敬遠しがちです。

あるとき、Gitリポジトリ全体を1ファイルに纏めて出力できる repomixというツールがあることを知りました。 GPTsにリクエストを投げるときに前処理として使うととても便利だったので、データベース(MySQL)でも情報を1ファイルに纏めてみたというのが背景です。 作ってみた結果、1ファイルに情報がまとまっていることで、人間(筆者)がDBの情報を把握するのに便利で助かっています。

github.com

使い方

早速サンプル

こんな感じで使えます!

$ databasemix -host 127.0.0.1 -port 3380 -user root -password rootpass -format markdown

出力サンプル全体: dbmix-output-sample.md · GitHub

(抜粋)

# File Summary

This file contains comprehensive MySQL database information compiled for AI context analysis. It includes schema definitions, account configurations, system variables, and other database metadata consolidated into a single file for efficient processing.

**Database Type**: MySQL  
**Database Version**: 8.0.42

## File Structure

- Variables - MySQL system variables and their current values
- Tables - Database tables with metadata and DDL definitions
- View Details - Database views with their definitions
- Stored Functions - User-defined functions with their definitions
- Stored Procedures - User-defined procedures with their definitions
- User Roles - MySQL 8.0+ role definitions and assignments
- User Accounts - Database user accounts with privileges

# Variables

| Variable Name | Current Value |
|---------------|---------------|
| activate_all_roles_on_login | OFF |
| admin_address |  |
| admin_port | 33062 |
| admin_ssl_ca |  |
| admin_ssl_capath |  |
| admin_ssl_cert |  |
| ... | ... |
| xa_detach_on_prepare | ON |

# Tables

# Tables

## testdb.data_types_test

- Engine: InnoDB
- Created: 2025-10-06 14:34:20
- Collation: utf8mb4_0900_ai_ci
- Charset: utf8mb4
- Row Format: Dynamic

``sql
CREATE TABLE `data_types_test` (
  `id` int NOT NULL AUTO_INCREMENT,
  `tinyint_col` tinyint DEFAULT NULL,
  ...
  `json_col` json DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci
``

## testdb.logs

- Engine: InnoDB
- Auto Increment: 6
- Created: 2025-10-06 14:34:20
- Collation: utf8mb4_unicode_ci
- Charset: utf8mb4
- Row Format: Dynamic
- Create Options: partitioned

``sql
CREATE TABLE `logs` (
  `id` bigint NOT NULL AUTO_INCREMENT,
  `log_date` date NOT NULL,
  `level` varchar(10) COLLATE utf8mb4_unicode_ci NOT NULL,
  `message` text COLLATE utf8mb4_unicode_ci,
  PRIMARY KEY (`id`,`log_date`)
) ENGINE=InnoDB AUTO_INCREMENT=6 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci
/*!50100 PARTITION BY RANGE (year(`log_date`))
(PARTITION p2023 VALUES LESS THAN (2024) ENGINE = InnoDB,
 PARTITION p2024 VALUES LESS THAN (2025) ENGINE = InnoDB,
 PARTITION p2025 VALUES LESS THAN (2026) ENGINE = InnoDB) */
``

# View info details

## testdb.active_users

``sql
CREATE ALGORITHM=UNDEFINED DEFINER=`root`@`localhost` SQL SECURITY DEFINER VIEW `testdb`.`active_users` AS select `testdb`.`users`.`id` AS `id`,`testdb`.`users`.`username` AS `username`,`testdb`.`users`.`email` AS `email`,`testdb`.`users`.`created_at` AS `created_at` from `testdb`.`users` where (`testdb`.`users`.`is_active` = true)
``

# Stored Functions

## testdb.calculate_user_total

- Specific Name: calculate_user_total
- Routine Catalog: def
- Character Set: utf8mb4
- Collation: utf8mb4_unicode_ci
- Routine Body: SQL
- External Language: SQL
- Parameter Style: SQL
- Is Deterministic: YES
- SQL Data Access: READS SQL DATA
- Security Type: DEFINER
- Created: 2025-10-06 14:34:20
- Last Altered: 2025-10-06 14:34:20
- SQL Mode: ONLY_FULL_GROUP_BY,STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_ENGINE_SUBSTITUTION

``sql
BEGIN
    DECLARE total DECIMAL(10,2);
    SELECT COALESCE(SUM(total_amount), 0) INTO total
    FROM orders
    WHERE user_id = p_user_id AND status = 'completed';
    RETURN total;
END
``

# Stored Procedures

## testdb.generate_sales_report

- Specific Name: generate_sales_report
- Routine Catalog: def
- Routine Body: SQL
- External Language: SQL
- Parameter Style: SQL
- Is Deterministic: YES
- SQL Data Access: CONTAINS SQL
- Security Type: DEFINER
- Created: 2025-10-06 14:34:20
- Last Altered: 2025-10-06 14:34:20
- SQL Mode: ONLY_FULL_GROUP_BY,STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_ENGINE_SUBSTITUTION

``sql
BEGIN
    SELECT 
        DATE(o.order_date) as sale_date,
        COUNT(DISTINCT o.id) as order_count,
        COUNT(DISTINCT o.user_id) as unique_customers,
        SUM(o.total_amount) as daily_revenue
    FROM orders o
    WHERE o.order_date BETWEEN p_start_date AND p_end_date
        AND o.status = 'completed'
    GROUP BY DATE(o.order_date)
    ORDER BY sale_date;
END
``


# User Roles (MySQL 8.0+)

## app_user1@%

- Plugin: mysql_native_password
- Account Locked: N
- Grants:
  - GRANT USAGE ON *.* TO `app_user1`@`%`
  - GRANT `app_read`@`%` TO `app_user1`@`%`

## app_user2@%

- Plugin: mysql_native_password
- Account Locked: N
- Grants:
  - GRANT USAGE ON *.* TO `app_user2`@`%`
  - GRANT `app_read`@`%`,`app_write`@`%` TO `app_user2`@`%`

## test_reader_role@%

- Plugin: mysql_native_password
- Account Locked: Y
- Grants:
  - GRANT USAGE ON *.* TO `test_reader_role`@`%`
  - GRANT SELECT ON `testdb`.* TO `test_reader_role`@`%`

## test_writer_role@%

- Plugin: mysql_native_password
- Account Locked: Y
- Grants:
  - GRANT USAGE ON *.* TO `test_writer_role`@`%`
  - GRANT INSERT, UPDATE, DELETE, CREATE, DROP ON `testdb`.* TO `test_writer_role`@`%`
``

install

go installを実行

$ go install github.com/tom--bo/databasemix@v0.1.0

オプション

% databasemix -help
Usage of databasemix:
  -database string
        MySQL database name (if not specified, all accessible databases will be analyzed)
  -except-plugins
        Exclude installed plugins
  -except-roles
        Exclude user roles
  -except-stored-procedures
        Exclude stored procedures and functions
  -except-tables
        Exclude tables and views
  -except-users
        Exclude user accounts
  -except-variables
        Exclude variables/configuration parameters
  -format string
        Output format: markdown, xml, plaintext (default "markdown")
  -host string
        Database host (default "localhost")
  -only-modified-variables
        Show only modified variables (default: show all)
  -outfile string
        Output filename (if not specified, output goes to stdout) (default "dbmix-output")
  -password string
        Database password
  -port string
        MySQL port (default "3306")
  -replication
        Include replication information
  -user string
        MySQL user (default "root")

機能

  • サポートバージョン

    • 5.7
    • 8.0
    • 8.4
  • 出力する情報

    • データベースのバージョンと接続情報
    • Variables
    • テーブル
    • View
    • Stored function/procedure
    • ユーザアカウント
    • ロール
  • 出力フォーマット

便利な使い方

(1) ChatGPT-5に投げてアドバイスを貰う

マスクしないといけない情報もわかりやすいので、AIに投げられない情報を除いたら雑に投げることが可能。

↓ chatgptに良くない設定について聞いてみた例。 (仕込んでおいたsync_binlogとinnodb_flush_log_at_trx_commitを指摘してくれた)

(2) SQLを書かずに特定のキーワードで検索ができる

とりあえず複数あるDBの"mixファイル"(出力結果)を取っておけばキーワードで検索する事が可能。

例1 "ALL権限"がついているユーザいる?とかをSQLを書かなくても(誰でも)確認できる

$ cat dbmix-output.md | grep ' ALL '
  - GRANT ALL PRIVILEGES ON `testdb`.* TO `app_admin`@`%`
  - GRANT ALL PRIVILEGES ON `testdb`.* TO `testuser`@`%`
  - GRANT ALL PRIVILEGES ON `testdb2`.* TO `testuser`@`%`

例2: native_password_pluginで認証しているユーザがいるかな?とか

% cat dbmix-output.md | grep -B 2 mysql_native_password
...
--
## admin@%

- Plugin: mysql_native_password
--
## analyst@%

- Plugin: mysql_native_password
--
## analyst_user@%

- Plugin: mysql_native_password
--
## app_admin@%

- Plugin: mysql_native_password

課題

Future work

  • show statusの結果や統計情報の取得
  • 他のDB製品の対応