如何使用Maxwell實時同步mysql數(shù)據(jù)
Maxwell簡介
maxwell是由java編寫的守護(hù)進(jìn)程,可以實時讀取mysql binlog并將行更新以JSON格式寫入kafka、rabbitMq、redis等中, 這樣有了mysql增量數(shù)據(jù)流,使用場景就很多了,比如:實時同步數(shù)據(jù)到緩存,同步數(shù)據(jù)到ElasticSearch,數(shù)據(jù)遷移等等。
maxwell官網(wǎng):http://maxwells-daemon.io
maxwell源代碼:https://github.com/zendesk/maxwell
Maxwell的配置與使用
maxwell 依賴java sdk,所以需要先配置JDK環(huán)境。
1.下載Maxwell安裝包
root@xxx maxwell]# pwd /usr/local/maxwell [root@xxx maxwell]# wget https://github.com/zendesk/maxwell/releases/download/v1.19.5/maxwell-1.19.5.tar.gz [root@xxx maxwell]# tar zxvf maxwell-1.19.5.tar.gz [root@xxx maxwell]# cd maxwell-1.19.5
2.配置mysql,打開mysql binlog日志
[root@xxx mysql]# vi /usr/local/mysql/my.cnf [mysqld] log-bin=mysql-bin #添加這一行就 binlog-format=ROW #選擇row模式 server_id=1 #隨機(jī)指定一個不能和其他集群中機(jī)器重名的字符串,如果只有一臺機(jī)器,那就可以隨便指定了
重啟mysql服務(wù),登陸mysql,查看binlog日志模式
mysql> show variables like '%log_bin%' +---------------------------------+-------------------------------+ | Variable_name | Value | +---------------------------------+-------------------------------+ | log_bin | ON| | log_bin_basename | /data/mysqldb/mysql-bin | | log_bin_index | /data/mysqldb/mysql-bin.index | | log_bin_trust_function_creators | OFF | | log_bin_use_v1_row_events | OFF | | sql_log_bin | ON| +---------------------------------+-------------------------------+ 6 rows in set (0.11 sec)
Maxwell需要在schema_database選項指定的數(shù)據(jù)庫中存儲狀態(tài)的權(quán)限(默認(rèn)庫名稱為maxwell),所以需要提前給權(quán)限:
#創(chuàng)建一個有同步數(shù)據(jù)的用戶yhrepl mysql> create user 'yhrepl'@'*' identified by 'scgaopan'; Query OK, 0 rows affected (0.10 sec) #此用戶yhrepl要有對需要同步的數(shù)據(jù)庫表有操作權(quán)限 mysql> grant all privileges on test.* to 'yhrepl'@'%' identified by 'scgaopan'; Query OK, 0 rows affected (0.13 sec) #給yhrepl有同步數(shù)據(jù)的權(quán)限 mysql> grant select,replication client,replication slave on *.* to 'yhrepl'@'%' identified by 'scgaopan'; Query OK, 0 rows affected (0.10 sec) # Maxwell需要在schema_database選項指定的數(shù)據(jù)庫中存儲狀態(tài)的權(quán)限(默認(rèn)庫名稱為maxwell) mysql> grant all privileges on maxwell.* to 'yhrepl'@'%' identified by 'scgaopan'; Query OK, 0 rows affected (0.09 sec)
3.啟動Maxwell,主要介紹數(shù)據(jù)寫入rabbitmq的實戰(zhàn):
[root@xxx maxwell-1.19.5]# vi /usr/local/maxwell/maxwell-1.19.5/config.properties #日志級別 log_level=DEBUG producer=rabbitmq daemon=true #監(jiān)控的數(shù)據(jù)庫, mysql用戶必須擁有讀取binlog權(quán)限和新建庫表的權(quán)限 host=47.105.110.xxx user=yhrepl password=scgaopan output_nulls=true jdbc_options=autoReconnet=true #監(jiān)控數(shù)據(jù)庫中的哪些表 filter=exclude: *.*,include: test.AA #replica_server_id 和 client_id 唯一標(biāo)示,用于集群部署 replica_server_id=64 client_id=test-id #metrics_type=http #metrics_slf4j_interval=60 #http_port=8111 #http_diagnostic=true # default false #rabbitmq rabbitmq_host=47.105.110.xxx rabbitmq_port=5672 rabbitmq_user=guest rabbitmq_pass=guest rabbitmq_virtual_host=/ rabbitmq_exchange=maxwell rabbitmq_exchange_type=topic rabbitmq_exchange_durable=false rabbitmq_exchange_autodelete=false rabbitmq_routing_key_template=%db%.%table% rabbitmq_message_persistent=false rabbitmq_declare_exchange=true
啟動Maxwell:
[root@xxx maxwell-1.19.5]# ./bin/maxwell #可以后臺啟動 [root@xxx maxwell-1.19.5]# nohub ./bin/maxwell &
啟動成功,此時會自動生成maxwell庫,該庫記錄了maxwell同步的狀態(tài),最后一次同步的id等等信息,在主庫失敗或同步異常后,只要maxwell庫存在,下次同步會根據(jù)最后一次同步的id。如果沒有生成maxwell庫或報錯,可能config.properties中配置的mysql用戶權(quán)限不夠
rabbitmq的操作
rabbitmq的操作,啟動maxwell后就有一個maxwell的exchage生成
但對應(yīng)的queue和exchange和queue的綁定需要用戶自己去實現(xiàn)
新建一個maxwell-test的queue:
把queue與exchange進(jìn)行綁定:
注意,這里的Routing key 是區(qū)分大小寫的
在數(shù)據(jù)庫中修改一條記錄,可以看到maxwell-test隊列里面有一第記錄了。
全量同步
使用maxwell-bootstrap命令
./bin/maxwell-bootstrap --database xhd --table xhd-sso --host 127.0.0.1 --user xiehd --password xiehd2018 --client_id maxwell_dev
同步xhd.xhd-sso表的所有數(shù)據(jù),并指定client_id示maxwell_dev的maxwell執(zhí)行同步
上一個命令先開著,然后再啟動client_id=maxwell_dev的maxwell
./bin/maxwell --client_id maxwell_dev
等待執(zhí)行完成即可
以上就是如何使用Maxwell實時同步mysql數(shù)據(jù)的詳細(xì)內(nèi)容,更多關(guān)于用Maxwell同步mysql數(shù)據(jù)的資料請關(guān)注本站其它相關(guān)文章!
版權(quán)聲明:本站文章來源標(biāo)注為YINGSOO的內(nèi)容版權(quán)均為本站所有,歡迎引用、轉(zhuǎn)載,請保持原文完整并注明來源及原文鏈接。禁止復(fù)制或仿造本網(wǎng)站,禁止在非www.sddonglingsh.com所屬的服務(wù)器上建立鏡像,否則將依法追究法律責(zé)任。本站部分內(nèi)容來源于網(wǎng)友推薦、互聯(lián)網(wǎng)收集整理而來,僅供學(xué)習(xí)參考,不代表本站立場,如有內(nèi)容涉嫌侵權(quán),請聯(lián)系alex-e#qq.com處理。