Maxwell安装部署

1 Maxwell输出格式

  • database:变更数据所属的数据库
  • table:变更数据所属的表
  • type:数据变更类型
  • ts:数据变更发生的时间
  • xid:事务id
  • commit:事务提交标志,可用于重新组装事务
  • data:对于insert类型,表示插入的数据;对于update类型,表示修改之后的数据;对于delete类型,表示删除的数据
  • old:对于update类习惯,表示修改之前的数据,只包含变更字段

2.Maxwell部署

2.1 安装Maxwell

(1)解压安装包

tar -zxvf maxwell-1.29.2.tar.gz -C /opt/module

(2)修改解压后的maxwell名字

mv maxwell-1.29.2/ maxwell

2.2 配置MySQL

2.2.1 判断MySQL是否已经开启binlog

MySQL服务器的Binlog默认是未开启的,如需进行同步,需要先进行开启

SHOW VARIABLES LIKE 'log_bin';

查看MySQL的binlog模式

show global variables like "binlog%";

开启binlog日志

修改 MySQL 的 my.cnf 配置文件

一般默认在 /etc/my.cnf 下

#第一种方式:
#开启binlog日志
log_bin=ON
#binlog日志的基本文件名
log_bin_basename=/var/lib/mysql/mysql-bin
#binlog文件的索引文件,管理所有binlog文件
log_bin_index=/var/lib/mysql/mysql-bin.index
#配置serverid
server-id=1

注:MySQL Binlog模式

Statement-based:基于语句,Binlog会记录所有写操作的SQL语句,包括insert、update、delete等。

优点: 节省空间

缺点: 有可能造成数据不一致,例如insert语句中包含now()函数。

Row-based:基于行,Binlog会记录每次写操作后被操作行记录的变化。

优点:保持数据的绝对一致性。

缺点:占用较大空间。

mixed:混合模式,默认是Statement-based,如果SQL语句可能导致数据不一致,就自动切换到Row-based。

Maxwell要求Binlog采用Row-based模式。

修改完配置后,重启MySQL。

重启MySQL服务

systemctl restart mysqld

执行 SHOW VARIABLES LIKE 'log_bin'; value值为 ON 即可。

2.2.2 创建Maxwell所需数据库和用户

Maxwell需要在MySQL中存储其运行过程中的所需的一些数据,包括binlog同步的断点位置(Maxwell支持断点续传)等等,故需要在MySQL为Maxwell创建数据库及用户。

(1)创建数据库
msyql> CREATE DATABASE maxwell;
(2)更改MySQL数据库密码级别(可选操作)
mysql> set global validate_password_policy=0;
mysql> set global validate_password_length=4;
(3)创建Maxwell用户并赋予必要权限
mysql> CREATE USER 'maxwell'@'%' IDENTIFIED BY 'abc';
mysql> GRANT ALL ON maxwell.* TO 'maxwell'@'%';
mysql> GRANT SELECT, REPLICATION CLIENT, REPLICATION SLAVE ON *.* TO 'maxwell'@'%';

2.3 配置Maxwell

(1)修改Maxwell配置文件

cd /opt/module/maxwell
cp config.properties.example config.properties

vim config.properties
 
#Maxwell数据发送目的地,可选配置有stdout|file|kafka|kinesis|pubsub|sqs|rabbitmq|redis
producer=kafka
#目标Kafka集群地址
kafka.bootstrap.servers=bigdata1:9092,bigdata2:9092,bigdata3:9092
#目标Kafka topic,可静态配置,例如:maxwell,也可动态配置,例如:%{database}_%{table}
kafka_topic=maxwell
 
#MySQL相关配置
host=bigdata1
user=maxwell
password=123456
jdbc_options=useSSL=false&serverTimezone=Asia/Shanghai

3.Maxwell使用

3.1启动Kafka集群

若Maxwell发送数据的目的地为Kafka集群,则需先确保Kafka集群为启动状态。

3.2 Maxwell启停

(1)启动

/opt/module/maxwell/bin/maxwell --config /opt/module/maxwell/config.properties --daemon

(2)停止Maxwell

ps -ef | grep maxwell | grep -v grep | grep maxwell | awk '{print $2}' | xargs kill -9

(3)Maxwell启停脚本

创建启停脚本

vim mxw.sh

内容

#!/bin/bash
 
MAXWELL_HOME=/opt/module/maxwell
 
