MySQL 数据迁移Postgresql(openGuass) 之 pg_chameleon
1 pg_chameleon 介绍
pgchameleon 是一款MySQL 到 PostgreSQL/openGuass 的复制工具。
pg_chameleon 使用场景:
1)分析
2)迁移
3)对多个MySQL 数据库进行数据聚合
操作系统信息
(myenv) root@u24-pg-60:~# cat /etc/issue
Ubuntu 24.04.2 LTS \n \l
2 主机列表
192.168.254.50 #mysql源库
192.168.254.60 #Postgresql 目标数据库
192.168.254.61 #安装pg_chameleon
3 mysql 数据库环境准备(192.168.254.50)
vi /etc/my.cnf
binlog_format = ROW
binlog_rows_query_log_events = on
binlog_row_image = full
binlog_row_metadata = full
binlog_row_image = full
autocommit = on
使用pg_chameleon创建用于配置复制的用户,并使用以下步骤为用户提供适当的权限.
$ mysql -uroot -p123456
create user 'repuser'@'%' identified by 'repuser123';
GRANT ALL ON *.* TO 'repuser'@'%';
GRANT RELOAD,REPLICATION CLIENT,REPLICATION SLAVE ON *.* TO 'repuser'@'%';
FLUSH PRIVILEGES;
#源数据准备
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| itpux |
| itpux01 |
| itpux02 |
| itpux03 |
| mydb |
| mysql |
| performance_schema |
| sys |
+--------------------+
4 Postgresql 环境准备(192.168.254.60)
在PostgreSQL中创建一个用户,pg_Chameleon可以使用该用户将更改的数据写入PostgreSQL.还要创建目标数据库.
$psql -U postgres
CREATE USER repuser WITH ENCRYPTED PASSWORD 'repuser123';
CREATE DATABASE mydb WITH OWNER repuser;
5 安装pg_chameleon(192.168.254.61)
#apt install python3-pip -y
#root 用户无法启动pg_chameleon
su - postgres
python3 -m venv myenv
source myenv/bin/activate
pip install --upgrade setuptools
pip install pg_chameleon
将示例配置文件复制到另一个文件,例如default.yml
cd .pg_chameleon/configuration
cp config-example.yml default.yml
修改default.yml
# postgres destination connection
pg_conn:
host: "192.168.254.60"
port: "5432"
user: "repuser"
password: "repuser123"
database: "mydb"
charset: "utf8"
sources:
mysql:
db_conn:
host: "192.168.254.50"
port: "3306"
user: "repuser"
password: "repuser123"
charset: 'utf8'
connect_timeout: 10
schema_mappings:
itpux: public
itpux01: itpux01
itpux02: itpux02
itpux03: itpux03
limit_tables:
- delphis_mediterranea.foo
skip_tables:
- delphis_mediterranea.bar
grant_select_to:
- usr_readonly
lock_timeout: "120s"
my_server_id: 100
replica_batch_size: 10000
replay_max_rows: 10000
batch_retention: '1 day'
copy_max_memory: "300M"
copy_mode: 'file'
out_dir: /tmp
sleep_loop: 1
on_error_replay: continue
on_error_read: continue
auto_maintenance: "disabled"
gtid_enable: false
type: mysql
skip_events:
insert:
- delphis_mediterranea.foo # skips inserts on delphis_mediterranea.foo
delete:
- delphis_mediterranea # skips deletes on schema delphis_mediterranea
update:
keep_existing_schema: No
net_read_timeout: 600
参数说明:
#目标Postgresql数据库配置(192.168.254.61)
pg_conn:
host: "192.168.254.60" #主机
port: "5432" #端口
user: "repuser" #用户
password: "repuser123" #密码
database: "mydb" #数据库
charset: "utf8" #编码
#源数MySQL据库(192.168.254.50)
sources:
mysql:
db_conn:
host: "192.168.254.50" #主机
port: "3306" #端口
user: "repuser" #用户
password: "repuser123" #密码
charset: 'utf8' #编码
connect_timeout: 10 #连接超时
schema_mappings:
itpux: public #mysql的itpux库 对应Postgresql的mydb库public schema
itpux01: itpux01 #mysql的itpux01库 对应Postgresql的mydb库itpux01 schema
itpux02: itpux02 #mysql的itpux02库 对应Postgresql的mydb库itpux02 schema
itpux03: itpux03 #mysql的itpux03库 对应Postgresql的mydb库itpux03 schema
#开始数据库迁移操作步骤
1 初始化副本(在192.168.254.61 #安装pg_chameleon)
命令:
chameleon create_replica_schema --debug
在Postgresql目标库查看(192.168.254.60)
上面的命令在.pg_chameleon/configuration/default.yml文件中指定的PostgreSQL数据库中创建一个模式和13个表,
需要这些表来管理从源到目标的复制.在以下日志中可以观察到相同的情况.
$psql
\dt sch_chameleon.t_*
mydb=# \dt sch_chameleon.*
List of relations
Schema | Name | Type | Owner
---------------+-----------------------+-------+---------
sch_chameleon | t_batch_events | table | repuser
sch_chameleon | t_discarded_rows | table | repuser
sch_chameleon | t_error_log | table | repuser
sch_chameleon | t_fkeys | table | repuser
sch_chameleon | t_indexes | table | repuser
sch_chameleon | t_last_received | table | repuser
sch_chameleon | t_last_replayed | table | repuser
sch_chameleon | t_log_replica | table | repuser
sch_chameleon | t_log_replica_mysql_1 | table | repuser
sch_chameleon | t_log_replica_mysql_2 | table | repuser
sch_chameleon | t_pkeys | table | repuser
sch_chameleon | t_replica_batch | table | repuser
sch_chameleon | t_replica_tables | table | repuser
sch_chameleon | t_sources | table | repuser
sch_chameleon | t_ukeys | table | repuser
(15 rows)
2 指定源详细信息(在192.168.254.61 #安装pg_chameleon)
使用以下命令将源详细信息添加到pg_chameleon.提供配置文件中指定的源名称。
在此示例中,源名称为mysql,目标是在pg_conn下定义的postgreSQL数据库.
命令:
chameleon add_source --config default --source mysql --debug
chameleon show_status --config default
在Postgresql目标库查看(192.168.254.60)
运行完上述命令后,查看表t_sources即可看到源详细信息已添加到表t_sources中.
\x
select * from sch_chameleon.t_sources;
mydb=# select * from sch_chameleon.t_sources;
-[ RECORD 1 ]-------+--------------------------------------------------------------------------------------
i_id_source | 1
t_source | mysql
jsb_schema_mappings | {"itpux": "public", "itpux01": "itpux01", "itpux02": "itpux02", "itpux03": "itpux03"}
enm_status | initialised
t_binlog_name | mysql-bin.000008
i_binlog_position | 14868456
b_consistent | f
b_paused | f
b_maintenance | f
ts_last_maintenance |
enm_source_type | mysql
v_log_table | {t_log_replica_mysql_2,t_log_replica_mysql_1}
3 初始化目标库
命令:
chameleon init_replica --config default --source mysql --debug
#一下日志表示完成迁移
2025-05-28 20:39:27 MainProcess INFO pg_lib.py (3464): Set high watermark for source: mysql
2025-05-28 20:39:27 MainProcess INFO mysql_lib.py (1590): init replica for source mysql is complete
在Postgresql目标库查看(192.168.254.60)
检查数据是否已经迁移
mydb=# \dn
List of schemas
Name | Owner
---------------+---------
itpux01 | repuser
itpux02 | repuser
itpux03 | repuser
public | repuser
sch_chameleon | repuser
(5 rows)
mydb=# \dt public.*
List of relations
Schema | Name | Type | Owner
--------+---------+-------+---------
public | itpux01 | table | repuser
public | itpux02 | table | repuser
public | itpux03 | table | repuser
public | test | table | repuser