1. 准备MySql主库
1.1.在服务器新建文件夹 mysql/data,新建文件 mysql/conf.d/my.cnf
其中my.cnf 内容如下
[mysqld]
log_timestamps=SYSTEM
default-time-zone='+8:00'
server-id=1
log-bin=mysql-bin
binlog-do-db = mall # 要监听的库
binlog_format=ROW
配置解读:
① server-id :指定当前服务的id,必须指定,否则会报错
② log-bin :设置binlog文件的存放地址和文件名,叫做mysql-bin,此处指定的目录前缀是mysql容器的数据存放目录,所以可以在挂载目录中看到相关的文件,命名格式是mysql-bin.000001开始慢慢叠加
③ binlog-do-db :指定针对哪个数据库记录binlog的events事件,此处记录mall库
如果在 MySQL 配置中没有显式地配置 binlog-do-db 参数,那么 MySQL 的二进制日志(binlog)将会记录所有数据库的变更操作。
binlog-do-db 参数用于指定需要被记录到 binlog 中的数据库。通过配置 binlog-do-db,你可以选择只记录指定的数据库的变更操作,而忽略其他数据库的变更操作。
如果没有配置 binlog-do-db 参数,或者将其设置为一个空值(例如 binlog-do-db=),那么 MySQL 将会监听并记录所有数据库的变更操作到 binlog 中。
需要注意的是,即使没有配置 binlog-do-db 参数,你仍然可以通过其他方式(如 MySQL 的权限控制)来限制用户对数据库的操作权限,从而间接地控制 binlog 中的记录内容
- binlog_format
在 MySQL 配置中,binlog_format 是用于配置二进制日志(binlog)的格式。binlog 是 MySQL 中用于记录数据库的变更操作的日志文件。
binlog_format 可以设置为以下几种值:
-
STATEMENT:以 SQL 语句的形式记录数据库的变更操作。这种格式记录的是每个执行的 SQL 语句,可以通过 replay SQL 语句的方式来还原数据变更。但是,由于某些情况下,同一个 SQL 语句在不同的数据库上执行可能会产生不同的结果,所以在使用 STATEMENT 格式时需要注意一致性问题。
-
ROW:以行的形式记录数据库的变更操作。这种格式记录的是每一行数据的变更情况,包括被修改、插入或删除的数据。ROW 格式记录了更加详细和精确的变更信息,但相对于 STATEMENT 格式来说,会占用更多的存储空间。
-
MIXED:混合模式,根据具体的情况自动选择 STATEMENT 或 ROW 格式来记录数据库的变更操作。MIXED 模式会根据 SQL 语句的类型和特性来决定使用哪种格式,以达到性能和存储空间的平衡。
binlog_format 的选择需要根据实际需求和应用场景来决定。不同的格式具有不同的优缺点,需要根据具体情况来进行权衡和选择
1.2.启动数据库
docker run --name mysql01 \
-p 3306:3306 \
-v /opt/mysql/conf.d:/etc/mysql/conf.d \
-v /opt/mysql/data:/var/lib/mysql \
-e MYSQL_ROOT_PASSWORD=123456 \
-d mysql:8.0
1.3.校验是否开启成功
show variables like 'log_%';
show variables like 'binlog_format';
show variables like 'server_id';
--查看所有日志
show binlog events;
--查看最新的日志
show master status
-- 查询指定的binlog日志
show binlog events in 'XTZJ-20221008CY-bin.000020'
--清空所有的 binlog 日志文件reset master
2.DML与DDL
MySQL中,DML(Data Manipulation Language)和DDL(Data Definition Language)是两种不同类型的SQL语句,它们分别用于不同的数据库操作目的:
DML(数据操作语言): DML语句主要用于对数据库表中的实际数据进行操作,主要包括以下几种命令:
-
INSERT
:向表中插入新的行数据。 -
UPDATE
:更新表中已存在的行数据。 -
DELETE
:从表中删除满足特定条件的行数据。 -
SELECT
:从表中检索数据,虽然SELECT
不改变数据本身,但因其属于对数据的操作,所以也被归类于DML。
DML操作通常发生在事务中,可以被用户手动控制事务的开启、提交和回滚,确保数据的一致性和完整性。
DDL(数据定义语言): DDL语句主要用于创建、修改或删除数据库的结构元素,例如:
-
CREATE
:创建新的数据库、表、索引、视图等。 -
ALTER
:更改现有数据库对象的结构,例如增加或删除列,修改列的数据类型,重命名表等。 -
DROP
:删除数据库对象,如表、索引、视图等。 -
TRUNCATE
:清空表的内容,但保留表的结构。
3. 安装canal
3.1.新建文件夹logs, 新建文件canal.properties instance.properties docker.compose.yml
如下图
1.canal.properties
#################################################
######### common argument #############
#################################################
# tcp bind ip
canal.ip =
# register ip to zookeeper
canal.register.ip =
canal.port = 11111
canal.metrics.pull.port = 11112
# canal instance user/passwd
# canal.user = canal
# canal.passwd = E3619321C1A937C46A0D8BD1DAC39F93B27D4458
# canal admin config
#canal.admin.manager = 127.0.0.1:8089
canal.admin.port = 11110
canal.admin.user = admin
canal.admin.passwd = 4ACFE3202A5FF5CF467898FC58AAB1D615029441
# admin auto register
#canal.admin.register.auto = true
#canal.admin.register.cluster =
#canal.admin.register.name =
canal.zkServers =
# flush data to zk
canal.zookeeper.flush.period = 1000
canal.withoutNetty = false
# tcp, kafka, rocketMQ, rabbitMQ, pulsarMQ
canal.serverMode = rabbitMQ
# flush meta cursor/parse position to file
canal.file.data.dir = ${canal.conf.dir}
canal.file.flush.period = 1000
## memory store RingBuffer size, should be Math.pow(2,n)
canal.instance.memory.buffer.size = 16384
## memory store RingBuffer used memory unit size , default 1kb
canal.instance.memory.buffer.memunit = 1024
## meory store gets mode used MEMSIZE or ITEMSIZE
canal.instance.memory.batch.mode = MEMSIZE
canal.instance.memory.rawEntry = true
## detecing config
canal.instance.detecting.enable = false
#canal.instance.detecting.sql = insert into retl.xdual values(1,now()) on duplicate key update x=now()
canal.instance.detecting.sql = select 1
canal.instance.detecting.interval.time = 3
canal.instance.detecting.retry.threshold = 3
canal.instance.detecting.heartbeatHaEnable = false
# support maximum transaction size, more than the size of the transaction will be cut into multiple transactions delivery
canal.instance.transaction.size = 1024
# mysql fallback connected to new master should fallback times
canal.instance.fallbackIntervalInSeconds = 60
# network config
canal.instance.network.receiveBufferSize = 16384
canal.instance.network.sendBufferSize = 16384
canal.instance.network.soTimeout = 30
# binlog filter config
canal.instance.filter.druid.ddl = true
canal.instance.filter.query.dcl = false
canal.instance.filter.query.dml = false
canal.instance.filter.query.ddl = false
canal.instance.filter.table.error = false
canal.instance.filter.rows = false
canal.instance.filter.transaction.entry = false
canal.instance.filter.dml.insert = false
canal.instance.filter.dml.update = false
canal.instance.filter.dml.delete = false
# binlog format/image check
canal.instance.binlog.format = ROW,STATEMENT,MIXED
canal.instance.binlog.image = FULL,MINIMAL,NOBLOB
# binlog ddl isolation
canal.instance.get.ddl.isolation = false
# parallel parser config
canal.instance.parser.parallel = true
## concurrent thread number, default 60% available processors, suggest not to exceed Runtime.getRuntime().availableProcessors()
#canal.instance.parser.parallelThreadSize = 16
## disruptor ringbuffer size, must be power of 2
canal.instance.parser.parallelBufferSize = 256
# table meta tsdb info
canal.instance.tsdb.enable = true
canal.instance.tsdb.dir = ${canal.file.data.dir:../conf}/${canal.instance.destination:}
canal.instance.tsdb.url = jdbc:h2:${canal.instance.tsdb.dir}/h2;CACHE_SIZE=1000;MODE=MYSQL;
canal.instance.tsdb.dbUsername = canal
canal.instance.tsdb.dbPassword = canal
# dump snapshot interval, default 24 hour
canal.instance.tsdb.snapshot.interval = 24
# purge snapshot expire , default 360 hour(15 days)
canal.instance.tsdb.snapshot.expire = 360
#################################################
######### destinations #############
#################################################
canal.destinations = example
# conf root dir
canal.conf.dir = ../conf
# auto scan instance dir add/remove and start/stop instance
canal.auto.scan = true
canal.auto.scan.interval = 5
# set this value to 'true' means that when binlog pos not found, skip to latest.
# WARN: pls keep 'false' in production env, or if you know what you want.
canal.auto.reset.latest.pos.mode = false
canal.instance.tsdb.spring.xml = classpath:spring/tsdb/h2-tsdb.xml
#canal.instance.tsdb.spring.xml = classpath:spring/tsdb/mysql-tsdb.xml
canal.instance.global.mode = spring
canal.instance.global.lazy = false
canal.instance.global.manager.address = ${canal.admin.manager}
#canal.instance.global.spring.xml = classpath:spring/memory-instance.xml
canal.instance.global.spring.xml = classpath:spring/file-instance.xml
#canal.instance.global.spring.xml = classpath:spring/default-instance.xml
##################################################
######### MQ Properties #############
##################################################
# aliyun ak/sk , support rds/mq
canal.aliyun.accessKey =
canal.aliyun.secretKey =
canal.aliyun.uid=
canal.mq.flatMessage = true
canal.mq.canalBatchSize = 50
canal.mq.canalGetTimeout = 100
# Set this value to "cloud", if you want open message trace feature in aliyun.
canal.mq.accessChannel = local
canal.mq.database.hash = true
canal.mq.send.thread.size = 30
canal.mq.build.thread.size = 8
##################################################
######### Kafka #############
##################################################
kafka.bootstrap.servers = 127.0.0.1:9092
kafka.acks = all
kafka.compression.type = none
kafka.batch.size = 16384
kafka.linger.ms = 1
kafka.max.request.size = 1048576
kafka.buffer.memory = 33554432
kafka.max.in.flight.requests.per.connection = 1
kafka.retries = 0
kafka.kerberos.enable = false
kafka.kerberos.krb5.file = ../conf/kerberos/krb5.conf
kafka.kerberos.jaas.file = ../conf/kerberos/jaas.conf
# sasl demo
# kafka.sasl.jaas.config = org.apache.kafka.common.security.scram.ScramLoginModule required \\n username=\"alice\" \\npassword="alice-secret\";
# kafka.sasl.mechanism = SCRAM-SHA-512
# kafka.security.protocol = SASL_PLAINTEXT
##################################################
######### RocketMQ #############
##################################################
rocketmq.producer.group = test
rocketmq.enable.message.trace = false
rocketmq.customized.trace.topic =
rocketmq.namespace =
rocketmq.namesrv.addr = 127.0.0.1:9876
rocketmq.retry.times.when.send.failed = 0
rocketmq.vip.channel.enabled = false
rocketmq.tag =
##################################################
######### RabbitMQ #############
##################################################
rabbitmq.host = 192.168.11.48:5672
rabbitmq.virtual.host = /
rabbitmq.exchange = canal-exchange
rabbitmq.username = guest
rabbitmq.password = guest
rabbitmq.deliveryMode = 2
##################################################
######### Pulsar #############
##################################################
pulsarmq.serverUrl =
pulsarmq.roleToken =
pulsarmq.topicTenantPrefix =
-
docker-compose.yml
version: '3'
services:
canal:
container_name: canal_latest
image: canal/canal-server:v1.1.5
restart: always
ports:
- 11111:11111
volumes:
- ./canal.properties:/home/admin/canal-server/conf/canal.properties
- ./instance.properties:/home/admin/canal-server/conf/example/instance.properties
- ./logs:/home/admin/canal-server/logs
3.instance.properties
#################################################
## mysql serverId , v1.0.26+ will autoGen
# canal.instance.mysql.slaveId=0
# enable gtid use true/false
canal.instance.gtidon=false
# position info
canal.instance.master.address=192.168.11.43:3306
canal.instance.master.journal.name=
canal.instance.master.position=
canal.instance.master.timestamp=
canal.instance.master.gtid=
# rds oss binlog
canal.instance.rds.accesskey=
canal.instance.rds.secretkey=
canal.instance.rds.instanceId=
# table meta tsdb info
canal.instance.tsdb.enable=true
#canal.instance.tsdb.url=jdbc:mysql://127.0.0.1:3306/canal_tsdb
#canal.instance.tsdb.dbUsername=canal
#canal.instance.tsdb.dbPassword=canal
#canal.instance.standby.address =
#canal.instance.standby.journal.name =
#canal.instance.standby.position =
#canal.instance.standby.timestamp =
#canal.instance.standby.gtid=
# username/password
canal.instance.dbUsername=root
canal.instance.dbPassword=123456
canal.instance.connectionCharset = UTF-8
# enable druid Decrypt database password
canal.instance.enableDruid=false
#canal.instance.pwdPublicKey=MFwwDQYJKoZIhvcNAQEBBQADSwAwSAJBALK4BUxdDltRRE5/zXpVEVPUgunvscYFtEip3pmLlhrWpacX7y7GCMo2/JM6LeHmiiNdH1FWgGCpUfircSwlWKUCAwEAAQ==
# table regex
canal.instance.filter.regex=.*\\..*
# table black regex
canal.instance.filter.black.regex=mysql\\.slave_.*
# table field filter(format: schema1.tableName1:field1/field2,schema2.tableName2:field1/field2)
#canal.instance.filter.field=test1.t_product:id/subject/keywords,test2.t_company:id/name/contact/ch
# table field black filter(format: schema1.tableName1:field1/field2,schema2.tableName2:field1/field2)
#canal.instance.filter.black.field=test1.t_product:subject/product_image,test2.t_company:id/name/contact/ch
# mq config
canal.mq.topic=example
# dynamic topic route by schema or table regex
#canal.mq.dynamicTopic=mytest1.user,topic2:mytest2\\..*,.*\\..*
canal.mq.partition=0
# hash partition config
#canal.mq.enableDynamicQueuePartition=false
#canal.mq.partitionsNum=3
#canal.mq.dynamicTopicPartitionNum=test.*:4,mycanal:6
#canal.mq.partitionHash=test.table:id^name,.*\\..*
#################################################
修改canal.properties内容如下
修改instance.properties内容如下
2.启动容器
切换到
此目录下进行启动容器
docker-compose up -d
3.查看结果
如若数据库和ip问题,先检查配置,可以尝试修改数据库用户
在重置密码,在执行reset master语句,重启docker在进行尝试