# mysql-schema-diff **Repository Path**: opstool/mysql-schema-diff ## Basic Information - **Project Name**: mysql-schema-diff - **Description**: php 的 表结构对比工具 - **Primary Language**: PHP - **License**: Not specified - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 1 - **Created**: 2018-06-25 - **Last Updated**: 2022-10-22 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README ### 重要说明 - 针对字段重命名的sql修改,无法对比出差异 - 针对视图、存储过程、函数、触发器的修改,无法对比出差异(后期部分会支持) - 暂不支持在移动采编(移动采编目录结构问题)项目下运行该程序,但是可以在其它jian应用项目下操作移动采编数据库对比 - 该工具的对比会生成 `drop table`、`drop database` 等操作,实际执行中,尤其在项目有定制的情况下,谨慎操作 > 生产环境进行数据库的操作需谨慎, 请在操作前确保数据库已经做了备份 ### mysqldiff.phar 的使用说明 执行 `php mysqldiff.phar` 可以查看所有命令 ``` #!/usr/bin/env php app version 1.0 Usage: command [options] [arguments] Options: -h, --help Display this help message -q, --quiet Do not output any message -V, --version Display this application version --ansi Force ANSI output --no-ansi Disable ANSI output -n, --no-interaction Do not ask any interactive question -v|vv|vvv, --verbose Increase the verbosity of messages: 1 for normal output, 2 for more verbose output and 3 for debug Available commands: help Displays help for a command list Lists commands schema schema:diff 对比两个数据的结构,生成差异的sql文件 schema:migrate 执行数据库文件 schema:source 导入一个sql文件到数据库 ``` ### 具体操作说明 以jian应用 `app-invited` 为例 #### 导出sql文件到数据库 执行 `php mysqldiff.phar schema:source -h` 查看帮助: ``` Usage: schema:source Arguments: file 需要导入的sql文件,例:app_invited.sql Options: -h, --help Display this help message -q, --quiet Do not output any message -V, --version Display this application version --ansi Force ANSI output --no-ansi Disable ANSI output -n, --no-interaction Do not ask any interactive question -v|vv|vvv, --verbose Increase the verbosity of messages: 1 for normal output, 2 for more verbose output and 3 for debug Help: 导入一个sql文件到数据库 ``` 执行 `php mysqldiff.phar schema:source app_invited.sql` 会将sql导入到一个默认数据库`cmstop_schema` #### 对比两个数据库 执行 `php mysqldiff.phar schema:diff -h` 查看帮助: ``` Usage: schema:diff [] [] Arguments: old 老数据库的名称 [default: "app-invited"] new 新数据库的名称 [default: "cmstop_schema"] Options: -h, --help Display this help message -q, --quiet Do not output any message -V, --version Display this application version --ansi Force ANSI output --no-ansi Disable ANSI output -n, --no-interaction Do not ask any interactive question -v|vv|vvv, --verbose Increase the verbosity of messages: 1 for normal output, 2 for more verbose output and 3 for debug Help: 对比两个数据的结构,生成差异的sql文件 ``` 执行 `php mysqldiff.phar schema:diff app-invited cmstop_schema` 后会在当前目录生成一个 `diff.sql`文件 > 说明:在jian应用的根目录下,数据库参数可以省略, 默认对比当前jian应用的数据库 > 也可以对比两个已经存在的数据库:`php mysqldiff.phar schema:diff site_10001 site_10002` #### 执行migration 文件 执行 `php mysqldiff.phar schema:migrate -h` 查看帮助: ``` Usage: schema:migrate [] [] Arguments: file 需要执行的sql文件 [default: "diff.sql"] dbname 需要导入的数据库 [default: "app-invited"] Options: -h, --help Display this help message -q, --quiet Do not output any message -V, --version Display this application version --ansi Force ANSI output --no-ansi Disable ANSI output -n, --no-interaction Do not ask any interactive question -v|vv|vvv, --verbose Increase the verbosity of messages: 1 for normal output, 2 for more verbose output and 3 for debug Help: 执行数据库文件 ``` 执行 `php mysqldiff.phar schema:migrate diff.sql app-invited` 后会将上一步生成的 `diff.sql` 应用到 `app-invited` 数据库 也可执行 `php mysqldiff.phar schema:migrate diff.sql collect_10001,collect_10002,collect_10003` 将差异sql应用到多个数据库 >说明: jian应用下,默认可以不带参数,且默认是导入 差异的sql文件到 jian应用对应的数据库