MySQL与Redis数据同步

 

下载canal-1.1.4

wget https://github.com/alibaba/canal/releases/download/canal-1.1.4/canal.deployer-1.1.4.tar.gz
tar zvfx canal.deployer-1.1.4.tar.gz

 

 配置MySQL

[mysqld]

log-bin=mysql-bin # 开启Binlog

binlog-format=ROW # 设置Binlog格式为ROW

server_id=1 # 配置一个ServerID

 

给Canal开专门MySQL用户并授权

CREATE USER canal IDENTIFIED BY 'canal';
GRANT SELECT, REPLICATION SLAVE, REPLICATION CLIENT ON *.* TO 'canal'@'%';
FLUSH PRIVILEGES;

show master status;

 

vim canal/conf/example/instance.properties


canal.instance.gtidon=false


# position info
canal.instance.master.address=127.0.0.1:3306
canal.instance.master.journal.name=binlog.000009
canal.instance.master.position=155
canal.instance.master.timestamp=
canal.instance.master.gtid=


# username/password
canal.instance.dbUsername=canal
canal.instance.dbPassword=canal
canal.instance.connectionCharset = UTF-8
canal.instance.defaultDatabaseName=test
# table regex
canal.instance.filter.regex=.*\\..


canal/bin/startup.sh

查看日志canal/logs/example/example.log

while (true) {     Message message = connector.getWithoutAck(batchSize); // 获取指定数量的数据     long batchId = message.getId();     try {         int size = message.getEntries().size();         if (batchId == -1 || size == 0) {             Thread.sleep(1000);         } else {             processEntries(message.getEntries(), jedis);         }           connector.ack(batchId); // 提交确认     } catch (Throwable t) {         connector.rollback(batchId); // 处理失败, 回滚数据     } } 

 

for (CanalEntry.RowData rowData : rowChage.getRowDatasList()) {     if (eventType == CanalEntry.EventType.DELETE) { // 删除         jedis.del(row2Key(user_id, rowData.getBeforeColumnsList()));     } else if (eventType == CanalEntry.EventType.INSERT) { // 插入         jedis.set(row2Key(user_id, rowData.getAfterColumnsList()), row2Value(rowData.getAfterColumnsList()));     } else { // 更新         jedis.set(row2Key(user_id, rowData.getAfterColumnsList()), row2Value(rowData.getAfterColumnsList()));     } } 

insert into account_balance values (888, 100, NOW(), 999);

get 888