暂无图片
暂无图片
暂无图片
暂无图片
暂无图片

使用 Skeema 更改 MySQL 架构 – 第 1 部分“基本操作”

原创 Bigbig 2022-10-13
808

作为一名数据库工程师,日常活动中的最大挑战之一是在高流量和事务密集型表上执行 DDL。当处理大量服务器/分片时,它将成为开销。

作为标准流程,我们将首先在 DEV 和 QA 中部署更改,然后再将它们部署到生产环境中。

在分片环境中,维护 DEV、QA 和 PROD 服务器中的模式更改将成为沉重的开销。因为我们将在分片中有多个服务器。

为了克服这种部署监督,Skeema 工具也将有助于以安全且并行的方式(对于 Shards)部署 QA、DEV 和 PROD 中的更改。

本篇博客重点介绍Skeema的基本操作,将会有Skeema系列的博客

  1. 关于 Skeema 工具:
  2. Skeema 工具的内部工作流程:
  3. 基本操作:
    1. Init :
    2. 主机目录:
    3. 差异:
    4. Push :
    5. Pull :
    6. Lint :
  4. 概括 :

关于 Skeema 工具:

Skeema 工具是用 Golang 语言编写的,它也是一个积极开发的工具。它仅支持多台服务器上的 DDL(创建、删除、更改)。它不支持 DDL 中的重命名和截断。

欲了解更多信息——Skeema 网站

Skeema 工具的内部工作流程:

Skeema 工具的内部工作流程如下图所示

安装 Skeema 工具后,我们不能直接使用它。我们需要首先初始化 Skeema 工具。初始化后,我们可以使用选项来执行活动。

Syntax : Skeema <command> <options>
CommandDescriptions
InitInitialize the server by using the Skeema tool. So the host directory will be created. The host directory will hold the schema directory and the metadata file called the .skeema file.
The host directory will hold the table structure with a .sql extension. And .skeema file with hold the metadata information.
If any changes need to be pushed via the Skeema tool, we need to change in *.sql file only.
DiffDiff options will be used to identify the changes that have been done in the *.sql file in the host directory and the actual structure in the production.
LintLint is used for parsing the changes done in the *.sql before pushing it to the server.
It will parse the query by executing it in the skeem_tmp database. Once the parsing is done, it will drop the database automatically.
PushOnce the changes are confirmed publish, By using the push command. We can push the changes to the server.
PullIf any of the changes are done directly to the server not via the Skeema tool. So the structure in the *.sql file won’t be updated. To make it an update, we can use the pull option. It will pull the latest structure from the serve


基本操作:

包含在里面:

root@localhost:/home/mydbops/skeema# skeema init -h *.*.*.* -u test -p -d production
Enter password:
2022-07-26 15:06:21 [INFO]  Using host dir /home/mydbops/skeema/production for *.*.*.*:3306

2022-07-26 15:06:21 [INFO]  Populating /home/mydbops/skeema/production/mysqldiff
2022-07-26 15:06:21 [INFO]  Created /home/mydbops/skeema/production/mysqldiff/mysqldiff_test.sql (191 bytes)

2022-07-26 15:06:21 [INFO]  Populating /home/mydbops/skeema/production/mydbops
2022-07-26 15:06:21 [INFO]  Created /home/mydbops/skeema/production/mydbops/sbtest.sql (271 bytes)

2022-07-26 15:06:21 [INFO]  Populating /home/mydbops/skeema/production/test
2022-07-26 15:06:21 [INFO]  Created /home/mydbops/skeema/production/test/message.sql (16649 bytes)


我们正在使用 Skeema 工具初始化服务器。因此主机目录被创建为“生产”,我们可以使用-d选项来决定目录名称,或者将根据主机名创建目录。

在主机目录中,将创建 *.sql 文件和 .skeema 文件。

主机目录:

root@localhost:/home/mydbops/skeema# ls -ltrh
total 8.0K
drwxr-xr-x 10 root root 4.0K Jul 26 15:04 production

root@localhost:/home/mydbops/skeema/production# ls -ltrh
total 32K
drwxr-xr-x 2 root root 4.0K Jul 26 15:06 mysqldiff
drwxr-xr-x 2 root root 4.0K Jul 26 15:06 sbtest
drwxr-xr-x 2 root root 4.0K Jul 26 15:06 test
drwxr-xr-x 2 root root 4.0K Jul 26 15:10 mydbops

root@localhost:/home/mydbops/skeema/production# cat .skeema
generator=skeema:1.7.1-community

[production]
flavor=percona:5.7
host=*.*.*.*
port=3306
user=test
password="*.*.*.*"

