mysql开启binlog
1 开启binlog
sudo vi /etc/my.cnf
2 mysql的binlog格式有3种,为了把binlog解析成json数据格式,要设置binlog的格式为row(binlog有三种格式:Statement、Row以及Mixed)
server-id=1
log-bin=master(这一步开启binlog)
binlog_format=row
3重启msyql服务
sudo service mysqld restart
4查看是否已经开启binlog
Mysql>show variables like ‘%log_bin%’;
此时,可以在目录/var/lib/mysql下看到生成了相应的binlog监听日志文件,如图,master.000001文件,每次重启msyql服务,就会生成一个新的监听文件
Kafka的配置启动:
1首先启动zookeeper
$sbin/zkServer.sh start
2开启kafka命令行
bin/kafka-server-start.shconfig/server.properties
3创建一个主题叫maxwell以便于接受数据
bin/kafka-topics.sh–create –zookeeper mw:2181 –replication-factor 1 –partitions 1 –topic maxwell
4启动生产者窗口
bin/kafka-console-producer.sh–broker-list mw:9092 –topic maxwell
5启动消费者窗口
bin/kafka-console-consumer.sh–zookeeper mw:2181 –topic maxwell –from-beginning(老版本的启动命令,运行可能会报错)
下面是新版本的命令,亲测可行:
bin/kafka-console-consumer.sh --bootstrap-server localhost:9092 --topic test --from-beginning
Maxwell配置安装:
1下载Maxwell
官网
http://maxwells-daemon.io/
2 安装Maxwell
tar -zxvf maxwell-1.10.6.tar.gz
3给mysql授权
mysql> GRANT ALL on maxwell.* to’maxwell’@’%’ identified by ‘XXXXXX’;
mysql> GRANT SELECT, REPLICATION CLIENT,REPLICATION SLAVE on *.* to ‘maxwell’@’%’;
以上图片为官网参考
以我自己的为例:
GRANTALL on *.* to ‘user01’@’%’ identified by ‘123456’;
把所有数据库的所有表授权给user01用户以密码123456登录
GRANTSELECT, REPLICATION CLIENT, REPLICATION SLAVE on *.* to ‘user01’@’%’;
flushprivileges;
4开启maxwell命令行(注意,如果没有设置,maxwell默认是把监听的mysql的binlog日志发送到kafka的主题叫maxwell的topic上的)
bin/maxwell --user='maxwell' --password='XXXXXX' --host='127.0.0.1' \
- 1
--producer=kafka --kafka.bootstrap.servers=localhost:9092
- 1
以上为官网参考,我自己的具体的demo 这里我直接用的是root账号,没有使用上面新增的那个账号
bin/maxwell --user=’root’ --password=’123456’ --host=’192.168.100.106’ --producer=kafka --kafka.bootstrap.servers=localhost:9092解释:host参数是安装mysql的那台主机,最后的kafka.bootstrap.servers是安装kafka集群的节点主机名和端口号
测试:
1、此时,mysql的binlog已经开启,maxwell命令行也开启,kafka的生产者消费者窗口也开启在监听
开始往mysql里插入修改数据,数据提交的时候可以看到Kafka的消费者窗口有监听到json数据
此处测试出个问题:增删改都没问题,但是我对表结构的修改并没有输出。待进一步探索。
此问题已解决,需要修改配置文件,output_ddl是否在输出的json流中,包含ddl语句。默认 false
详见:https://blog.csdn.net/xiaolegeaizy/article/details/100743918
2、在Kafka生产者窗口输入消息,消费者窗口也可以看到数据
生产者窗口:
消费者窗口:
更详细的Kafka安装使用见下面链接:
https://blog.csdn.net/xiaolegeaizy/article/details/100736637