1 安装配置
1.1 下载
https://github.com/alibaba/canal/releases/download/canal-1.1.6/canal.deployer-1.1.6.tar.gz
1.2 mysql配置binlog
# on 时,代表着开启
show VARIABLES like 'log_bin';
1.3 创建MySQL canal用户
# 创建用户
create user 'canal'@'%' identified by '123';
# 授权
grant show view,select,replication slave, replication client on *.* to 'canal'@'%';
# 刷新权限
flush privileges;
1.4 canal的核心配置文件
conf/example/instance.properties
# 配置 slaveId 自定义,不等于 mysql 的 server Id 即可
canal.instance.mysql.slaveId=10
# 数据库地址
canal.instance.master.address=127.0.0.1:3306
# binlog日志名称
canal.instance.master.journal.name=mysql-bin.000001
# binlog偏移量
canal.instance.master.position=913
# 数据库账号密码
canal.instance.dbUsername=canal
canal.instance.dbPassword=123
# table regex
canal.instance.filter.regex=test_xxx\\.user
mysql日志查询知识补充
# 查看最新一个binlog日志文件名称和Position
show master status
conf/canal.properties
canal.admin.port = 11110
canal.port = 11111 # client 连接的端口
canal.metrics.pull.port = 11112
# 指定实例,多个实例使用逗号分隔: canal.destinations = example1,example2
canal.destinations = example
1.5 启动canal
双击 bin/startup.bat
cd bin
./startup.bat
2 安装配置
2.1 php案列
composer require xingwenge/canal_php
<?php
use xingwenge\canal_php\CanalClient;
use xingwenge\canal_php\CanalConnectorFactory;
use xingwenge\canal_php\Fmt;
include __DIR__ . '/../../../vendor/autoload.php';
$client = CanalConnectorFactory::createClient(CanalClient::TYPE_SOCKET_CLUE);
echo "建立连接" . PHP_EOL;
$client->connect("127.0.0.1", 11111);
echo "检查" . PHP_EOL;
$client->checkValid();
echo "开始订阅" . PHP_EOL;
$client->subscribe(1001, "example", "test_xshalong\.zeai_tg_user_update_info");
while (true) {
$message = $client->get(100);
if ($entries = $message->getEntries()) {
foreach ($entries as $entry) {
Fmt::println($entry);
}
}
sleep(1);
}
输出结果
# 常见问题
1、 Could not find first log file name in binary log index file
删除 conf/example/meta.dat的文件,重启canal即可