TiDB Binlog 集群部署

推荐使用 TiUP 部署 TiDB Binlog,详细部署方式参考 。

  1. 以 TiDB 用户登录中控机并进入 /home/tidb 目录。以下为 TiDB Ansible 分支与 TiDB 版本的对应关系,版本选择可咨询官方 info@pingcap.com。

    | TiDB Ansible 分支 | TiDB 版本 | 备注 | | ———————— | ————- | —- | | master | master 版本 | 包含最新特性,每日更新。 |

  2. 使用以下命令从 GitHub TiDB Ansible 项目上下载 TiDB Ansible 相应分支,默认的文件夹名称为 tidb-ansible

    • 下载 master 版本:

  1. 修改 tidb-ansible/inventory.ini 文件

    1. 设置 enable_binlog = True,表示 TiDB 集群开启 binlog。

      1. ## binlog trigger
      2. enable_binlog = True
    2. pump_servers 主机组添加部署机器 IP。

      1. ## Binlog Part
      2. [pump_servers]
      3. 172.16.10.72
      4. 172.16.10.73
      5. 172.16.10.74

      默认 Pump 保留 7 天数据,如需修改可修改 tidb-ansible/conf/pump.yml(TiDB 3.0.0~3.0.2 版本中为 tidb-ansible/conf/pump-cluster.yml)文件中 gc 变量值,并取消注释。

      1. global:
      2. # an integer value to control the expiry date of the binlog data, which indicates for how long (in days) the binlog data would be stored
      3. # must be bigger than 0
      4. # gc: 7

      请确保部署目录有足够空间存储 binlog,详见,也可为 Pump 设置单独的部署目录。

      1. ## Binlog Part
      2. [pump_servers]
      3. pump1 ansible_host=172.16.10.72 deploy_dir=/data1/pump
      4. pump2 ansible_host=172.16.10.73 deploy_dir=/data2/pump
      5. pump3 ansible_host=172.16.10.74 deploy_dir=/data3/pump
  2. 部署并启动含 Pump 组件的 TiDB 集群

    参照上文配置完 inventory.ini 文件后,从以下两种方式中选择一种进行部署。

    方式一:在已有的 TiDB 集群上增加 Pump 组件,需按以下步骤逐步进行。

    1. 部署 pump_servers 和 node_exporters

      1. ansible-playbook deploy.yml --tags=pump -l ${pump1_ip},${pump2_ip},[${alias1_name},${alias2_name}]
    2. 启动 pump_servers

      1. ansible-playbook start.yml --tags=pump
    3. 更新并重启 tidb_servers

      1. ansible-playbook rolling_update.yml --tags=tidb
    4. 更新监控信息

      1. ansible-playbook rolling_update_monitor.yml --tags=prometheus

      方式二:从零开始部署含 Pump 组件的 TiDB 集群

      使用 TiDB Ansible 部署 TiDB 集群,方法参考 使用 TiDB Ansible 部署 TiDB 集群

  3. 查看 Pump 服务状态

    使用 binlogctl 查看 Pump 服务状态,pd-urls 参数请替换为集群 PD 地址,结果 State 为 online 表示 Pump 启动成功。

    1. cd /home/tidb/tidb-ansible &&
    2. resources/bin/binlogctl -pd-urls=http://172.16.10.72:2379 -cmd pumps
  1. Drainer 初次启动时需要获取 initial_commit_ts 这个时间戳信息。

    • 如果从最近的时间点开始同步,initial_commit_ts 使用 -1 即可。

    • 如果下游为 MySQL 或 TiDB,为了保证数据的完整性,需要进行全量数据的备份与恢复。此时 initial_commit_ts 的值必须是全量备份的时间戳。

      如果使用 mydumper 进行全量备份,可以在导出目录中找到 metadata 文件,其中的 Pos 字段值即全量备份的时间戳。metadata 文件示例如下:

      1. Started dump at: 2019-12-30 13:25:41
      2. Log: tidb-binlog
      3. Pos: 413580274257362947
      4. GTID:
      5. Finished dump at: 2019-12-30 13:25:41
  2. 修改 tidb-ansible/inventory.ini 文件

    drainer_servers 主机组添加部署机器 IP,initial_commit_ts 请设置为获取的 initial_commit_ts,仅用于 Drainer 第一次启动。

    • 以下游为 MySQL 为例,别名为 drainer_mysql

      1. [drainer_servers]
      2. drainer_mysql ansible_host=172.16.10.71 initial_commit_ts="402899541671542785"
    • 以下游为 file 为例,别名为 drainer_file

      1. [drainer_servers]
      2. drainer_file ansible_host=172.16.10.71 initial_commit_ts="402899541671542785"
  3. 修改配置文件

    • 以下游为 MySQL 为例

      1. cd /home/tidb/tidb-ansible/conf &&
      2. cp drainer.toml drainer_mysql_drainer.toml &&
      3. vi drainer_mysql_drainer.toml

      db-type 设置为 “mysql”, 配置下游 MySQL 信息。

      1. [syncer]
      2. # downstream storage, equal to --dest-db-type
      3. # Valid values are "mysql", "file", "tidb", "kafka".
      4. db-type = "mysql"
      5. # the downstream MySQL protocol database
      6. [syncer.to]
      7. host = "172.16.10.72"
      8. user = "root"
      9. password = "123456"
      10. port = 3306
    • 以下游为增量备份文件为例

      1. cd /home/tidb/tidb-ansible/conf &&
      2. cp drainer.toml drainer_file_drainer.toml &&
      3. vi drainer_file_drainer.toml

      db-type 设置为 “file”。

      1. [syncer]
      2. # downstream storage, equal to --dest-db-type
      3. # Valid values are "mysql", "file", "tidb", "kafka".
      4. db-type = "file"
      5. # Uncomment this if you want to use "file" as "db-type".
      6. [syncer.to]
      7. # default data directory: "{{ deploy_dir }}/data.drainer"
      8. dir = "data.drainer"
  4. 部署 Drainer

    1. ansible-playbook deploy_drainer.yml
  5. 启动 Drainer

    1. ansible-playbook start_drainer.yml

