当前位置: 首页 > web >正文

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

http://www.xdnf.cn/news/9549.html

相关文章:

  • BGP实验报告
  • SQLiteStudio - 免费开源、轻量高效,跨平台的 SQLite 数据库管理工具,代替 Navicat for SQLite
  • 【已解决】windows gitbash 出现CondaError: Run ‘conda init‘ before ‘conda activate‘
  • 深入探讨集合与数组转换方法
  • 如何实现电竞比赛的实时直播?
  • 如何收集Oracle DB SQL Monitor报告
  • JavaScript性能优化实战大纲
  • win10 pip安装插件包报错:No matching distribution found for pytest-xlsx
  • nohup命令基本用法
  • delta 流响应
  • 华为手机用的时间长了,提示手机电池性能下降,需要去换电池吗?平时要怎么用能让电池寿命长久一些?
  • Android Compose开发架构选择指南:单Activity vs 多Activity
  • Nginx代理SSL 到Spring boot
  • 多相电机驱动控制学习(2)——基于双dq的双三相PMSM学习(考虑互感/交叉耦合)
  • Chroma 向量数据库使用示例
  • UE5 Niagara 如何让四元数进行旋转
  • 单片机 串口发送和接收
  • ⚡ Linux 系统安装与配置 Vim 编辑器(包括 Vim 插件管理器)
  • RTOS 完整概述与实战应用:从基础原理到产业实情
  • 论文略读:Deep reinforcement learning for community architectural layout generation
  • Dolphinscheduler-3.2.0分布式集群详细部署
  • 时间的基本概念与相关技术二
  • 如何将多张图组合到一张图里同时保留高的分辨率(用PPT+AdobeAcrobat)
  • 用 Appuploader,让 iOS 上架流程真正“可交接、可记录、可复用”:我们是这样实现的
  • 能按需拆分 PDF 为多个文档的工具
  • Linux C++ 开发基础命令指南
  • 亚远景-ISO 21434标准:汽车网络安全实践的落地指南
  • 基于深度学习的工业OCR实践:仪器仪表数字识别技术详解
  • qt之开发大恒usb3.0相机三
  • 基于python,html,flask,echart,ids/ips,VMware,mysql,在线sdn防御ddos系统