本文目录导读:

为PHP项目自动导出数据库表结构,推荐以下几种实用方案(从简单到专业):
使用PHP原生脚本(零依赖)
<?php
// export_schema.php
$host = 'localhost';
$dbname = 'your_database';
$user = 'root';
$pass = '';
try {
$pdo = new PDO("mysql:host=$host;dbname=$dbname", $user, $pass);
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
// 获取所有表
$tables = $pdo->query("SHOW TABLES")->fetchAll(PDO::FETCH_COLUMN);
$markdown = "# 数据库表结构文档\n\n";
foreach ($tables as $table) {
$markdown .= "## {$table}\n\n";
$markdown .= "| 字段名 | 类型 | 空 | 默认值 | 注释 |\n";
$markdown .= "|--------|------|----|--------|------|\n";
$columns = $pdo->query("SHOW FULL COLUMNS FROM `$table`")->fetchAll();
foreach ($columns as $col) {
$markdown .= sprintf(
"| %s | %s | %s | %s | %s |\n",
$col['Field'],
$col['Type'],
$col['Null'] == 'YES' ? '是' : '否',
$col['Default'] ?? 'NULL',
$col['Comment']
);
}
$markdown .= "\n";
}
file_put_contents('database_schema.md', $markdown);
echo "导出成功!文件: database_schema.md\n";
} catch (PDOException $e) {
echo "错误: " . $e->getMessage() . "\n";
}
使用命令行工具(推荐)
MySQL Workbench 导出
# 命令行导出Markdown mysqldump --no-data --skip-create-options --compact \ -u root -p your_database > schema.sql
使用 phpMyAdmin
- 选择数据库
- 点击 "导出"
- 选择 "自定义" -> 勾选 "表结构"
- 格式选择 "Markdown" 或 "CSV"
使用 SchemaCrawler(专业工具)
# 安装 composer require schemacrawler/schemacrawler # 导出为HTML ./vendor/bin/schemacrawler --server=mysql \ --host=localhost --port=3306 \ --database=your_db --user=root --password=xxx \ --info-level=maximum \ --command=details --output-format=html \ --output-file=schema.html
集成到PHP框架
Laravel 项目使用包
// 安装 composer require --dev beyondcode/laravel-dokomori // 生成文档 php artisan dokomori:generate // 或使用更全面的工具 composer require --dev the-events-calendar/php-db-schema-exporter
自定义Artisan命令
// app/Console/Commands/ExportDatabaseSchema.php
<?php
namespace App\Console\Commands;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\DB;
class ExportDatabaseSchema extends Command
{
protected $signature = 'db:export-schema {--format=markdown}';
protected $description = '导出数据库表结构';
public function handle()
{
$tables = DB::select('SHOW TABLES');
$key = 'Tables_in_' . DB::getDatabaseName();
$output = "# 数据库文档\n\n";
foreach ($tables as $table) {
$tableName = $table->$key;
$output .= "## {$tableName}\n\n";
$columns = DB::select("SHOW FULL COLUMNS FROM `{$tableName}`");
$output .= "| 字段 | 类型 | 主键 | 默认值 | 注释 |\n";
$output .= "|------|------|------|--------|------|\n";
foreach ($columns as $col) {
$output .= sprintf(
"| %s | %s | %s | %s | %s |\n",
$col->Field,
$col->Type,
$col->Key == 'PRI' ? '✓' : '',
$col->Default ?? 'NULL',
$col->Comment
);
}
$output .= "\n---\n\n";
}
file_put_contents('docs/database.md', $output);
$this->info('文档导出完成!');
}
}
使用开源工具(Web界面)
DBeaver(最佳免费工具)
- 连接数据库
- 右键数据库 -> 生成DDL
- 选择导出格式(HTML/Markdown)
Navicat(商业)
- 右键表 -> 对象信息
- 导出为SQL或HTML
自动化CI/CD集成
# .github/workflows/schema-docs.yml
name: Generate Database Docs
on:
push:
branches: [main]
paths:
- 'database/migrations/**'
jobs:
generate-docs:
runs-on: ubuntu-latest
services:
mysql:
image: mysql:8.0
env:
MYSQL_ROOT_PASSWORD: test
MYSQL_DATABASE: test_db
steps:
- uses: actions/checkout@v2
- name: Run migrations
run: php artisan migrate
- name: Export schema
run: php artisan db:export-schema
- name: Deploy docs
uses: peaceiris/actions-gh-pages@v3
with:
github_token: ${{ secrets.GITHUB_TOKEN }}
publish_dir: ./docs
推荐方案
| 场景 | 推荐方案 |
|---|---|
| 快速导出 | 使用phpMyAdmin或DBeaver |
| 自动化文档 | 编写上面PHP脚本 + Cron任务 |
| 团队协作 | 使用SchemaCrawler生成HTML |
| Laravel项目 | 自定义Artisan命令 |
| 持续集成 | 集成到CI/CD流程 |
最佳实践:编写上面第3种方案中的Laravel命令,配合Git钩子自动化生成文档,这样每次数据库结构变更,文档都会自动更新。