概覽
這是Maxwell的守護進程,這是一個讀取MySQL binlogs並將行更新寫入Kafka,Kinesis,RabbitMQ,Google Cloud Pub / Sub或Redis(Pub / Sub或LPUSH)作爲JSON的應用程序。 Maxwell具有較低的操作欄,可生成一致,易於攝取的更新流。它允許您輕鬆“固定”流處理系統的一些優點,而無需通過整個代碼庫來添加(不可靠)檢測點。常見用例包括ETL,緩存構建/到期,度量收集,搜索索引和服務間通信。
Download: wget https://github.com/zendesk/maxwell/releases/download/v1.19.0/maxwell-1.19.0.tar.gz
轉換樣例:
mysql> insert into `test`.`maxwell` set id = 1, daemon = 'Stanislaw Lem';
maxwell: {
"database": "test",
"table": "maxwell",
"type": "insert",
"ts": 1449786310,
"xid": 940752,
"commit": true,
"data": { "id":1, "daemon": "Stanislaw Lem" }
}
mysql> update test.maxwell set daemon = 'firebus! firebus!' where id = 1;
maxwell: {
"database": "test",
"table": "maxwell",
"type": "update",
"ts": 1449786341,
"xid": 940786,
"commit": true,
"data": {"id":1, "daemon": "Firebus! Firebus!"},
"old": {"daemon": "Stanislaw Lem"}
}
安裝
從github直接下載後解壓即可。
mysql配置
$ vim /etc/my.cnf # centos
[mysqld]
server_id=1
log-bin=master
binlog_format=row
以上爲官方的寫法,如果上述操作導致mysql無法啓動,請自行百度適合的方式
我的寫法:
server-id=2
binlog-format=row
log-bin = /var/lib/mysql/mysql-bin
# expire_logs_days=10 # 未使用
# max_binlog_size=100M 未使用
注:和官方的寫法區別在於中橫線的位置不同,因官方的寫法可能會導致mysql無法啓動
注意:binlog_format是基於會話的屬性。您需要關閉所有活動連接才能完全轉換爲基於行的複製。
權限:Maxwell需要在schema_database選項(默認maxwell)指定的數據庫中存儲狀態的權限。
mysql> GRANT ALL on maxwell.* to 'maxwell'@'%' identified by 'abcd@1234';
mysql> GRANT SELECT, REPLICATION CLIENT, REPLICATION SLAVE on *.* to 'maxwell'@'%';
mysql> FLUSH PRIVILEGES;
如果提示密碼不合要求,可參考以下鏈接解決。
https://blog.csdn.net/kuluzs/article/details/51924374
maxwell的配置
解壓後的文件夾內有文件 config.properties.example
此爲config的模板,可以將其複製後將後綴example刪除,然後修改其中的內容爲自己需要的內容,之後再啓動maxwell時指定 --config
參數爲此文件即可。我在操作過程中沒有使用此方式,而是將所需參數全部寫在啓動命令中了。
啓動maxwell
注意以下所有命令的啓動需要在解壓後的maxwell目錄內
啓動時的參數至少要有以下幾項:
--user mysql數據庫用戶名
--password mysql數據庫密碼
--host mysql 數據庫服務器
--producer 指定數據輸出到哪裏
# 以上爲必須要的,以下的kafka相關的是以kafka方式啓動時需要的參數,kafka相關分區參數在此未指定
--kafka.bootstrap.servers 指定kafka地址
--kafka_topic 指定kakfa的topic是哪一個
# 以下可選
--daemon 守護進程 後臺運行
--filter 過濾條件,可以過濾數據庫,表,列等等
命令行
bin/maxwell --user='maxwell' --password='XXXXXX' --host='127.0.0.1' --producer=stdout
- 以上命令將binlog解析後的json直接顯示在控制檯中
kafka
本次測試僅僅針對單機版kafka,一個topic和一個分區,關於kafka的內容,需自行學習
官方:
bin/maxwell --user='maxwell' --password='XXXXXX' --host='127.0.0.1' \ --producer=kafka --kafka.bootstrap.servers=localhost:9092 --kafka_topic=maxwell
我的測試:操作未對kafka分區進行區分,僅一個分區,所有數據庫的變更均發送到一個topic的一個分區中
bin/maxwell --user='maxwell' --password='abcd@1234' --host='192.168.19.148' \ --producer=kafka --kafka.bootstrap.servers=192.168.19.148:9092 --kafka_topic=mysql2ck
更多參數:http://maxwells-daemon.io/config/
maxwell對sql語句的解析
- 一條insert語句插入多條數據,會將其拆爲一條條輸出到kafka,一條記錄爲一個json數據
- 類似的還有delete、update,與 insert 一致
使用過程中出現的問題
1、python創建的kafka消費者報錯:
kafka.errors.UnsupportedCodecError: UnsupportedCodecError: Libraries for snappy compression codec not found
解決:
https://blog.csdn.net/chenggong2dm/article/details/70172682
https://github.com/andrix/python-snappy
https://blog.csdn.net/yiifaa/article/details/81876338
windows解決:
https://blog.csdn.net/chinewwen/article/details/81071182
https://www.lfd.uci.edu/~gohlke/pythonlibs/
下載安裝即可
2、啓動maxwell時,循環報錯,與mysql的連接斷開,
Could not find first log file name in binary log index file
類似上述錯誤的內容。
原因:修改了 /etc/my.cnf
文件內的log-bin=master 導致出現兩種二進制文件,之前是mysql-bin的寫法
解決:刪除mysql目錄內的 master.* 文件,更改/etc/my.cnf 爲文章開始處的內容
3、maxwell分區參數的使用
- 測試中只有database方式,可以將不同數據庫的操作,插入到不同的分區中
bin/maxwell --user='maxwell' --password='abcd@1234' --host='192.168.19.148' \
--producer=kafka --kafka.bootstrap.servers=192.168.19.148:9092 --kafka_topic=testCK \
--producer_partition_by=database --kafka_partition_hash=default