LoginSignup
3
0

More than 1 year has passed since last update.

SchemaSpyでデータベースのビジュアライズ化をしてみる(お試し篇)

Posted at

はじめに

  • 以前、こちらの記事でAPI定義をコードで管理して幸せを掴んだ話を書かせていただきました
  • と、なってくるとデータベース定義に関しても幸せになりたいなと思うのが世の常でございます
  • なにやらSchemaSpyというツールを使うとER図の生成ができるということだったので試してみたいと思います

SchemaSpyとは

chemaSpy is a Java-based tool (requires Java 8 or higher) that analyzes the metadata of a schema in a database and generates a visual representation of it in a browser-displayable format. It lets you click through the hierarchy of database tables via child and parent table relationships as represented by both HTML links and entity-relationship diagrams. It’s also designed to help resolve the obtuse errors that a database sometimes gives related to failures due to constraints.

  • Javaベースのデータベースビジュアライズツールということですね。おそらく。

cf. デモ
cf. ドキュメント

まずは動かしてみる

  • 今回はサンプルとしてdockerでWordPressをたてて試してみます
  • WordPressのdocker環境はこちらで簡単に用意できます
  • 記載のymlファイル(以下参照)を作成し、 docker-compose up -d でコンテナを立ち上げたら、ブラウザにて localhost:8000 で初回アクセス、WordPressの初期設定まで完了したら準備完了です
  • ※ ホストから繋ぐためdbコンテナ側の ports 設定だけ追加してます
docker-compose.yml
version: '3'

services:
   db:
     image: mysql:5.7
     ports:
      - "3306:3306"
     volumes:
       - db_data:/var/lib/mysql
     restart: always
     environment:
       MYSQL_ROOT_PASSWORD: somewordpress
       MYSQL_DATABASE: wordpress
       MYSQL_USER: wordpress
       MYSQL_PASSWORD: wordpress

   wordpress:
     depends_on:
       - db
     image: wordpress:latest
     ports:
       - "8000:80"
     restart: always
     environment:
       WORDPRESS_DB_HOST: db:3306
       WORDPRESS_DB_USER: wordpress
       WORDPRESS_DB_PASSWORD: wordpress
volumes:
    db_data:

スクリーンショット 2021-12-11 14.19.25.png
↑ブラウザはここまででOK

  • つづいて、ドキュメントを生成していきましょう
  • Dockerで使用できるようSchemaSpy公式がイメージを公開してるので簡単に生成できます
  • ただ、driverが古いとのことでそのまま実行するとコケるので、まずは最新のdriverを導入します
$ mkdir drivers
$ curl -L https://dev.mysql.com/get/Downloads/Connector-J/mysql-connector-java-8.0.27.tar.gz |    tar xvzf - -C drivers --strip=1 mysql-connector-java-8.0.27/mysql-connector-java-8.0.27.jar

$ docker run -v "$PWD/schema:/output" -v "$PWD/drivers:/drivers"  --net="host" schemaspy/schemaspy:snapshot -t mysql -host localhost:3306 -db wordpress -u root -p somewordpress -s wordpress
オプション一部抜粋 説明
-t データベースタイプ(ex. mysql, pgsql)
-host ホスト
-db データベース名
-u ユーザー
-p パスワード
-s スキーマ名(※ MySQLでも指定が必要。データベース名と同一でOK)

cf. https://schemaspy.readthedocs.io/en/latest/configuration/commandline.html

スクリーンショット 2021-12-11 15.25.20.png

  • なにやらめっちゃエラーと出ましたが、とりあえず出力が成功したようです
  • 出力されたindex.htmlをブラウザで開いてみると無事表示されました

スクリーンショット 2021-12-11 15.27.22.png

スクリーンショット 2021-12-11 15.32.16.png

おわりに

  • 今回は、SchemaSpyを用いたデータベースのビジュアライズ化を試してみました
  • 次は生成したページの方を掘り下げる&実運用のことを考えていければと思います
3
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
3
0