docker run --name mysql3307 -p 3307:3306 -e MYSQL_ROOT_PASSWORD=rootpassword --restart always -d mysql:5.7.30
binlog配置
docker exec -it mysql3307 bash
cd /etc/mysql/mysql.conf.d
cat -n mysqld.cnf
往pid-file那行前插入,注意,一定要写在pid-file配置前面
sed '30 ilog-bin=mysql-bin\nbinlog-format=ROW\nserver_id=1' -i mysqld.cnf
mysql中创建canal_user用户,密码canal_password,赋予select等权限
mysql -uroot -p
CREATE USER canal_user IDENTIFIED BY 'canal_password';
GRANT SELECT, REPLICATION SLAVE, REPLICATION CLIENT ON *.* TO 'canal_user'@'%';
FLUSH PRIVILEGES;
exit
docker重启mysql3307
exit
docker restart mysql3307
docker exec -it mysql3307 bash
mysql -uroot -p
show variables like 'log_bin';
show variables like 'binlog_format';
show master status;
create database test;
use test;
user表:
create table user(
id int(11) unsigned not null primary key auto_increment,
name varchar(25) not null default '',
role_id int(11) not null default 0
) engine=innodb default charset=utf8mb4;
jdk下载地址:https://www.oracle.com/java/technologies/javase/javase-jdk8-downloads.html
下载 jdk-8u261-linux-x64.tar.gz
sudo mkdir /usr/local/java
sudo mv jdk1.8.0_261 /usr/local/java
sudo vim /etc/profile
末尾加入
export JAVA_HOME=/usr/local/java/jdk1.8.0_261
export JRE_HOME=${JAVA_HOME}/jre
export CLASSPATH=.:${JAVA_HOME}/lib:${JRE_HOME}/lib
export PATH=${JAVA_HOME}/bin:$PATH
环境变量生效
source /etc/profile
下载地址:https://github.com/alibaba/canal/releases/download/canal-1.1.4/canal.adapter-1.1.4.tar.gz
sudo mkdir canal-server && sudo tar -zxvf canal.adapter-1.1.4.tar.gz -C canal-server
解压后目录结构
root@wcjs-wxsearch-1:/canal-server# ls
bin conf lib logs
cd canal-server/conf
cp -r example myinstance
修改canal-server默认读取 myinstance 下面的配置文件
vim canal.properties
找到 canal.destinations 配置项
# canal.destinations = example
canal.destinations = myinstance
:wq保存退出
接着进入 myinstance 目录,配置 mysql的项目配置项
cd myinstance
vim instance.properties
找到canal.instance.master.address,修改为自己的mysql 的地址和端口
# canal.instance.master.address = 127.0.0.1:3306
canal.instance.master.address = 127.0.0.1:3307
修改mysql帐号密码
# canal.instance.dbUsername=canal
# canal.instance.dbPassword=canal
canal.instance.dbUsername=canal_user
canal.instance.dbPassword=canal_password
修改canal-server要监听哪些数据库的哪些表的变动
默认是监听所有库下面的所有表,如下:
canal.instance.filter.regex=.*\\..*
如我只监听test库的user表,修改如下:
# canal.instance.filter.regex=.*\\..*
canal.instance.filter.regex=test.user
:wq保存退出
cd ../..
./bin/startup.sh
less logs/myinstance/myinstance.log
有错可以去项目仓库的issue看看:https://github.com/alibaba/canal/issues
docker run --name es -p 9200:9200 -p 9300:9300 -e "discovery.type=single-node" --restart always -d elasticsearch:6.5.4
查看es容器的cluster name
docker exec -it es bash
cd config/
vi elasticsearch.yml
下载地址:https://github.com/alibaba/canal/releases/download/canal-1.1.4/canal.deployer-1.1.4.tar.gz
mkdir canal-adapter && tar -zxvf canal.adapter-1.1.3.tar.gz -C canal-adapter
cd canal-adapter
vim conf/application.yml
修改mysql配置
srcDataSources:
defaultDS:
url: jdbc:mysql://127.0.0.1:3307/test?useUnicode=true
username: canal_user
password: canal_password
修改实例名
canalAdapters:
- instance: myinstance
修改es配置:
- name: es
hosts: 127.0.0.1:9300 # 127.0.0.1:9200 for rest mode
properties:
mode: transport # or rest
# security.auth: user:paword # only used for rest mode
cluster.name: docker-cluster
:wq保存退出
修改mysql到es的映射关系
cp conf/es/mytest_user.yml conf/es/test.yml
vim conf/es/test.yml
修改实例名
dataSourceKey: defaultDS
# destination: example
destination: myinstance
groupId: g1
esMapping:
_index: test
_type: _doc
_id: _id
upsert: true
# pk: id
sql: "select id as _id, name, role_id from user"
# objFields:
# _labels: array:;
# etlCondition: "where a.c_time>='{0}'"
commitBatch: 3000
:wq保存退出
./bin/startup.sh
less logs/adapter/adapter.log
往数据库插入一条数据
insert into user(name,role_id) value ('pxh', 4);
https://www.alibabacloud.com/help/zh/doc-detail/135297.htm
https://github.com/alibaba/canal