检查文件完整性,返回 ok 则正确:

  1. sha256sum -c tidb-{version}-linux-amd64.sha256

对于 v2.1.0 GA 及以上版本,Pump 和 Drainer 已经包含在 TiDB 的下载包中,其他版本需要单独下载 Pump 和 Drainer:

  1. wget https://download.pingcap.org/tidb-binlog-latest-linux-amd64.tar.gz &&
  2. wget https://download.pingcap.org/tidb-binlog-latest-linux-amd64.sha256

检查文件完整性,返回 ok 则正确:

  1. sha256sum -c tidb-binlog-latest-linux-amd64.sha256

假设有三个 PD,一个 TiDB,另外有两台机器用于部署 Pump,一台机器用于部署 Drainer。各个节点信息如下:

  1. TiDB="192.168.0.10"
  2. PD1="192.168.0.16"
  3. PD2="192.168.0.15"
  4. PD3="192.168.0.14"
  5. Pump="192.168.0.12"
  6. Drainer="192.168.0.13"

下面以此为例,说明 Pump/Drainer 的使用。

  1. 使用 binary 部署 Pump

      1. Usage of Pump:
      2. -L string
      3. 日志输出信息等级设置:debuginfowarnerrorfatal (默认 "info")
      4. -V
      5. 打印版本信息
      6. -addr string
      7. Pump 提供服务的 RPC 地址(-addr="192.168.0.11:8250")
      8. -advertise-addr string
      9. Pump 对外提供服务的 RPC 地址(-advertise-addr="192.168.0.11:8250")
      10. -config string
      11. 配置文件路径,如果你指定了配置文件,Pump 会首先读取配置文件的配置;
      12. 如果对应的配置在命令行参数里面也存在,Pump 就会使用命令行参数的配置来覆盖配置文件里的配置。
      13. -data-dir string
      14. Pump 数据存储位置路径
      15. -gc int
      16. Pump 只保留多少天以内的数据 (默认 7)
      17. -heartbeat-interval int
      18. Pump PD 发送心跳间隔 (单位 秒)
      19. -log-file string
      20. log 文件路径
      21. -log-rotate string
      22. log 文件切换频率,hour/day
      23. -metrics-addr string
      24. Prometheus Pushgateway 地址,不设置则禁止上报监控信息
      25. -metrics-interval int
      26. 监控信息上报频率 (默认 15,单位 秒)
      27. -node-id string
      28. Pump 节点的唯一识别 ID,如果不指定,程序会根据主机名和监听端口自动生成
      29. -pd-urls string
      30. PD 集群节点的地址 (-pd-urls="http://192.168.0.16:2379,http://192.168.0.15:2379,http://192.168.0.14:2379")
      31. -fake-binlog-interval int
      32. Pump 节点生成 fake binlog 的频率 (默认 3,单位 秒)
    • Pump 配置文件(以在 “192.168.0.11” 上部署为例)

      1. # Pump Configuration
      2. # Pump 绑定的地址
      3. addr = "192.168.0.11:8250"
      4. # Pump 对外提供服务的地址
      5. advertise-addr = "192.168.0.11:8250"
      6. # Pump 只保留多少天以内的数据 (默认 7)
      7. gc = 7
      8. # Pump 数据存储位置路径
      9. data-dir = "data.pump"
      10. # Pump 向 PD 发送心跳的间隔 (单位 秒)
      11. heartbeat-interval = 2
      12. # PD 集群节点的地址 (英文逗号分割,中间不加空格)
      13. pd-urls = "http://192.168.0.16:2379,http://192.168.0.15:2379,http://192.168.0.14:2379"
      14. # [security]
      15. # 如无特殊安全设置需要,该部分一般都注解掉
      16. # 包含与集群连接的受信任 SSL CA 列表的文件路径
      17. # ssl-ca = "/path/to/ca.pem"
      18. # ssl-cert = "/path/to/drainer.pem"
      19. # 包含与集群链接的 PEM 形式的 X509 key 的路径
      20. # ssl-key = "/path/to/drainer-key.pem"
      21. # [storage]
      22. # 设置为 true(默认值)来保证可靠性,确保 binlog 数据刷新到磁盘
      23. # sync-log = true
      24. # 当可用磁盘容量小于该设置值时,Pump 将停止写入数据
      25. # 42 MB -> 42000000, 42 mib -> 44040192
      26. # default: 10 gib
      27. # stop-write-at-available-space = "10 gib"
      28. # Pump 内嵌的 LSM DB 设置,除非对该部分很了解,否则一般注解掉
      29. # [storage.kv]
      30. # block-cache-capacity = 8388608
      31. # block-restart-interval = 16
      32. # block-size = 4096
      33. # compaction-L0-trigger = 8
      34. # compaction-table-size = 67108864
      35. # compaction-total-size = 536870912
      36. # compaction-total-size-multiplier = 8.0
      37. # write-buffer = 67108864
      38. # write-L0-pause-trigger = 24
      39. # write-L0-slowdown-trigger = 17
    • 启动示例

      1. ./bin/pump -config pump.toml

      如果命令行参数与配置文件中的参数重合,则使用命令行设置的参数的值。

  2. 使用 binary 部署 Drainer

    • Drainer 命令行参数说明(以在 “192.168.0.13” 上部署为例)

      1. Usage of Drainer
      2. 日志输出信息等级设置:debuginfowarnerrorfatal (默认 "info")
      3. -V
      4. 打印版本信息
      5. -addr string
      6. Drainer 提供服务的地址(-addr="192.168.0.13:8249")
      7. -c int
      8. 同步下游的并发数,该值设置越高同步的吞吐性能越好 (default 1)
      9. -cache-binlog-count int
      10. 缓存中的 binlog 数目限制(默认 8
      11. 如果上游的单个 binlog 较大导致 Drainer 出现 OOM 时,可尝试调小该值减少内存使用
      12. -config string
      13. 配置文件路径,Drainer 会首先读取配置文件的配置;
      14. 如果对应的配置在命令行参数里面也存在,Drainer 就会使用命令行参数的配置来覆盖配置文件里面的配置
      15. -data-dir string
      16. Drainer 数据存储位置路径 (默认 "data.drainer")
      17. -dest-db-type string
      18. Drainer 下游服务类型 (默认为 mysql,支持 tidbkafkafile)
      19. -detect-interval int
      20. PD 查询在线 Pump 的时间间隔 (默认 10,单位 秒)
      21. -disable-detect
      22. 是否禁用冲突监测
      23. -disable-dispatch
      24. 是否禁用拆分单个 binlog SQL 的功能,如果设置为 true,则每个 binlog
      25. 按顺序依次还原成单个事务进行同步(下游服务类型为 MySQL,该项设置为 False
      26. -ignore-schemas string
      27. db 过滤列表 (默认 "INFORMATION_SCHEMA,PERFORMANCE_SCHEMA,mysql,test"),
      28. 不支持对 ignore schemas table 进行 rename DDL 操作
      29. -initial-commit-ts(默认为 `-1`
      30. 如果 Drainer 没有相关的断点信息,可以通过该项来设置相关的断点信息
      31. 该参数值为 `-1` 时,Drainer 会自动从 PD 获取一个最新的时间戳
      32. -log-file string
      33. log 文件路径
      34. -log-rotate string
      35. log 文件切换频率,hour/day
      36. -metrics-addr string
      37. Prometheus Pushgateway 地址,不设置则禁止上报监控信息
      38. -metrics-interval int
      39. 监控信息上报频率(默认 15,单位:秒)
      40. -node-id string
      41. drainer 节点的唯一识别 ID,如果不指定,程序会根据主机名和监听端口自动生成
      42. -pd-urls string
      43. PD 集群节点的地址 (-pd-urls="http://192.168.0.16:2379,http://192.168.0.15:2379,http://192.168.0.14:2379")
      44. -safe-mode
      45. 是否开启安全模式使得下游 MySQL/TiDB 可被重复写入
      46. 即将 insert 语句换为 replace 语句,将 update 语句拆分为 delete + replace 语句
      47. -txn-batch int
      48. 输出到下游数据库一个事务的 SQL 数量(默认 1
    • Drainer 配置文件(以在 “192.168.0.13” 上部署为例)

      ```toml

      Drainer Configuration.

      Drainer 提供服务的地址(“192.168.0.13:8249”)

      addr = “192.168.0.13:8249”

      Drainer 对外提供服务的地址

      advertise-addr = “192.168.0.13:8249”

      向 PD 查询在线 Pump 的时间间隔 (默认 10,单位 秒)

      detect-interval = 10

      Drainer 数据存储位置路径 (默认 “data.drainer”)

      data-dir = “data.drainer”

      PD 集群节点的地址 (英文逗号分割,中间不加空格)

      pd-urls = ““

      log 文件路径

      log-file = “drainer.log”

      Drainer 从 Pump 获取 binlog 时对数据进行压缩,值可以为 “gzip”,如果不配置则不进行压缩

      compressor = “gzip”

      [security]

      如无特殊安全设置需要,该部分一般都注解掉

      包含与集群连接的受信任 SSL CA 列表的文件路径

      ssl-ca = “/path/to/ca.pem”

      包含与集群连接的 PEM 形式的 X509 certificate 的路径

      ssl-cert = “/path/to/pump.pem”

      包含与集群链接的 PEM 形式的 X509 key 的路径

      ssl-key = “/path/to/pump-key.pem”

      Syncer Configuration

      [syncer]

      如果设置了该项,会使用该 sql-mode 解析 DDL 语句,此时如果下游是 MySQL 或 TiDB 则

      下游的 sql-mode 也会被设置为该值

      sql-mode = “STRICT_TRANS_TABLES,NO_ENGINE_SUBSTITUTION”

      输出到下游数据库一个事务的 SQL 语句数量 (默认 20)

      txn-batch = 20

      同步下游的并发数,该值设置越高同步的吞吐性能越好 (默认 16)

      worker-count = 16

      是否禁用拆分单个 binlog 的 SQL 的功能,如果设置为 true,则按照每个 binlog

      顺序依次还原成单个事务进行同步(下游服务类型为 MySQL, 该项设置为 False)

      disable-dispatch = false

      safe mode 会使写下游 MySQL/TiDB 可被重复写入

      会用 replace 替换 insert 语句,用 delete + replace 替换 update 语句

      safe-mode = false

      Drainer 下游服务类型(默认为 mysql)

      参数有效值为 “mysql”,”tidb”,”file”,”kafka”

      db-type = “mysql”

      事务的 commit ts 若在该列表中,则该事务将被过滤,不会同步至下游

      ignore-txn-commit-ts = []

      db 过滤列表 (默认 “INFORMATION_SCHEMA,PERFORMANCE_SCHEMA,mysql,test”),

      不支持对 ignore schemas 的 table 进行 rename DDL 操作

      ignore-schemas = “INFORMATION_SCHEMA,PERFORMANCE_SCHEMA,mysql”

      replicate-do-db 配置的优先级高于 replicate-do-table。如果配置了相同的库名,支持使用正则表达式进行配置。

      以 ‘~’ 开始声明使用正则表达式

      replicate-do-db = [“~^b.*”,”s1”]

      [syncer.relay]

      保存 relay log 的目录,空值表示不开启。

      只有下游是 TiDB 或 MySQL 时该配置才生效。

      log-dir = “”

      每个文件的大小上限

      max-file-size = 10485760

      [[syncer.replicate-do-table]]

      db-name =”test”

      tbl-name = “log”

      [[syncer.replicate-do-table]]

      db-name =”test”

      tbl-name = “~^a.*”

      忽略同步某些表

      [[syncer.ignore-table]]

      db-name = “test”

      tbl-name = “log”

      db-type 设置为 mysql 时,下游数据库服务器参数

      [syncer.to] host = “192.168.0.13” user = “root” password = “”

      使用 ./binlogctl -cmd encrypt -text string 加密的密码

      encrypted_password 非空时 password 会被忽略

      encrypted_password = “” port = 3306

      当 checkpoint type 是 mysql 或 tidb 时可以开启该选项,以改变保存 checkpoint 的数据库

      schema = “tidb_binlog”

      目前只支持 mysql 或者 tidb 类型。可以去掉注释来控制 checkpoint 保存的位置。

      db-type 默认的 checkpoint 保存方式是:

      mysql/tidb -> 对应的下游 mysql/tidb

      file/kafka -> file in data-dir

      type = “mysql”

      host = “127.0.0.1”

      user = “root”

      password = “”

      使用 ./binlogctl -cmd encrypt -text string 加密的密码

      encrypted_password 非空时 password 会被忽略

      encrypted_password = “”

      port = 3306

  1. # db-type 设置为 file 时,存放 binlog 文件的目录
  2. # [syncer.to]
  3. # dir = "data.drainer"
  4. # db-type 设置为 kafka 时,Kafka 相关配置
  5. # [syncer.to]
  6. # kafka-addrs 和 zookeeper-addrs 只需要一个,两者都有时程序会优先用 zookeeper 中的 kafka 地址
  7. # zookeeper-addrs = "127.0.0.1:2181"
  8. # kafka-addrs = "127.0.0.1:9092"
  9. # kafka-version = "0.8.2.0"
  10. # kafka-max-messages = 1024
  11. # 保存 binlog 数据的 Kafka 集群的 topic 名称,默认值为 <cluster-id>_obinlog
  12. # 如果运行多个 Drainer 同步数据到同一个 Kafka 集群,每个 Drainer 的 topic-name 需要设置不同的名称
  13. # topic-name = ""
  14. ```
  15. - 启动示例
  16. > **注意:**
  17. >
  18. > 如果下游为 MySQL/TiDB,为了保证数据的完整性,在 Drainer 初次启动前需要获取 `initial-commit-ts` 的值,并进行全量数据的备份与恢复。详细信息参见[部署 Drainer](#第-3-步部署-drainer)。
  19. 初次启动时使用参数 `initial-commit-ts`, 命令如下:
  20. <!-- copyable "shell-regular" -->
  21. ```bash
  22. ./bin/drainer -config drainer.toml -initial-commit-ts {initial-commit-ts}
  23. ```
  24. 如果命令行参数与配置文件中的参数重合,则使用命令行设置的参数的值。