root@localhost:/home/mydbops/skeema/production/mydbops# ls -al
total 16
drwxr-xr-x  2 root root 4096 Jul 26 15:10 .
drwxr-xr-x 10 root root 4096 Jul 26 15:06 ..
-rw-r--r--  1 root root   80 Jul 26 15:06 .skeema
-rw-r--r--  1 root root  288 Jul 26 15:10 sbtest.sql
root@localhost:/home/mydbops/skeema/production/mydbops#
root@localhost:/home/mydbops/skeema/production/mydbops# pwd
/home/mydbops/skeema/production/mydbops
root@localhost:/home/mydbops/skeema/production/mydbops# cat sbtest.sql
CREATE TABLE `sbtest` (
  `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
  `k` int(10) unsigned NOT NULL DEFAULT '0',
  `c` char(120) NOT NULL DEFAULT '',
  `pad` char(60) NOT NULL DEFAULT '',
  PRIMARY KEY (`id`),
  KEY `k` (`k`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;


.Skeema 文件具有元数据,*.sql 文件具有表结构。如果我们需要使用 Skeema 工具对任何表执行 DDL,我们需要在 *.sql 文件中对其进行修改。

差异:

Diff 用于通过将 *.sql 文件中的结构与服务器中的当前结构进行比较来识别我们对 *.sql 文件所做的修改。

root@localhost:/home/mydbops/skeema# skeema diff
2022-07-26 15:55:59 [INFO]  Generating diff of *.*.*.*:3306 mysqldiff vs /home/mydbops/skeema/production/mysqldiff/*.sql
2022-07-26 15:55:59 [INFO]  *.*.*.*:3306 mysqldiff: No differences found

2022-07-26 15:55:59 [INFO]  Generating diff of *.*.*.*:3306 mydbops vs /home/mydbops/skeema/production/mydbops/*.sql
-- instance: *.*.*.*:3306
USE `mydbops`;
\! /usr/bin/pt-online-schema-change --execute --alter 'ADD KEY `c` (`c`)' D=mydbops,t=sbtest,h=*.*.*.*,P=3306,u=test,p=XXXXX
2022-07-26 15:55:59 [INFO]  *.*.*.*:3306 mydbops: diff complete

2022-07-26 15:55:59 [INFO]  Generating diff of *.*.*.*:3306 sbtest vs /home/mydbops/skeema/production/sbtest/*.sql
2022-07-26 15:55:59 [INFO]  *.*.*.*:3306 sbtest: No differences found

2022-07-26 15:55:59 [INFO]  Generating diff of *.*.*.*:3306 test vs /home/mydbops/skeema/production/test/*.sql
2022-07-26 15:55:59 [INFO]  *.*.*.*:3306 test: No differences found

我已经在 sbtest.sql 中进行了修改,因此 diff 命令会识别它并共享示例命令以像干运行一样执行。

PUSH:

确认更改后,将它们部署在服务器上。我们需要使用 push 命令在服务器上执行 DDL。

root@localhost:/home/mydbops/skeema/production# skeema push
2022-07-26 16:32:51 [INFO]  Pushing changes from /home/mydbops/skeema/production/mysqldiff/*.sql to *.*.*.*:3306 mysqldiff
2022-07-26 16:32:51 [INFO]  *.*.*.*:3306 mysqldiff: No differences found

2022-07-26 16:32:51 [INFO]  Pushing changes from /home/mydbops/skeema/production/mydbops/*.sql to *.*.*.*:3306 mydbops
-- instance: *.*.*.*:3306
USE `mydbops`;
\! /usr/bin/pt-online-schema-change --execute --alter 'ADD KEY `c` (`c`)' D=mydbops,t=sbtest,h=*.*.*.*,P=3306,u=test,p=XXXXX
perl: warning: Setting locale failed.
perl: warning: Please check that your locale settings:
	LANGUAGE = (unset),
	LC_ALL = (unset),
	LC_CTYPE = "UTF-8",
	LC_TERMINAL_VERSION = "3.4.15",
	LC_TERMINAL = "iTerm2",
	LANG = (unset)
    are supported and installed on your system.
perl: warning: Falling back to the standard locale ("C").
Found 2 slaves:
  warehouse-b2b-db-playground-none-8491856
  warehouse-b2b-db-playground-none-8491857
Will check slave lag on:
  warehouse-b2b-db-playground-none-8491856
  warehouse-b2b-db-playground-none-8491857
Operation, tries, wait:
  analyze_table, 10, 1
  copy_rows, 10, 0.25
  create_triggers, 10, 1
  drop_triggers, 10, 1
  swap_tables, 10, 1
  update_foreign_keys, 10, 1
Altering `mydbops`.`sbtest`...
Creating new table...
Created new table mydbops._sbtest_new OK.
Altering new table...
Altered `mydbops`.`_sbtest_new` OK.
2022-07-26T16:35:05 Creating triggers...
2022-07-26T16:35:05 Created triggers OK.
2022-07-26T16:35:05 Copying approximately 1972656 rows...
2022-07-26T16:35:29 Copied rows OK.
2022-07-26T16:35:29 Analyzing new table...
2022-07-26T16:35:29 Swapping tables...
2022-07-26T16:35:29 Swapped original and new tables OK.
2022-07-26T16:35:29 Dropping old table...
2022-07-26T16:35:29 Dropped old table `mydbops`.`_sbtest_old` OK.
2022-07-26T16:35:29 Dropping triggers...
2022-07-26T16:35:29 Dropped triggers OK.
Successfully altered `mydbops`.`sbtest`.
2022-07-26 16:35:29 [INFO]  *.*.*.*:3306 mydbops: push complete

2022-07-26 16:35:29 [INFO]  Pushing changes from /home/mydbops/skeema/production/sbtest/*.sql to *.*.*.*:3306 sbtest
2022-07-26 16:35:29 [INFO]  *.*.*.*:3306 sbtest: No differences found

2022-07-26 16:35:29 [INFO]  Pushing changes from /home/mydbops/skeema/production/test/*.sql to *.*.*.*:3306 test
2022-07-26 16:35:29 [INFO]  *.*.*.*:3306 test: No differences found

我已经在 sbtest.sql 中进行了修改,因此 diff 命令会识别它并共享示例命令以像干运行一样执行。

PULL:

确认更改后,将它们部署在服务器上。我们需要使用 push 命令在服务器上执行 DDL。

root@localhost:/home/mydbops/skeema# skeema pull
2022-07-26 16:40:01 [INFO]  Updating /home/mydbops/skeema/production/mysqldiff to reflect *.*.*.*:3306 mysqldiff

2022-07-26 16:40:01 [INFO]  Updating /home/mydbops/skeema/production/mydbops to reflect *.*.*.*:3306 mydbops
2022-07-26 16:40:01 [INFO]  Wrote /home/mydbops/skeema/production/mydbops/sbtest.sql (271 bytes)

2022-07-26 16:40:01 [INFO]  Updating /home/mydbops/skeema/production/sbtest to reflect *.*.*.*:3306 sbtest

2022-07-26 16:40:01 [INFO]  Updating /home/mydbops/skeema/production/test to reflect *.*.*.*:3306 test

Lint:

Lint 用于解析应用于 .sql 文件的更改,方法是在名为 _skeema_temp 的临时模式中执行它,一旦解析完成。架构将被自动删除。


root@warehouse-b2b-db-playground-none-8448763:/home/mydbops/skeema/production# skeema lint
2022-07-29 18:38:40 [INFO]  Linting /home/mydbops/skeema/production
2022-07-29 18:38:40 [INFO]  Linting /home/mydbops/skeema/production/mysqldiff
2022-07-29 18:38:40 [INFO]  Linting /home/mydbops/skeema/production/mydbops
"2022-07-29 18:38:40 [ERROR] /home/mydbops/skeema/production/mydbops/sbtest.sql:9: SQL syntax error: 
Error 1064: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server 
version for the right syntax to use near ') ENGINE=InnoDB DEFAULT CHARSET=latin1' at line 9"
2022-07-29 18:38:40 [INFO]  Linting /home/mydbops/skeema/production/sbtest
2022-07-29 18:38:51 [INFO]  Linting /home/mydbops/skeema/production/test
2022-07-29 18:38:54 [ERROR] Found 1 error and 0 warnings
root@warehouse-b2b-db-playground-none-8448763:/home/mydbops/skeema/production#

概括 :

在本篇博客中,我们介绍了基本操作和 skeema 工具,以全面了解它。在即将发布的博客中,我们将了解如何根据生产案例使用 Skeema 工具,以及添加任何一种方式来执行更改。


原文标题:MySQL Schema change With Skeema – Part 1 “Basic Operations”

原文作者:Praveen GR

原文链接:https://mydbops.wordpress.com/2022/09/14/mysql-schema-change-with-skeema-part-1-basic-operations/

「喜欢这篇文章,您的关注和赞赏是给作者最好的鼓励」
关注作者
【版权声明】本文为墨天轮用户原创内容,转载时必须标注文章的来源(墨天轮),文章链接,文章作者等基本信息,否则作者和墨天轮有权追究责任。如果您发现墨天轮中有涉嫌抄袭或者侵权的内容,欢迎发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。

评论