因为业务需要,对mysql数据变更进行实时抓取。maxwell可以对mysql数据库的二进制日志bin-log进行提取,并生成 JSON 格式的消息,作为生产者将它发送到kafka。
mysql数据库有自己的bin-log,mysql通过bin-log实现mysql数据库的主从备份
此处maxwell将自己伪装为slave(从节点),向MySQL主节点发送dump协议获取bin-log,然后maxwell解析日志文件。
1.预先安装kafka,因为maxwell需要kafka的消费者来消费信息。
2.mysql配置。
vim /etc/my.cnf
添加如下几项,然后重启 service mysql restart (sql查看修改是否成功 show variables like '%binlog_format%'; )
(注:docker部署的mysql,查看挂载配置与否docker inspect containername |grep Source,如果挂载了 就修改磁盘上的配置然后重启container。如果没挂载,则参考https://www.cnblogs.com/tian874540961/p/12133469.html)
#mysql的id指定
server_id=1
#日志命名格式
log_bin = mysql-bin
#日志的记录模式,应设置为row
binlog_format = row
授权涉及maxwell进程访问业务库数据和maxwell数据,maxwell库用来存放maxwell抓取数据的数据库,表,位置等信息。
CREATE USER 'maxwell'@'%' IDENTIFIED BY '123456';
create database maxwell DEFAULT CHARSET utf8 COLLATE utf8_general_ci;
GRANT ALL ON *.* TO 'maxwell'@'%';
GRANT ALL ON *.* TO 'maxwell'@'localhost';
GRANT ALL ON *.* TO 'maxwell'@'localhost.localdomain';
GRANT SELECT, REPLICATION CLIENT, REPLICATION SLAVE on *.* to 'maxwell'@'%';
GRANT SELECT, REPLICATION CLIENT, REPLICATION SLAVE on *.* to 'maxwell'@'localhost';
GRANT SELECT, REPLICATION CLIENT, REPLICATION SLAVE on *.* to 'maxwell'@'localhost.localdomain';
GRANT USAGE ON *.* TO 'maxwell'@'localhost.localdomain' IDENTIFIED BY '123456' WITH GRANT OPTION;
grant all privileges on *.* to [email protected] identified by '123456';
flush privileges;
3.maxwell配置
解压maxwell-1.22.1.tar.gz
tar zxf maxwell-1.22.1.tar.gz -C /usr/local/
修改maxwell配置文件config.properties.example
# tl;dr config
daemon=true
log_level=info
producer=kafka
#kafka的ip以及端口配置
kafka.bootstrap.servers=10.192.38.146:9092
#kafka的主题配置
kafka_topic=maxwell
# mysql login info
host=10.17.87.176
user=maxwell
password=123456
port=3306
schema_database=maxwell
#maxwell启动端口
http_port=9002
http_diagnostic=true
http_path_prefix=/
metrics_type=jmx|http|datadog
maxwell运行需要先启动kafka
1.启动zookeeper
.\zookeeper-server-start.bat ..\..\config\zookeeper.properties
2.启动kafka
.\kafka-server-start.bat ..\..\config\server.properties
3.创建maxwell主题
kafka-topics.bat --create --zookeeper 10.192.38.146:2181 --replication-factor 1 --partitions 3 --topic maxwell Created topic "maxwell"
4.创建消费者
kafka-console-consumer.bat --zookeeper 10.192.38.146:2181 --topic maxwell
启动maxwell
指定binlog位置进行启动,首次启动采用
/usr/local/maxwell/bin/maxwell --conf /usr/local/maxwell/config.properties.example --init_position=mysql-bin.000007:1:0 &
第二次以及之后启动方式
/usr/local/maxwell/bin/maxwell --conf /usr/local/maxwell/config.properties.example &
如果启动报错,解决方法是进入mysql的Maxwell库,然后:清空columns,tables,schemas,databases表后再启动
删除一条记录,kafka消费者抓取到信息,成功