canal 的工作原理
很简单,就是把自己伪装成 slave,假装从 master 复制数据
开启mysql的binlog
sudo vi /etc/mysql/my.cnf
[mysqld]
server-id = 1
log-bin=mysql-bin
binlog_format=row
binlog-do-db=gmall
参数说明
配置好以后重启mysql
sudo systemctl restart mysqld
mysql> set global validate_password_length=4;
mysql> set global validate_password_policy=0;
mysql> GRANT SELECT, REPLICATION SLAVE, REPLICATION CLIENT ON *.* TO
'canal'@'%' IDENTIFIED BY 'canal' ;
https://github.com/alibaba/canal/releases
tar -zxvf canal.deployer-1.1.4.tar.gz
修改配置文件canal.properties
vi canal.properties
canal.serverMode = kafka
canal.mq.servers = master:9092,node1:9092,node2:9092
canal.destinations = example
vi instance.properties
canal.instance.dbUsername=root
canal.instance.dbPassword=root
# mq config
canal.mq.topic=gmall_db
# dynamic topic route by schema or table regex
#canal.mq.dynamicTopic=mytest1.user,mytest2\\..*,.*\\..*
#canal.mq.partition=0
# hash partition config,下面的分区数和自己主题的partition数目相同
canal.mq.partitionsNum=4
bin/startup.sh
测试
bin/kafka-console-consumer.sh --bootstrap-server master:9092 --topic gmall_db
然后往mysql里面监听的数据库插入一条数据看kafka是否有数据接收到
bin/stop.sh
{
"data": [
{
"id": "1547090631092903951",
"user_id": "1413",
"nick_name": null,
"head_img": null,
"sku_id": "16",
"spu_id": "4",
"order_id": "5991",
"appraise": "1201",
"comment_txt": "评论内容:51811533721518964616668687831874922222995433369391",
"create_time": "2022-06-28 13:28:44",
"operate_time": null
}
],
"database": "gmall",
"es": 1657690124000,
"id": 211,
"isDdl": false,
"mysqlType": {
"id": "bigint(20)",
"user_id": "bigint(20)",
"nick_name": "varchar(20)",
"head_img": "varchar(200)",
"sku_id": "bigint(20)",
"spu_id": "bigint(20)",
"order_id": "bigint(20)",
"appraise": "varchar(10)",
"comment_txt": "varchar(2000)",
"create_time": "datetime",
"operate_time": "datetime"
},
"old": null,
"pkNames": [
"id"
],
"sql": "",
"sqlType": {
"id": -5,
"user_id": -5,
"nick_name": 12,
"head_img": 12,
"sku_id": -5,
"spu_id": -5,
"order_id": -5,
"appraise": 12,
"comment_txt": 12,
"create_time": 93,
"operate_time": 93
},
"table": "comment_info",
"ts": 1657690130536,
"type": "INSERT"
}
{
"data": [
{
"dic_code": "1102",
"dic_name": "微信",
"parent_code": "11",
"create_time": null,
"operate_time": null
}
],
"database": "gmall",
"es": 1657690329000,
"id": 212,
"isDdl": false,
"mysqlType": {
"dic_code": "varchar(10)",
"dic_name": "varchar(100)",
"parent_code": "varchar(10)",
"create_time": "datetime",
"operate_time": "datetime"
},
"old": null,
"pkNames": null,
"sql": "",
"sqlType": {
"dic_code": 12,
"dic_name": 12,
"parent_code": 12,
"create_time": 93,
"operate_time": 93
},
"table": "base_dic",
"ts": 1657690329374,
"type": "DELETE"
}
{
"data": [
{
"id": "1",
"login_name": "g4hpd2mp2",
"nick_name": "修改",
"passwd": null,
"name": "沈昌成",
"phone_num": "13973123428",
"email": "[email protected]",
"head_img": null,
"user_level": "1",
"birthday": "2004-08-10",
"gender": "M",
"create_time": "2020-06-10 21:48:29",
"operate_time": "2022-06-28 13:28:34",
"status": null
}
],
"database": "gmall",
"es": 1657690400000,
"id": 213,
"isDdl": false,
"mysqlType": {
"id": "bigint(20)",
"login_name": "varchar(200)",
"nick_name": "varchar(200)",
"passwd": "varchar(200)",
"name": "varchar(200)",
"phone_num": "varchar(200)",
"email": "varchar(200)",
"head_img": "varchar(200)",
"user_level": "varchar(200)",
"birthday": "date",
"gender": "varchar(1)",
"create_time": "datetime",
"operate_time": "datetime",
"status": "varchar(200)"
},
"old": [
{
"nick_name": "雄琛"
}
],
"pkNames": [
"id"
],
"sql": "",
"sqlType": {
"id": -5,
"login_name": 12,
"nick_name": 12,
"passwd": 12,
"name": 12,
"phone_num": 12,
"email": 12,
"head_img": 12,
"user_level": 12,
"birthday": 91,
"gender": 12,
"create_time": 93,
"operate_time": 93,
"status": 12
},
"table": "user_info",
"ts": 1657690400556,
"type": "UPDATE"
}