TiDB Binlog 集群部署

使用 TiDB Ansible 部署 TiDB Binlog

第 1 步:下载 TiDB Ansible

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

    | TiDB Ansible 分支 | TiDB 版本 | 备注 | | ———————— | ————- | —- | | release-2.0-new-binlog | 2.0 版本 | 最新 2.0 稳定版本,可用于生产环境。 | | release-2.1 | 2.1 版本 | 最新 2.1 稳定版本,可用于生产环境(建议)。 | | master | master 版本 | 包含最新特性,每日更新。 |

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

    • 下载 2.0 版本:

      1. $ git clone -b release-2.0-new-binlog https://github.com/pingcap/tidb-ansible.git
    • 下载 2.1 版本:

      1. $ git clone -b release-2.1 https://github.com/pingcap/tidb-ansible.git
    • 下载 master 版本:

      1. $ git clone https://github.com/pingcap/tidb-ansible.git

第 2 步:部署 Pump

  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 文件中 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 -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 集群

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

  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
    3. INFO[0000] pump: {NodeID: ip-172-16-10-72:8250, Addr: 172.16.10.72:8250, State: online, MaxCommitTS: 403051525690884099, UpdateTime: 2018-12-25 14:23:37 +0800 CST}
    4. INFO[0000] pump: {NodeID: ip-172-16-10-73:8250, Addr: 172.16.10.73:8250, State: online, MaxCommitTS: 403051525703991299, UpdateTime: 2018-12-25 14:23:36 +0800 CST}
    5. INFO[0000] pump: {NodeID: ip-172-16-10-74:8250, Addr: 172.16.10.74:8250, State: online, MaxCommitTS: 403051525717360643, UpdateTime: 2018-12-25 14:23:35 +0800 CST}

