目錄
- Maxwell簡介
- Maxwell的配置與使用
- 1.下載Maxwell安裝包
- 2.配置mysql,打開mysql binlog日志
- 3.啟動Maxwell,主要介紹數(shù)據寫入rabbitmq的實戰(zhàn):
- rabbitmq的操作
- 全量同步
Maxwell簡介
maxwell是由java編寫的守護進程,可以實時讀取mysql binlog并將行更新以JSON格式寫入kafka、rabbitMq、redis等中, 這樣有了mysql增量數(shù)據流,使用場景就很多了,比如:實時同步數(shù)據到緩存,同步數(shù)據到ElasticSearch,數(shù)據遷移等等。
maxwell官網: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 #隨機指定一個不能和其他集群中機器重名的字符串,如果只有一臺機器,那就可以隨便指定了
重啟mysql服務,登陸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ù)據庫中存儲狀態(tài)的權限(默認庫名稱為maxwell),所以需要提前給權限:
#創(chuàng)建一個有同步數(shù)據的用戶yhrepl
mysql> create user 'yhrepl'@'*' identified by 'scgaopan';
Query OK, 0 rows affected (0.10 sec)
#此用戶yhrepl要有對需要同步的數(shù)據庫表有操作權限
mysql> grant all privileges on test.* to 'yhrepl'@'%' identified by 'scgaopan';
Query OK, 0 rows affected (0.13 sec)
#給yhrepl有同步數(shù)據的權限
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ù)據庫中存儲狀態(tài)的權限(默認庫名稱為maxwell)
mysql> grant all privileges on maxwell.* to 'yhrepl'@'%' identified by 'scgaopan';
Query OK, 0 rows affected (0.09 sec)
3.啟動Maxwell,主要介紹數(shù)據寫入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ù)據庫, mysql用戶必須擁有讀取binlog權限和新建庫表的權限
host=47.105.110.xxx
user=yhrepl
password=scgaopan
output_nulls=true
jdbc_options=autoReconnet=true
#監(jiān)控數(shù)據庫中的哪些表
filter=exclude: *.*,include: test.AA
#replica_server_id 和 client_id 唯一標示,用于集群部署
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庫存在,下次同步會根據最后一次同步的id。如果沒有生成maxwell庫或報錯,可能config.properties中配置的mysql用戶權限不夠
rabbitmq的操作
rabbitmq的操作,啟動maxwell后就有一個maxwell的exchage生成

但對應的queue和exchange和queue的綁定需要用戶自己去實現(xiàn)
新建一個maxwell-test的queue:

把queue與exchange進行綁定:

注意,這里的Routing key 是區(qū)分大小寫的
在數(shù)據庫中修改一條記錄,可以看到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ù)據,并指定client_id示maxwell_dev的maxwell執(zhí)行同步
上一個命令先開著,然后再啟動client_id=maxwell_dev的maxwell
./bin/maxwell --client_id maxwell_dev
等待執(zhí)行完成即可
以上就是如何使用Maxwell實時同步mysql數(shù)據的詳細內容,更多關于用Maxwell同步mysql數(shù)據的資料請關注腳本之家其它相關文章!
您可能感興趣的文章:- python實現(xiàn)MySQL指定表增量同步數(shù)據到clickhouse的腳本
- MySQL數(shù)據庫主從同步實戰(zhàn)過程詳解
- scrapy數(shù)據存儲在mysql數(shù)據庫的兩種方式(同步和異步)
- Mysql主從數(shù)據庫(Master/Slave)同步配置與常見錯誤
- ktl工具實現(xiàn)mysql向mysql同步數(shù)據方法
- 用python簡單實現(xiàn)mysql數(shù)據同步到ElasticSearch的教程
- MySQL數(shù)據庫的主從同步配置與讀寫分離
- node.js將MongoDB數(shù)據同步到MySQL的步驟
- Linux下MySQL數(shù)據庫的主從同步復制配置
- PHP使用SWOOLE擴展實現(xiàn)定時同步 MySQL 數(shù)據