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