通過maxwell讀取binlog日誌,把mysql變化數據傳入redis

Maxwell簡介

Maxwell是一個能實時讀取MySQL二進制日誌binlog,並生成 JSON 格式的消息,作爲生產者發送給 Kafka,Kinesis、RabbitMQ、Redis、Google Cloud Pub/Sub、文件或其它平臺的應用程序。它的常見應用場景有ETL、維護緩存、收集表級別的dml指標、增量到搜索引擎、數據分區遷移、切庫binlog回滾方案等。Maxwell給出了一些無需重新構建整個平臺的事件來源的好處。大家可以通過官網下載合適的版本進行使用。
Maxwell主要提供了下列功能:

  • 支持 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/maxwell

在把maxwell安裝完成後,再在mysql數據庫中配置maxwell用戶和庫。

Mysql服務配置(my.conf)

在/etc/my.conf添加以下內容,在安裝mysql時,已在my.cnf文件中添加了相應內容。

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

解釋:
MySQL必須開啓了binlogs,即log-bin指定了目錄
binlog_format必須是row
server_id指定mysql的全局唯一id

在修改mysql conf後,需要重啓mysql服務

[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';

創建maxwell數據庫,存儲maxwell工具的一些狀態數據。
mysql> CREATE DATABASE IF NOT EXISTS maxwell default charset utf8 COLLATE utf8_general_ci;

對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瞭解。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章