经过maxwell读取binlog日志,把mysql变化数据传入redis

Maxwell简介

Maxwell是一个能实时读取MySQL二进制日志binlog,并生成 JSON 格式的消息,做为生产者发送给 Kafka,Kinesis、RabbitMQ、Redis、Google Cloud Pub/Sub、文件或其它平台的应用程序。它的常见应用场景有ETL、维护缓存、收集表级别的dml指标、增量到搜索引擎、数据分区迁移、切库binlog回滚方案等。Maxwell给出了一些无需从新构建整个平台的事件来源的好处。你们能够经过官网下载合适的版本进行使用。
Maxwell主要提供了下列功能:mysql

  • 支持 SELECT * FROM table 的方式进行全量数据初始化
  • 支持在主库发生failover后,自动恢复binlog位置(GTID)。
  • 能够对数据进行分区,解决数据倾斜问题,发送到kafka的数据支持database、table、column等级别的数据分区。
  • 工做方式是假装为Slave,接收binlog events,而后根据schemas信息拼装,能够接受ddl、xid、row等各类event。

MaxWell安装

Maxwell安装相对比较简单,本次主要是修改maxwell的配置文件。
1.上传maxwell并解压到指定目录
使用linux链接服务器工具,把maxwell.1.24.1.tar.gz上传到/soft目录下。
[root@localhost bin]# tar -xvf maxwell.1.24.1.tar.gz -C /opt/maxwelllinux

在把maxwell安装完成后,再在mysql数据库中配置maxwell用户和库。git

Mysql服务配置(my.conf)

在/etc/my.conf添加如下内容,在安装mysql时,已在my.cnf文件中添加了相应内容。github

[mysqld]
server_id=23
log-bin=bin-log
binlog_format=row

解释:
MySQL必须开启了binlogs,即log-bin指定了目录
binlog_format必须是row
server_id指定mysql的全局惟一idredis

在修改mysql conf后,须要重启mysql服务sql

[root@localhost ~]#systemctl stop mysql
[root@localhost ~]#systemctl start mysql

或者
[root@localhost ~]#service mysqld restart数据库

Maxwell用户权限配置

Maxwell须要储存它本身的一些状态数据,启动参数schema_database选型来指定,默认是maxwell。
MySQL 用户及权限配置(SQL)
建立maxwell用户,并设置密码为’123456’。
mysql> CREATE USER 'maxwell'@'%' IDENTIFIED BY '123456';json

建立maxwell数据库,存储maxwell工具的一些状态数据。
mysql> CREATE DATABASE IF NOT EXISTS maxwell default charset utf8 COLLATE utf8_general_ci;vim

对maxwell用户进行受权。缓存

mysql> GRANT ALL on *.* to 'maxwell'@'%' identified by 'XXXXXX';
mysql> GRANT SELECT, REPLICATION CLIENT, REPLICATION SLAVE on *.* to 'maxwell'@'%';

Maxwell的配置文件

  1. 在/opt/maxwell目录下建立一个config.properties文件,写入指定配置:
    [root@localhost maxwell]#vim config.properties
#[mysql]
user=maxwell    #链接mysql用户名
password=123456  #链接mysql的密码
host=192.168.1.22   # mysql的主机名(IP地址)
port=3306   #mysql端口
#[producer]
producer=redis
redis_host=127.0.0.1  
#redis服务器ip地址
redis_port=6379         
#redis的端口,默认是6379
redis_database =0       
#redis中数据库,默认为0
rredis_key=maxwell
redis_stream_json_key=message 
redis_type=pubsub

主要参数解释:
#[mysql]下的参数主要是链接mysql配置信息,填写上述建立的maxwell用户。
#[producer]下的参数是生产者相关信息
producer :生产者类型,但是kafka、redis等,本次使用redis
redis_XX : redis相关配置信息,其中redis_host本文填写的为127.0.0.1,是由于在redis配置文件中,bind配置的为127.0.0.1,若是在config.properties填写实际ip地址,maxwell会没法访问。
redis_type :选择redis的数据生成模式,目前支持[ pubsub | xadd | lpush | rpush ],默认值为pubsub。

