简介
Skeema is a tool for managing MySQL tables and schema changes in a declarative fashion using pure SQL. It provides a CLI tool allowing you to:
- Export CREATE TABLE statements to the filesystem, for tracking in a repo (git, hg, svn, etc)
- Diff changes in the schema repo against live DBs to automatically generate DDL
- Manage multiple environments (e.g. dev, staging, prod) and keep them in sync with ease
- Configure use of online schema change tools, such as pt-online-schema-change, for performing ALTERs
- Convert non-online migrations from frameworks like Rails or Django into online schema changes in production
Skeema supports a pull-request-based workflow for schema change submission, review, and execution. This permits your team to manage schema changes in exactly the same way as you manage code changes. Our new companion Cloud Linter for GitHub repos provides automatic linting of schema change commits and pull requests.
我这的需求是同步生产环境表结构到演练环境, 以下是针对我这个场景的具体使用方法
权限
skeema需要的用户权限具体见https://www.skeema.io/docs/requirements/
这里说一下重点, skeema diff
时会在目标环境创建一个_skeema_tmp
临时数据库, 然后会删除这个库
安装
安装go环境
1 | wget https://dl.google.com/go/go1.16.linux-amd64.tar.gz |
安装skeema
1 | curl -LO https://github.com/skeema/skeema/releases/latest/download/skeema_amd64.rpm |
使用
init
在演练环境
1 | skeema init -h 127.0.0.1 -P 3358 -u fanboshi -p -d 3358 --schema sss |
-d dump的sql文件存储路径
–schema 只导出sss这个数据库的结构信息
随后3358目录下回包含一些.sql文件和一个.skeema
文件
1 | -rw-r--r-- 1 root root 213 Feb 18 12:37 .skeema |
编辑.skeema文件
1 | #cat .skeema |
我们这里修改[production]
为[drill]
, 并增加一些参数
1 | alter-wrapper="gh-ost --allow-on-master --assume-rbr --initially-drop-ghost-table --initially-drop-old-table -exact-rowcount --approve-renamed-columns --concurrent-rowcount=false --chunk-size=800 --hooks-path=/tmp/hook --user={USER} --ask-pass --host={HOST} --port={PORT} --database={SCHEMA} --table={TABLE} --alter={CLAUSES} --execute" |
添加生产环境
1 | skeema add-environment product -h 172.16.120.11 -P 3358 -u fanboshi |
或者直接在.skeema
添加如下信息
1 | [product] |
最终的.skeema
文件如下
1 | alter-wrapper="gh-ost --allow-on-master --assume-rbr --initially-drop-ghost-table --initially-drop-old-table -exact-rowcount --approve-renamed-columns --concurrent-rowcount=false --chunk-size=800 --hooks-path=/tmp/hook --user={USER} --ask-pass --host={HOST} --port={PORT} --database={SCHEMA} --table={TABLE} --alter={CLAUSES} --execute" |
拉取生产环境表结构
1 | cd 3358 |
生成drill环境与生产环境的差异sql文件
1 | skeema diff drill --allow-unsafe -psuperpass > /tmp/diff.sql |
如果没有在
.skeema
文件中指定password参数, 则要在命令行指定-p参数并填写密码
查看生产的文件
1 | -- instance: 127.0.0.1:3358 |
可以看到这就是一个.sql
文件, 注意生成的gh-ost语句前有一个\!
, 大家应该明白是啥意思吧
如果是想手动执行gh-ost命令, 记得把”`”转义掉