目錄
- Maxwell簡(jiǎn)介
- Maxwell的配置與使用
- 1.下載Maxwell安裝包
- 2.配置mysql,打開(kāi)mysql binlog日志
- 3.啟動(dòng)Maxwell,主要介紹數(shù)據(jù)寫(xiě)入rabbitmq的實(shí)戰(zhàn):
- rabbitmq的操作
- 全量同步
Maxwell簡(jiǎn)介
maxwell是由java編寫(xiě)的守護(hù)進(jìn)程,可以實(shí)時(shí)讀取mysql binlog并將行更新以JSON格式寫(xiě)入kafka、rabbitMq、redis等中, 這樣有了mysql增量數(shù)據(jù)流,使用場(chǎng)景就很多了,比如:實(shí)時(shí)同步數(shù)據(jù)到緩存,同步數(shù)據(jù)到ElasticSearch,數(shù)據(jù)遷移等等。
maxwell官網(wǎng):http://maxwells-daemon.io
maxwell源代碼:https://github.com/zendesk/maxwell
Maxwell的配置與使用
maxwell 依賴(lài)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,打開(kāi)mysql binlog日志
[root@xxx mysql]# vi /usr/local/mysql/my.cnf
[mysqld]
log-bin=mysql-bin #添加這一行就
binlog-format=ROW #選擇row模式
server_id=1 #隨機(jī)指定一個(gè)不能和其他集群中機(jī)器重名的字符串,如果只有一臺(tái)機(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選項(xiàng)指定的數(shù)據(jù)庫(kù)中存儲(chǔ)狀態(tài)的權(quán)限(默認(rèn)庫(kù)名稱(chēng)為maxwell),所以需要提前給權(quán)限:
#創(chuàng)建一個(gè)有同步數(shù)據(jù)的用戶(hù)yhrepl
mysql> create user 'yhrepl'@'*' identified by 'scgaopan';
Query OK, 0 rows affected (0.10 sec)
#此用戶(hù)yhrepl要有對(duì)需要同步的數(shù)據(jù)庫(kù)表有操作權(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選項(xiàng)指定的數(shù)據(jù)庫(kù)中存儲(chǔ)狀態(tài)的權(quán)限(默認(rèn)庫(kù)名稱(chēng)為maxwell)
mysql> grant all privileges on maxwell.* to 'yhrepl'@'%' identified by 'scgaopan';
Query OK, 0 rows affected (0.09 sec)
3.啟動(dòng)Maxwell,主要介紹數(shù)據(jù)寫(xiě)入rabbitmq的實(shí)戰(zhàn):
[root@xxx maxwell-1.19.5]# vi /usr/local/maxwell/maxwell-1.19.5/config.properties
#日志級(jí)別
log_level=DEBUG
producer=rabbitmq
daemon=true
#監(jiān)控的數(shù)據(jù)庫(kù), mysql用戶(hù)必須擁有讀取binlog權(quán)限和新建庫(kù)表的權(quán)限
host=47.105.110.xxx
user=yhrepl
password=scgaopan
output_nulls=true
jdbc_options=autoReconnet=true
#監(jiān)控?cái)?shù)據(jù)庫(kù)中的哪些表
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
啟動(dòng)Maxwell:
[root@xxx maxwell-1.19.5]# ./bin/maxwell
#可以后臺(tái)啟動(dòng)
[root@xxx maxwell-1.19.5]# nohub ./bin/maxwell
啟動(dòng)成功,此時(shí)會(huì)自動(dòng)生成maxwell庫(kù),該庫(kù)記錄了maxwell同步的狀態(tài),最后一次同步的id等等信息,在主庫(kù)失敗或同步異常后,只要maxwell庫(kù)存在,下次同步會(huì)根據(jù)最后一次同步的id。如果沒(méi)有生成maxwell庫(kù)或報(bào)錯(cuò),可能config.properties中配置的mysql用戶(hù)權(quán)限不夠
rabbitmq的操作
rabbitmq的操作,啟動(dòng)maxwell后就有一個(gè)maxwell的exchage生成

但對(duì)應(yīng)的queue和exchange和queue的綁定需要用戶(hù)自己去實(shí)現(xiàn)
新建一個(gè)maxwell-test的queue:

把queue與exchange進(jìn)行綁定:

注意,這里的Routing key 是區(qū)分大小寫(xiě)的
在數(shù)據(jù)庫(kù)中修改一條記錄,可以看到maxwell-test隊(duì)列里面有一第記錄了。

全量同步
使用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í)行同步
上一個(gè)命令先開(kāi)著,然后再啟動(dòng)client_id=maxwell_dev的maxwell
./bin/maxwell --client_id maxwell_dev
等待執(zhí)行完成即可
以上就是如何使用Maxwell實(shí)時(shí)同步mysql數(shù)據(jù)的詳細(xì)內(nèi)容,更多關(guān)于用Maxwell同步mysql數(shù)據(jù)的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
您可能感興趣的文章:- python實(shí)現(xiàn)MySQL指定表增量同步數(shù)據(jù)到clickhouse的腳本
- MySQL數(shù)據(jù)庫(kù)主從同步實(shí)戰(zhàn)過(guò)程詳解
- scrapy數(shù)據(jù)存儲(chǔ)在mysql數(shù)據(jù)庫(kù)的兩種方式(同步和異步)
- Mysql主從數(shù)據(jù)庫(kù)(Master/Slave)同步配置與常見(jiàn)錯(cuò)誤
- ktl工具實(shí)現(xiàn)mysql向mysql同步數(shù)據(jù)方法
- 用python簡(jiǎn)單實(shí)現(xiàn)mysql數(shù)據(jù)同步到ElasticSearch的教程
- MySQL數(shù)據(jù)庫(kù)的主從同步配置與讀寫(xiě)分離
- node.js將MongoDB數(shù)據(jù)同步到MySQL的步驟
- Linux下MySQL數(shù)據(jù)庫(kù)的主從同步復(fù)制配置
- PHP使用SWOOLE擴(kuò)展實(shí)現(xiàn)定時(shí)同步 MySQL 數(shù)據(jù)