MySQL迁移PostgreSQL--py-mysql2pgsql迁移

本文介绍了如何使用py-mysql2pgsql工具进行MySQL到PostgreSQL的数据迁移,包括数据类型转化、自动化迁移过程和具体操作步骤,确保迁移的无缝对接。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

作者:徐田原

1.简介

介绍一款MySQL迁移PostgreSQL的自动化工具py-mysql2pgsql,该工具通过python使用不落地方式,或写出到文件方式(可选),直接将MySQL中的表结构转化到pg,并自动导入数据、自动在pg端创建相关索引。

2.MySQL迁移PostgreSQL数据类型转化

目前该工具可将以下38种数据类型自动转化为PostgreSQL对应的兼容类型,如需其他类型转化,在配置文件添加即可。

MySQLPostgreSQLcharcharactervarcharcharacter varyingtinytexttextmediumtexttexttexttextlongtexttexttinyblobbyteamediumblobbyteablobbytealongblobbyteabinarybyteavarbinarybyteabitbit varyingtinyintsmallinttinyint unsignedsmallintsmallintsmallintsmallint unsignedintegermediumintintegermediumint unsignedintegerintintegerint unsignedbigintbigintbigintbigint unsignednumericfloatrealfloat unsignedrealdoubledouble precisiondouble unsigneddouble
precisiondecimalnumericdecimal unsignednumericnumericnumericnumeric unsignednumericdatedatedatetimetimestamp without time zonetimetime without time zonetimestamptimestamp without time zoneyearsmallintenumcharacter varying (with check
constraint)setARRAY[]::text[]

3.自动化迁移过程

工具运行后,整个过程可以分为三个阶段:

  • 在PostgreSQL端自动创建表结构;
  • 在PostgreSQL端自动loading加载数据;
  • 在PostgreSQL端自动创建索引、约束。

4.工具安装及使用例子

4.1 下载源码安装包

下载地址:
https://pypi.org/project/py-mysql2pgsql/#description

1、解压安装

2、需解决的依赖包:

python需按照这几个依赖包:termcolor、mysql-python、psycopg2、argparse

3、安装mysql2pgsql

[root@123 ]# pwd
/data/aken/tools/py-mysql2pgsql-0.1.5
[root@123 ]# python setup.py install

4、验证安装,查看help帮助

[root@123 /data/aken/tools/py-mysql2pgsql-0.1.5]# py-mysql2pgsql -h
usage: py-mysql2pgsql [-h] [-v] [-f FILE] [-V]
​
Tool for migrating/converting data from mysql to postgresql.
​
optional arguments:
 -h, --help     show this help message and exit
 -v, --verbose    Show progress of data migration.
 -f FILE, --file FILE Location of configuration file (default:mysql2pgsql.yml). If none exists at that path, one will be created for you.
 -V, --version    Print version and exit.
​
https://github.com/philipsoutham/py-mysql2pgsql

4.2 迁移存量数据

如下将MySQL中的test.tab_testtext表存量迁移到PostgreSQL中pg的public下面。

1、编辑迁移配置文件

vi mysql2pgsql.yml 如下:
#source 
# if a socket is specified we will use that
# if tcp is chosen you can use compression
​
mysql:  
 hostname: 100.66.66.66
 port: 15140
 socket:
 username: dbmgr
 password: 520DBA
 database: test
 compress: false
​
destination:
 # if file is given, output goes to file, else postgres. 
 file:
 postgres:
 hostname: 100.88.88.88
 port: 11005
 username: aken
 password: aken123
 database: pg
​
# 指定迁移的表,默认迁移database下所有表。if only_tables is given, only the listed tables will be converted. leave empty to convert all tables.
only_tables:
#- table1
#- table2
 tab_testtext
​
# 指定排除的表。if exclude_tables is given, exclude the listed tables from the conversion.
#exclude_tables:
#- table3
#- table4# 指定是否只导出表结构,ture表示只迁移dll表结构。if supress_data is true, only the schema definition will be exported/migrated, and not the data
supress_data: false# 指定是否只迁移数据,true表示只迁移数据。if supress_ddl is true, only the data will be exported/imported, and not the schema
supress_ddl: false# 表存在是否清空导入,true表示清空再导入。if force_truncate is true, forces a table truncate before table loading
force_truncate: false# if timezone is true, forces to append/convert to UTC tzinfo mysql data
timezone: false# if index_prefix is given, indexes will be created whith a name prefixed with index_prefix
index_prefix:

4.3 执行数据迁移

执行导入数据后,自动执行过程分3个阶段:

  • 自动在PostgreSQL创建表结构;
  • 自动加载数据(约1w rows/sec);
  • 自动在PostgreSQL创建索引。
[root@123]# py-mysql2pgsql -v -f mysql2pgsql.yml 
>>>>>>>>>>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值