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

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

相关文章:

  • 【Python办公】Excel简易透视办公小工具
  • EasyExcel使用导出模版后设置 CellStyle失效问题解决
  • C++ 异步编程与网络编程:工具、协议的层次与协同
  • 金砖国家人工智能高级别论坛在巴西召开,华院计算应邀出席并发表主题演讲
  • 多相电机驱动控制学习(2)——基于双dq的双三相PMSM学习(考虑互感/交叉耦合)
  • 网络协议入门:TCP/IP五层模型如何实现全球数据传输?
  • 学习STC51单片机18(芯片为STC89C52RCRC)
  • 微信小程序页面嵌套web-view点击系统导航返回时进行弹窗处理
  • WPF的基础控件:布局控件(StackPanel DockPanel)
  • RabbitMQ仲裁队列高可用架构解析
  • 720全景展示:VR全景的技术原理及应用
  • 深入理解设计模式之中介者模式
  • AudioTrack的理解
  • 用豆包写单元测试
  • 服务器定时任务查看和编辑
  • 理解并解决高丢包率问题,构建清晰流畅的实时音视频通话
  • 二重积分 -- 立体的体积
  • 网络安全十大漏洞
  • vue3+Pinia+element-plus 后台管理系统项目实战
  • CRM系统的数据库结构详细设计
  • 东莞网站建设网站/腾讯云域名
  • 学做网站前景/网络营销专业大学排名
  • 考试网站建设/品牌公关
  • 学校网站建设会议讲话稿/怎样做公司网站推广
  • 网站数据分析工具/百度seo外包
  • 网站基础建设和维护/广州新闻播报