Home > Database > Mysql Tutorial > How to use Canal to achieve real-time data synchronization between PHP applications and MySQL databases

How to use Canal to achieve real-time data synchronization between PHP applications and MySQL databases

WBOY
Release: 2023-05-28 09:37:51
forward
1644 people have browsed it

canal简介

Canal是阿里巴巴开源的一个基于MySQL协议的数据同步工具,可以将MySQL数据库中的数据实时同步到其他数据源中。在PHP应用程序中,可以使用Canal轻松实现与MySQL数据库的实时数据同步,减少了数据同步的延迟和数据丢失的风险,提高了系统的可靠性和实时性。

Canal提供了丰富的API和文档支持,可以方便地进行集成和使用

伪装成mysql从服务器,解析mysql的binlog文件

我主要是用来做redis缓存的更新&以及商品订阅降价的通知

安装

# 这玩意依赖java
# java8以后版本的jdk根据 stdout.log 中的报错对 startup.sh 中的执行参数进行更改

# 我的下载canal位置为 /download/canal
cd /download/canal
# 下载 得到 canal.deployer-1.1.5.tar.gz
wget http://github.com/alibaba/canal/releases/download/canal-1.1.5/canal.deployer-1.1.5.tar.gz
# 解压
tar -zxvf canal-1.1.5/canal.deployer-1.1.5.tar.gz
# /download/canal/当前目录结构
# bin 可执行sh
# conf 配置
# lib 包
# plugin 插件
# logs 日志   stdout.log 位置: /download/canal/logs/canal/stdout.log

配置

# 配置canal上的数据库连接设置 
vim /download/canal/conf/example/instance.properties
canal.instance.master.address=127.0.0.1:3306

# 通过mysql show master status 中的 File
canal.instance.master.journal.name=mysql-bin.000001
# 通过mysql show master status 中的 Position
canal.instance.master.position=856
# 连接mysql的用户名 一定要有读取binlog的权限
canal.instance.dbUsername=canal
canal.instance.dbPassword=canal

# 可以这么创建用户
CREATE USER 'canal'@'%' IDENTIFIED by 'canal';
GRANT SELECT,REPLICATION SLAVE, REPLICATION CLIENT ON *.* TO 'canal'@'%';
FLUSH PRIVILEGES

启动

/download/canal/bin/startup.sh

安装php与canal连接的组件

# github地址
https://github.com/xingwenge/canal-php
# 我在laravel中使用,所以选择composer的安装方式
composer require xingwenge/canal_php

简单示例

//我在laravel下的public同级下建bin/core.php
require __DIR__.'/../vendor/autoload.php';
use xingwenge\canal_php\CanalClient;
use xingwenge\canal_php\CanalConnectorFactory;
use xingwenge\canal_php\Fmt;
try {
    $client = CanalConnectorFactory::createClient(CanalClient::TYPE_SOCKET_CLUE);
    # $client = CanalConnectorFactory::createClient(CanalClient::TYPE_SWOOLE);
    $client->connect("127.0.0.1", 11111);
    $client->checkValid();
//    $client->subscribe("1001", "example", ".*\\..*");
    $client->subscribe("1001", "example", "laravel.school"); # 设置过滤
    while (true) {
        $message = $client->get(100);
        if ($entries = $message->getEntries()) {
            foreach ($entries as $entry) {
                Fmt::println($entry);
            }
        }
        sleep(1);
    }
    $client->disConnect();
} catch (\Exception $e) {
    echo $e->getMessage(), PHP_EOL;
}
Copy after login

结果展示

How to use Canal to achieve real-time data synchronization between PHP applications and MySQL databases

How to use Canal to achieve real-time data synchronization between PHP applications and MySQL databases

The above is the detailed content of How to use Canal to achieve real-time data synchronization between PHP applications and MySQL databases. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:yisu.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template