1.Maxwell简介
Maxwell 是由美国 Zendesk 开源,用 Java 编写的 MySQL 实时抓取软件。 实时读取MySQL 二进制日志 Binlog,并生成 JSON 格式的消息,作为生产者发送给 Kafka,Kinesis、RabbitMQ、Redis、Google Cloud Pub/Sub、文件或其它平台的应用程序。
官网地址
2.Maxwell 工作原理
Maxwell 的工作原理很简单,就是把自己伪装成 MySQL 的一个 slave,然后以 slave的身份假装从MySQL(master)复制数据。
2.1Mysql主从复制过程
- Master 主库将改变记录,写到二进制日志(binary log)中
- Slave 从库向 mysql master 发送 dump 协议,将 master 主库的 binary log events 拷贝到它的中继日志(relay log);
- Slave 从库向 mysql master 发送 dump 协议,将 master 主库的 binary log events 拷贝到它的中继日志(relay log);
工作原理图
2.2 Maxwell使用前准备
2.2.1 开启Mysql的binlog
- 什么是 binlog
MySQL 的二进制日志可以说 MySQL 最重要的日志了,它记录了所有的 DDL 和 DML(除了数据查询语句)语句,以事件形式记录,还包含语句所执行的消耗的时间,MySQL 的二进制日志是事务安全型的。
一般来说开启二进制日志大概会有 1%的性能损耗。二进制有两个最重要的使用场景:
- 其一:MySQL Replication 在 Master 端开启 binlog,Master 把它的二进制日志传递
给 slaves 来达到 master-slave 数据一致的目的。 - 其二:自然就是数据恢复了,通过使用 mysqlbinlog 工具来使恢复数据。
二进制日志包括两类文件:二进制日志索引文件(文件名后缀为.index)用于记录所有的二进制文件,二进制日志文件(文件名后缀为.00000*)记录数据库所有的 DDL 和 DML(除了数据查询语句)语句事件。
- binlog 的开启
- 找到 MySQL 配置文件的位置
Linux: /etc/my.cnf 如果/etc 目录下没有,可以通过 locate my.cnf 查找位置
Windows: Mysql安装目录下找\my.ini- 在 mysql 的配置文件下,修改配置
在[mysqld] 区块,设置/添加[mysqld] #设置服务id server_id=1 #配置binlog文件名前缀 log-bin=mysql-bin #binlog记录格式 有STATEMENT,MIXED,ROW。 binlog_format=row
- 重启Mysql服务
3 Maxwell使用
特别注意: maxwell-1.3x后不支持jdk8.0,本次使用版本为 1.3以下最新 maxwell-1.29.2
下载地址 提取码:yyds
3.1Maxwell 安装部署
安装部署前需要安装Mysql并开启binlog,这里不在赘述
- 上传 maxwell-1.29.2.tar.gz 到/opt/software 下
- 解压maxwell-1.29.2.tar.gz
tar -zxvf maxwell-1.29.2.tar.gz
3.1 Maxwell元数据初始化
- 在 MySQL 中建立一个 maxwell 库用于存储 Maxwell 的元数据
create database maxwell character set utf8mb4;
- 分配一个账号可以操作该数据库
-- 创建一个maxwell账号使其只有maxwell库的权限并设置密码为123456
GRANT ALL ON maxwell.* TO 'maxwell'@'%' IDENTIFIED BY '123456';
-- 分配maxwell账号其他数据库的监控权限
GRANT SELECT ,REPLICATION SLAVE , REPLICATION CLIENT ON *.* TO maxwell@'%';
-- 刷新权限
flush privileges;
3.2 Maxwell使用
Maxwell的启动方式有两种
- 使用命令行参数启动Maxwell进程
- 使用配置文件启动Maxwell进程
1.命令行简单启动
#进入到maxwell根目录下
cd maxwell-1.29.2
bin/maxwell --user='maxwell' --password='123456' --port=3306 --host='127.0.0.1' --producer=stdout
命令解说
bin/maxwel 为启动Maxwell脚本路径
–user 是连接数据库的用户名
–password 连接数据库的密码
–port 连接数据库的端口
–host 连接数据库的地址
–producer 生产者模式(stdout:控制台输出,kafka:kafka集群输出,redis:redis输出,rabbitmq:rabbitmq输出 …等)
- 配置文件启动
cd maxwell-1.29.2
bin/maxwell --config=./config.properties
config.properties配置文件
# tl;dr config
log_level=info
#生产者模式(既输出模式)
producer=stdout
kafka.bootstrap.servers=localhost:9092
# mysql login info
#mysql连接主机
host=127.0.0.1
#mysql用户名
user=maxwell
#mysql密码
password=123456
#mysql端口
port=3306
# *** general ***
# choose where to produce data to. stdout|file|kafka|kinesis|pubsub|sqs|rabbitmq|redis
#producer=kafka
# set the log level. note that you can configure things further in log4j2.xml
#log_level=DEBUG # [DEBUG, INFO, WARN, ERROR]
# if set, maxwell will look up the scoped environment variables, strip off the prefix and inject the configs
#env_config_prefix=MAXWELL_
# *** mysql ***
# mysql host to connect to
#host=hostname
# mysql port to connect to
#port=3306
# mysql user to connect as. This user must have REPLICATION SLAVE permissions,
# as well as full access to the `maxwell` (or schema_database) database
#user=maxwell
# mysql password
#password=maxwell
# options to pass into the jdbc connection, given as opt=val&opt2=val2
#jdbc_options=opt1=100&opt2=hello
# name of the mysql database where maxwell keeps its own state
#schema_database=maxwell
# whether to use GTID or not for positioning
#gtid_mode=true
# maxwell will capture an initial "base" schema containing all table and column information,
# and then keep delta-updates on top of that schema. If you have an inordinate amount of DDL changes,
# the table containing delta changes will grow unbounded (and possibly too large) over time. If you
# enable this option Maxwell will periodically compact its tables.
#max_schemas&