gravity mysql_gravity 使用操作。

gravity 使用操作。

最近我司有一个比较奇葩的需求,我们的环境是主从,因为数据量较大会定期的删除数据,

最近不行了,要求新建出来一个库 同步正事环境的数据,但是要剔除 delete ,drop,truncat 等这些删除数据的语句。

因此 准备使用gravity 来进行数据操作。看看能否满足这些需求。

这个环境实现的是mysql ---------》 mysql的数据同步(剔除drop,truncate,delete语句);

mysql:192.168.17.21(主)

mysql:192.168.17.23(辅)

gravity:192.168.17.20

mysql 如果不会安装的话 可以使用 https://www.cnblogs.com/noel/p/10314125.html 脚本安装

在192.168.17.20 配置go语言环境:

https://golang.org/dl/ 下载linux 安装包

上传到服务器 /usr/local/src/ 目录下:

tar -xvzf go1.11.5.linux-amd64.tar.gz -C /usr/local/

export PATH=$PATH:/usr/local/go/bin

或者

vim /etc/profile

查看 gopath

go env

source /etc/profile

编译 (TODO: 开源后直接从 github 下载 binary)

mkdir -p $GOPATH/src/github.com/moiot/

cd $GOPATH/src/github.com/moiot/

git clone https://github.com/moiot/gravity.git

cd gravity/ && make

可能会出现以下错误:

错误1:

[root@localhost moiot]# git clone https://github.com/moiot/gravity.git

Initialized empty Git repository in /usr/local/go/bin/src/github.com/moiot/gravity/.git/

error: while accessing https://github.com/moiot/gravity.git/info/refs

fatal: HTTP request failed

这是因为系统自带的git的版本太低了 需要升级git:

升级git: 下载相应的安装包

yum install curl-devel expat-devel gettext-devel openssl-devel zlib-devel asciidoc

yum install gcc perl-ExtUtils-MakeMaker

移除系统自带的git

yum remove git

安装新版git

cd /usr/local/src/

wget https://www.kernel.org/pub/software/scm/git/git-2.9.5.tar.xz

tar -xvz git-2.9.5.tar.xz

cd git-2.9.5

./configure --prefix=/usr/local/git

make && make install

echo "export PATH=$PATH:/usr/local/git/bin" >>/etc/profile

source /etc/profile

错误2:

[root@localhost moiot]# git clone https://github.com/moiot/gravity.git

Cloning into 'gravity'...

fatal: unable to access 'https://github.com/moiot/gravity.git/': SSL connect error

[root@localhost moiot]#

解决办法 yum update nss

分别在mysql:192.168.17.21(主) mysql:192.168.17.23(辅)执行一下命令

CREATE USER _gravity IDENTIFIED BY '_gravity';

GRANT SELECT, RELOAD, LOCK TABLES, REPLICATION SLAVE, REPLICATION CLIENT, CREATE, INSERT, UPDATE, DELETE ON *.* TO '_gravity'@'%';

GRANT ALL PRIVILEGES ON _gravity.* TO '_gravity'@'%';

flush privileges;

创建如下配置文件 mysql-mysql.toml

cat > mysql-mysql.toml << "EOF"

name = "mysql-mysql"

#

# Input 插件的定义,此处定义使用 mysql

#

[input]

type = "mysql"

mode = "replication" ##全量+增量(replication),增量(stream),全量(batch)

[input.config]

#是否忽略双向同步产生的内部数据,默认是false

ignore-bidirectional-data = false

# 总体扫描的并发线程数

# - 默认为 10,表示最多允许 10 个表同时扫描

# - 可选

nr-scanner = 10

# 单次扫描所去的行数

# - 默认为 10000,表示一次拉取 10000 行

# - 可选

table-scan-batch = 10000

# 全局限制,每秒所允许的 batch 数

# - 默认为 1

# - 可选

#

batch-per-second-limit = 1

# 全局限制,没有找到单列主键、唯一索引时,最多多少行的表可用全表扫描方式读取,否则报错退出。

# - 默认为 100,000

# - 可选

#

max-full-dump-count = 10000

[input.config.source]

host = "192.168.17.21"

username = "_gravity"

password = "_gravity"

port = 3306

# 需要扫描的表

# - 必填 多个schema的话 写多个configs

[[input.config.table-configs]]

schema = "test_gravity"

table = "*"

#

# Output 插件的定义,此处使用 mysql

#

[output]

type = "mysql"

#

[output.config]

enable-ddl = true #当前支持 create & alter table 语句。库表名会根据路由信息调整。

[output.config.target]

host = "192.168.17.23"

username = "_gravity"

password = "_gravity"

port = 3306

# 路由规则的定义

[[output.config.routes]]

match-schema = "test_gravity"

match-table = "*"

target_schema = "test_gravity"

target-table = "*"

[output.config.execution-engine]

# 开启双向同步标识的写入

use-bidirection = false

[[filters]]

type = "reject"

[filters.config]

match-schema = "test_gravity"

match-dml-op = "delete"

EOF

然后启动 :

bin/gravity --config mysql-mysql.toml

########遇到的一个坑:

binlog_row_image 必须设置成FULL

create database 操作需要两边都做

delete 可以剔除

drop,truncate 不支持 很好用

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值