Docker MySQL Canal
mysql docker canal binlog创建 docker network
#+BEGIN_SRC shell
docker network create --subnet=172.18.0.0/16 mynetwork
# 172.17.0.0/16 has been used as default network
#+END_SRC
镜像准备
docker desktop for Mac > Preferences > Docker Engine > 配置 docker 镜像
使用命令
docker pull canal/canal-server
拉取 canal-server 镜像使用命令
docker pull mysql
拉取 mysql 镜像使用命令
docker pull canal/canal-admin
拉取 canal-admin 镜像
容器拉起
- MySQL - 33306
#+BEGIN_SRC shell
docker run -d --name mysql --net mynetwork --ip 172.18.0.6 -p 3306:3306 -e MYSQL_ROOT_PASSWORD=root mysql
#+END_SRC
- Canal-Server
#+BEGIN_SRC shell
docker run -d --name canal-server --net mynetwork --ip 172.18.0.9 -p 11111:11111 canal/canal-server
#+END_SRC
MySQL 的配置修改
以上只是初步准备好了基础的环境, 但是怎么让canal伪装成salve并正确获取mysql中的binary log呢?
对于自建 MySQL , 需要先开启 Binlog
写入功能, 配置 binlog-format
为 ROW 模式, 通过修改mysql配置文件来开启 binlog
, 使用 find / -name my.cnf
查找 my.cnf
, 修改文件内容如下
#+BEGIN_SRC xorg.conf
[mysqld]
log-bin=mysql-bin # 开启 binlog
binlog-format=ROW # 选择 ROW 模式
server_id=1 # 配置 MySQL replaction 需要定义, 不要和 canal 的 slaveId 重复
#+END_SRC
进入 MySQL 容器, 创建账户 canal 并授于相关权限
#+BEGIN_SRC shell
$ docker exec -it mysql /bin/bash
...
# mysql -uroot -proot
...
mysql> CREATE USER canal IDENTIFIED BY 'canal';
Query OK, 0 rows affected (0.01 sec)
mysql> GRANT SELECT, REPLICATION SLAVE, REPLICATION CLIENT ON *.* TO 'canal'@'%';
Query OK, 0 rows affected (0.00 sec)
mysql> -- GRANT ALL PRIVILEGES ON *.* TO 'canal'@'%' ;
mysql> FLUSH PRIVILEGES;
Query OK, 0 rows affected (0.00 sec)
mysql>
#+END_SRC
验证:
#+BEGIN_SRC shell
mysql> show variables like 'log_bin';
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| log_bin | ON |
+---------------+-------+
1 row in set (0.00 sec)
mysql> show variables like 'binlog_format';
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| binlog_format | ROW |
+---------------+-------+
1 row in set (0.00 sec)
mysql> show master status;
+------------------+----------+--------------+------------------+-------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+-------------------+
| mysql-bin.000001 | 156 | | | |
+------------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)
mysql>