经过如下命令启动maxwell
[root@localhost ~]#/opt/maxwell/bin/maxwell --config config.properties --filter=exclude:*.*,include:lipp.* --daemon

参数说明:
config:指定配置文件,通常在参数较多时,须要把相关配置写到配置文件中。
filter:过滤设置,能够经过exclude和include关键字设置排除和包含哪些数据。
daemon:指定后台运行

测试发布/订阅模式

经过上一步配置,设置的redis_type=pubsub,此模式表示建立一个发布主题,当mysql数据库发生变化时,变化的内容将被maxwell转入到redis中发布到maxwell频道,当全部订阅了此频道的订阅者都将会收到相应变化消息。

经过以上步骤,启动mysql、redis和maxwell后,开始进行简单测试
1.在client1 经过redis_cli登陆到redis并订阅maxwell

[root@client1 ~]# redis-cli 
127.0.0.1:6379> SUBSCRIBE maxwell
Reading messages... (press Ctrl-C to quit)
1) "subscribe"
2) "maxwell"
3) (integer) 1

2.在client2中登陆mysql并向lipp数据库中表增长数据。

[root@client2 ~]# mysql -uroot -p111111
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 300
Server version: 5.7.23-log MySQL Community Server (GPL)

Copyright (c) 2000, 2018, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> use lipp;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Database changed
mysql> insert into t1 values(1,'test101');

3.在client1能够看到maxwell channel的输出

1) "message"
2) "maxwell"
3) "{\"database\":\"lipp\",\"table\":\"t1\",\"type\":\"insert\",\"ts\":1584279262,\"xid\":7726,\"commit\":true,\"data\":{\"id\":1,\"name\":\"test101\"}}"

测试list模式

1.修改maxwell目录下的conf.preporties文件中redis_type=lpush,后重启maxwell服务。

maxwell                          RUNNING   pid 11366, uptime 0:00:08

2.在client1 经过redis_cli登陆redis中,并查看当前库中有多少key

[root@client1 ~]# redis-cli 
127.0.0.1:6379> DBSIZE
(integer) 0

目前0号库中没有key。

3.在client2 登陆mysql数据库中,并在指定表插入数据。

mysql> insert into t1 values(1,'wangwu');
Query OK, 1 row affected (0.00 sec)

4.在client1中经过dbsize再次查看数据库大小

127.0.0.1:6379> DBSIZE
(integer) 1

经过keys 查看key名称,并经过type查看key的类型。

127.0.0.1:6379> keys *
1) "maxwell"
127.0.0.1:6379> type maxwell
list

当key的类型问list时,可使用list相关命令进行对key操做。
经过llen查看key的长度

127.0.0.1:6379> llen maxwell
(integer) 1

经过lrange命令查看key的内容

127.0.0.1:6379> LRANGE maxwell 0 10
1)"{\"database\":\"lipp\",\"table\":\"t1\",\"type\":\"insert\",\"ts\":1584286244,\"xid\":15381,\"commit\":true,\"data\":{\"id\":1,\"name\":\"wangwu\"}}"

经过lpop或rpop 弹出key中的值

127.0.0.1:6379> LPOP maxwell
"{\"database\":\"lipp\",\"table\":\"t1\",\"type\":\"insert\",\"ts\":1584286244,\"xid\":15381,\"commit\":true,\"data\":{\"id\":1,\"name\":\"wangwu\"}}"

经过修改redis_type的参数为pubsub和lpush,能够实现监控mysql数据库变化,经过发布订阅模式,实现数据同步功能。经过list方式能够获取最新的数据的变化和数据变化数量等需求。

本文只是演示了maxwell读取binlog到redis,其实maxwell能够实现多种producer方式,如kafka,pubsub、redis、自定义等。具体能够经过官网了解,也能够到github了解。

相关文章
相关标签/搜索