本文使用docker环境安装mysql、canal、elasticsearch,基于binlog利用canal实现mysql的数据同步到elasticsearch中。
实验中间件版本说明:
- centos 8
- mysql 5.7.36
- es 7.16.2
- cannal.server: 1.1.5
- canal.adapter: 1.1.5
- postman
1、安装docker
基本命令:
csharp
#centos 7 安装 docker
yum install docker
#centos 8 安装docker
yum erase podman buildah
yum install -y yum-utils
yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo
yum install docker-ce docker-ce-cli containerd.io
#检验安装是否成功
[root@localhost opt]# docker --version
Docker version 20.10.12, build e91ed57
#启动
systemctl start docker
#换镜像源
sudo vim /etc/docker/daemon.json
内容如下:
{
"registry-mirrors": ["https://m9r2r2uj.mirror.aliyuncs.com"]
}
保存退出,重启docker
#重启
sudo service docker restart
#列出镜像
docker images
#查看运行进程
docker ps
2、安装mysql
方法1
sql
docker pull mysql:5.7.36
docker run --name mysql5736 -p 3306:3306 -e MYSQL_ROOT_PASSWORD=admin -d mysql:5.7.36
docker exec -it mysql5736 /bin/bash
apt-get update
apt-get install vim
cd /etc/mysql/mysql.conf.d
vim mysqld.cnf // 修改mysql配置
查看配置情况
dart
mysql -uroot -p
show master status // binlog日志文件
reset master; // 重启日志
方法2 或者docker-compose-es -f docker-compose-mysql.yml up -d
yaml
version: '3.9'
services:
mysql:
image: 'mysql:5.7.36'
environment:
- MYSQL_ROOT_PASSWORD=123456
ports:
- '3306:3306'
volumes:
- './mysql/mysql.conf.d/mysqld.cnf:/etc/mysql/mysql.conf.d/mysqld.cnf'
container_name: mysql5736
新建文件 [root@c1 bk]# vi./mysql/mysql.conf.d/mysqld.cnf
ini
# Copyright (c) 2014, 2021, Oracle and/or its affiliates.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License, version 2.0,
# as published by the Free Software Foundation.
#
# This program is also distributed with certain software (including
# but not limited to OpenSSL) that is licensed under separate terms,
# as designated in a particular file or component or in included license
# documentation. The authors of MySQL hereby grant you an additional
# permission to link the program and your derivative works with the
# separately licensed software that they have included with MySQL.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License, version 2.0, for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
#
# The MySQL Server configuration file.
#
# For explanations see
# http://dev.mysql.com/doc/mysql/en/server-system-variables.html
[mysqld]
pid-file = /var/run/mysqld/mysqld.pid
socket = /var/run/mysqld/mysqld.sock
datadir = /var/lib/mysql
#log-error = /var/log/mysql/error.log
log-bin=/var/log/mysql/mysql-bin
binlog-format=ROW
server-id=1
# By default we only accept connections from localhost
#bind-address = 127.0.0.1
# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0
查看日志文件:
c
cd /var/log/mysql/ // 进入日志文件目录
mysqlbinlog -vv mysql-bin.000001 // row格式查看日志
3、安装es
方法1
arduino
docker pull elasticsearch:7.16.2
docker run -p 9200:9200 -p 9300:9300 -e "discovery.type=single-node" --name='es7162' -d elasticsearch:7.16.2
然后我们需要配置一下es的信息:
bash
docker exec -ites es7162 /bin/bash
cd config
vi elasticsearch.yml
配置文件:
yaml
cluster.name: dailyhub-es
network.host: 0.0.0.0
node.name: node-1
http.port: 9200
http.cors.enabled: true
http.cors.allow-origin: "*"
node.master: true
node.data: true
方法2 或者 [root@c1 bk]# docker-compose-es -f docker-compose-es.yml up -d
vbnet
version: '3.9'
services:
elasticsearch:
image: 'elasticsearch:7.16.2'
container_name: es7162
environment:
- discovery.type=single-node
volumes:
- './elasticsearch/config/elasticsearch.yml:/usr/share/elasticsearch/config/elasticsearch.yml'
ports:
- '9300:9300'
- '9200:9200'
配置文件
yaml
[root@c1 bk]# cat elasticsearch/config/elasticsearch.yml
cluster.name: "docker-cluster"
network.host: 0.0.0.0
node.name: node-1
http.port: 9200
http.cors.enabled: true
http.cors.allow-origin: "*"
node.master: true
node.data: true
// 查询es所有mapping
ini
http://119.45.25.164:9200/_mapping?pretty=true
1、在线安装分词器
bash
docker exec -ites es7162 /bin/bash
./bin/elasticsearch-plugin install https://github.com/medcl/elasticsearch-analysis-ik/releases/download/v7.16.2/elasticsearch-analysis-ik-7.16.2.zip
2、离线安装中文分词器:
首先打开这个链接:github.com/medcl/elast...,把分词器插件下载下来,
bash
# 把插件复制到容器内
docker cp elasticsearch-analysis-ik-7.16.2.zip es7162:/usr/share/elasticsearch/plugins
docker exec -it es7162 /bin/bash
cd /usr/share/elasticsearch/plugins/
mkdir ik
unzip elasticsearch-analysis-ik-7.16.2.zip -d ik
rm -rf elasticsearch-analysis-ik-7.16.2.zip
查看是否安装成功、当你看到日志中有输出analysis-ik,说明已经安装成功。
docker restart es7162
docker logs es7162

4、安装canal-server
方法1
拉取镜像并启动:
bash
docker pull canal/canal-server:v1.1.5
docker run --name canal115 -p 11111:11111 --link mysql5736:mysql5736 -id canal/canal-server:v1.1.5
修改对应的配置:
ini
docker exec -it canal115 /bin/bash
cd canal-server/conf/example/
vi instance.properties // 修改配置
# 把0改成10,只要不和mysql的id相同就行
canal.instance.mysql.slaveId=10
# 修改成mysql对应的账号密码,mysql5736就是mysql镜像的链接别名
canal.instance.master.address=mysql5736:3306
canal.instance.dbUsername=root
canal.instance.dbPassword=admin

验证配置是否成功:
bash
#首先重启一下canal
docker restart canal115
docker exec -it canal115 /bin/bash
cd canal-server/logs/example/
tail -100f example.log // 查看日志
截图如下,说明已经链接上了mysql主机,此时mysql中的数据变化,都会在canal中有同步。

方法二:
root@c1 bk\]# docker-compose-es -f docker-compose-canal-server.yml up -d
```yaml
version: '3.9'
services:
canal-server:
image: 'canal/canal-server:v1.1.5'
stdin_open: true
links:
- 'mysql5736:mysql5736'
ports:
- '11111:11111'
volumes:
- './canal-server/conf/example/instance.properties:/home/admin/canal-server/conf/example/instance.properties'
container_name: canal115
```
配置文件
```ini
cat canal-server/conf/example/instance.properties
#################################################
## mysql serverId , v1.0.26+ will autoGen
canal.instance.mysql.slaveId=10
# enable gtid use true/false
canal.instance.gtidon=false
# position info
canal.instance.master.address=192.168.8.93: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=123@456
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,mytest2\\..*,.*\\..*
canal.mq.partition=0
# hash partition config
#canal.mq.partitionsNum=3
#canal.mq.partitionHash=test.table:id^name,.*\\..*
#canal.mq.dynamicTopicPartitionNum=test.*:4,mycanal:6
#################################################
```
## java验证canal-server
可以通过Java程序测试有没连接上mysql:
导入canal-client包
```xml