status_maxwell(){
    result=`ps -ef | grep com.zendesk.maxwell.Maxwell | grep -v grep | wc -l`
    return $result
}
 
 
start_maxwell(){
    status_maxwell
    if [[ $? -lt 1 ]]; then
        echo "启动Maxwell"
        $MAXWELL_HOME/bin/maxwell --config $MAXWELL_HOME/config.properties --daemon
    else
        echo "Maxwell正在运行"
    fi
}
 
 
stop_maxwell(){
    status_maxwell
    if [[ $? -gt 0 ]]; then
        echo "停止Maxwell"
        ps -ef | grep com.zendesk.maxwell.Maxwell | grep -v grep | awk '{print $2}' | xargs kill -9
    else
        echo "Maxwell未在运行"
    fi
}
 
 
case $1 in
    start )
        start_maxwell
    ;;
    stop )
        stop_maxwell
    ;;
    restart )
       stop_maxwell
       start_maxwell
    ;;
esac

赋予权限

chmod 777 mxw.sh

3.3 增量数据同步

(1)启动Kafka消费者

bin/kafka-console-consumer.sh --bootstrap-server hadoop102:9092 --topic maxwell

(2)模拟生成数据

java -jar gmall2020-mock-db-2021-01-22.jar

(3)观察Kafka消费者

{"database":"gmall","table":"comment_info","type":"insert","ts":1634023510,"xid":1653373,"xoffset":11998,"data":{"id":1447825655672463369,"user_id":289,"nick_name":null,"head_img":null,"sku_id":11,"spu_id":3,"order_id":18440,"appraise":"1204","comment_txt":"评论内容:12897688728191593794966121429786132276125164551411","create_time":"2020-06-16 15:25:09","operate_time":null}}
{"database":"gmall","table":"comment_info","type":"insert","ts":1634023510,"xid":1653373,"xoffset":11999,"data":{"id":1447825655672463370,"user_id":774,"nick_name":null,"head_img":null,"sku_id":25,"spu_id":8,"order_id":18441,"appraise":"1204","comment_txt":"评论内容:67552221621263422568447438734865327666683661982185","create_time":"2020-06-16 15:25:09","operate_time":null}}

3.4 历史数据全量同步

可能需要使用到MySQL数据库中从历史至今的一个完整的数据集。这就需要我们在进行增量同步之前,先进行一次历史数据的全量同步。这样就能保证得到一个完整的数据集。

3.4.1Maxwell-bootstrap

历史数据的全量同步的命令

/opt/module/maxwell/bin/maxwell-bootstrap --database gmall --table user_info --config /opt/module/maxwell/config.properties

3.4.2 bootstrap数据格式

采用bootsrtap方式同步输出数据格式

{
    "database": "fooDB",
    "table": "barTable",
    "type": "bootstrap-start",
    "ts": 1450557744,
    "data": {}
}
{
    "database": "fooDB",
    "table": "barTable",
    "type": "bootstrap-insert",
    "ts": 1450557744,
    "data": {
        "txt": "hello"
    }
}
{
    "database": "fooDB",
    "table": "barTable",
    "type": "bootstrap-insert",
    "ts": 1450557744,
    "data": {
        "txt": "bootstrap!"
    }
}
{
    "database": "fooDB",
    "table": "barTable",
    "type": "bootstrap-complete",
    "ts": 1450557744,
    "data": {}
}

第一条type为bootstrap-start和最后一条type为bootstrap-complete的数据,是bootstrap开始和结束的标志,不包含数据,中间的type为bootstrap-insert的数据才包含数据。

一次bootstrap输出的所有记录的ts都相同,为bootstrap开始的时间。

相关推荐
爬山算法21 分钟前
Oracle(148)如何进行数据库降级?
数据库·oracle
世俗ˊ22 分钟前
MySQL—触发器详解
数据库·mysql
老年DBA23 分钟前
oracle direct path read处理过程
数据库·oracle
YashanDB24 分钟前
【YashanDB知识库】如何配置jdbc驱动使getDatabaseProductName()返回Oracle
数据库·oracle·yashandb·崖山数据库·yashandb知识库
YashanDB24 分钟前
【YashanDB知识库】YMP迁移oracle不兼容给用户授权高级包
数据库·oracle·yashandb·崖山数据库·yashandb知识库
yunzhonghefei25 分钟前
[Oracle] ORA-04036: 实例使用的 PGA 内存超出 PGA_AGGREGATE_LIMIT
数据库·oracle
Week_by_week27 分钟前
11-pg内核之锁管理器(六)死锁检测
数据库
偏执网友28 分钟前
sqlserver迁移数据库文件存储位置
数据库·sqlserver·database
Week_by_week30 分钟前
9-pg内核之锁管理器(四)常规锁
数据库·postgresql
胡耀超2 小时前
1.分页查询(后端)—— Vue3 + SpringCloud 5 + MyBatisPlus + MySQL 项目系列(基于 Zulu 11)
mysql·spring·spring cloud·mybatis·mybatisplus·zulu11