第 3 步:部署 Drainer

  1. 获取 initial_commit_ts

    使用 binlogctl 工具生成 Drainer 初次启动所需的 tso 信息,命令:

    1. $ cd /home/tidb/tidb-ansible
    2. $ resources/bin/binlogctl -pd-urls=http://127.0.0.1:2379 -cmd generate_meta
    3. INFO[0000] [pd] create pd client with endpoints [http://192.168.199.118:32379]
    4. INFO[0000] [pd] leader switches to: http://192.168.199.118:32379, previous:
    5. INFO[0000] [pd] init cluster id 6569368151110378289
    6. 2018/06/21 11:24:47 meta.go:117: [info] meta: &{CommitTS:400962745252184065}

    该命令会输出 meta: &{CommitTS:400962745252184065},CommitTS 的值作为 Drainer 初次启动使用的 initial-commit-ts 参数的值。

  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

      注意:

      配置文件名命名规则为 别名_drainer.toml,否则部署时无法找到自定义配置文件。

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

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

      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. # downstream storage, equal to --dest-db-type
      2. # Valid values are "mysql", "file", "kafka", "flash".
      3. db-type = "file"
      4. # Uncomment this if you want to use `file` as `db-type`.
      5. # The value can be `gzip`. Leave it empty to disable compression.
      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

使用 Binary 部署 TiDB Binlog

下载官方 Binary

  1. wget https://download.pingcap.org/tidb-{version}-linux-amd64.tar.gz
  2. wget https://download.pingcap.org/tidb-{version}-linux-amd64.sha256
  3. # 检查文件完整性,返回 ok 则正确
  4. 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
  3. # 检查文件完整性,返回 ok 则正确
  4. 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.11"
  6. Pump="192.168.0.12"
  7. Drainer="192.168.0.13"

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

  1. 使用 binary 部署 Pump

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

      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. -pd-urls string
      28. PD 集群节点的地址 (-pd-urls="http://192.168.0.16:2379,http://192.168.0.15:2379,http://192.168.0.14:2379")
    • 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. # [storage]
      15. # 设置为 true(默认值)来保证可靠性,确保 binlog 数据刷新到磁盘
      16. # sync-log = true
    • 启动示例

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

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

  2. 使用 binary 部署 Drainer

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

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

      1. # Drainer Configuration.
      2. # Drainer 提供服务的地址("192.168.0.13:8249")
      3. addr = "192.168.0.13:8249"
      4. # 向 PD 查询在线 Pump 的时间间隔 (默认 10,单位 秒)
      5. detect-interval = 10
      6. # Drainer 数据存储位置路径 (默认 "data.drainer")
      7. data-dir = "data.drainer"
      8. # PD 集群节点的地址
      9. pd-urls = "http://192.168.0.16:2379,http://192.168.0.15:2379,http://192.168.0.14:2379"
      10. # log 文件路径
      11. log-file = "drainer.log"
      12. # Drainer 从 Pump 获取 binlog 时对数据进行压缩,值可以为 "gzip",如果不配置则不进行压缩
      13. # compressor = "gzip"
      14. # Syncer Configuration
      15. [syncer]
      16. # 如果设置了该项,会使用该 sql-mode 解析 DDL 语句
      17. # sql-mode = "STRICT_TRANS_TABLES,NO_ENGINE_SUBSTITUTION"
      18. # 输出到下游数据库一个事务的 SQL 语句数量 (默认 20)
      19. txn-batch = 20
      20. # 同步下游的并发数,该值设置越高同步的吞吐性能越好 (默认 16)
      21. worker-count = 16
      22. # 是否禁用拆分单个 binlog 的 SQL 的功能,如果设置为 true,则按照每个 binlog
      23. # 顺序依次还原成单个事务进行同步(下游服务类型为 MySQL, 该项设置为 False)
      24. disable-dispatch = false
      25. # Drainer 下游服务类型(默认为 mysql)
      26. # 参数有效值为 "mysql","file","kafka","flash"
      27. db-type = "mysql"
      28. # db 过滤列表 (默认 "INFORMATION_SCHEMA,PERFORMANCE_SCHEMA,mysql,test"),
      29. # 不支持对 ignore schemas 的 table 进行 rename DDL 操作
      30. ignore-schemas = "INFORMATION_SCHEMA,PERFORMANCE_SCHEMA,mysql"
      31. # replicate-do-db 配置的优先级高于 replicate-do-table。如果配置了相同的库名,支持使用正则表达式进行配置。
      32. # 以 '~' 开始声明使用正则表达式
      33. # replicate-do-db = ["~^b.*","s1"]
      34. # [[syncer.replicate-do-table]]
      35. # db-name ="test"
      36. # tbl-name = "log"
      37. # [[syncer.replicate-do-table]]
      38. # db-name ="test"
      39. # tbl-name = "~^a.*"
      40. # 忽略同步某些表
      41. # [[syncer.ignore-table]]
      42. # db-name = "test"
      43. # tbl-name = "log"
      44. # db-type 设置为 mysql 时,下游数据库服务器参数
      45. [syncer.to]
      46. host = "192.168.0.13"
      47. user = "root"
      48. password = ""
      49. port = 3306
      50. # db-type 设置为 file 时,存放 binlog 文件的目录
      51. # [syncer.to]
      52. # dir = "data.drainer"
      53. # db-type 设置为 kafka 时,Kafka 相关配置
      54. # [syncer.to]
      55. # zookeeper-addrs = "127.0.0.1:2181"
      56. # kafka-addrs = "127.0.0.1:9092"
      57. # kafka-version = "0.8.2.0"
      58. # 保存 binlog 数据的 Kafka 集群的 topic 名称,默认值为 <cluster-id>_obinlog
      59. # 如果运行多个 Drainer 同步数据到同一个 Kafka 集群,每个 Drainer 的 topic-name 需要设置不同的名称
      60. # topic-name = ""
    • 启动示例

      注意:

      如果下游为 MySQL/TiDB,为了保证数据的完整性,在 Drainer 初次启动前需要获取 initial-commit-ts 的值,并进行全量数据的备份与恢复。详细信息参见部署 Drainer

      初次启动时使用参数 initial-commit-ts, 命令如下:

      1. ./bin/drainer -config drainer.toml -initial-commit-ts {initial-commit-ts}

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

注意:

  • 在运行 TiDB 时,需要保证至少一个 Pump 正常运行。
  • 通过给 TiDB 增加启动参数 enable-binlog 来开启 binlog 服务。尽量保证同一集群的所有 TiDB 都开启了 binlog 服务,否则在同步数据时可能会导致上下游数据不一致。如果要临时运行一个不开启 binlog 服务的 TiDB 实例,需要在 TiDB 的配置文件中设置 run_ddl= false
  • Drainer 不支持对 ignore schemas(在过滤列表中的 schemas)的 table 进行 rename DDL 操作。
  • 在已有的 TiDB 集群中启动 Drainer,一般需要全量备份并且获取 savepoint,然后导入全量备份,最后启动 Drainer 从 savepoint 开始同步增量数据。