数据迁移丨借助 pg2mysql 从 PostgreSQL 到 GreatSQL
数据迁移丨借助 pg2mysql 从 PostgreSQL 到 GreatSQL上篇《数据迁移丨借助 AI 从 PostgreSQL 到 GreatSQL》介绍了如何使用 AI + pg_dump/COPY 的方式将 PostgreSQL 迁移到 GreatSQL 中,各位同砚看过之后,会发现两款数据库还是有一些差异,比方对象层次结构、数据范例等方面,假如接纳人工来迁移,还是会比较麻烦,所以本篇将介绍,如何使用开源工具 pg2mysql 工具从 PostgreSQL 到 GreatSQL。
pg2mysql 简介
pg2mysql 工具是一款开源工具,由 VMware 公司提供,重要用于将数据从 PostgreSQL 迁移至 MySQL 或 GreatSQL。其核心价值在于数据兼容性查抄与迁移功能。在实际迁移使用之前,该工具会仔细查抄 MySQL/GreatSQL 的表结构与 PostgreSQL 中相应表结构是否兼容。若检测到诸如字段长度不足之类的埋伏问题,它会向用户发出提示,以便用户进行修正。
pg2mysql 工具重要是用 Go 语言编写的,遵循 Apache - 2.0 许可证。不外必要注意的是,目前这个项目已经归档,不再进行定期更新了。但对于有能力的朋友而言,可以基于该项目进行进一步的开发:)
[*]https://github.com/vmware-archive/pg2mysql
pg2mysql 安装
方式一:下载程序
[*]https://github.com/vmware-archive/pg2mysql/releases/tag/v0.0.6
以上链接可以直接下载 pg2mysql 最新 v0.0.6 版本,授权后可直接运行:
$ chomd 755 pg2mysql_linux
$ ./pg2mysql_linux -h
2024/10/28 14:52:40 error: Usage:
pg2mysql_linux <migrate | validate | verify>
Application Options:
-c, --config= Path to config file
Help Options:
-h, --help Show this help message
Available commands:
migrate Migrate data from PostgreSQL to MySQL
validateValidate that the data in PostgreSQL can be migrated to MySQL
verify Verify migrated data matches方式二:源码安装
源码安装方式必要有 GO 语言环境:
$ go version
go version go1.22.5 linux/amd64版本没有强要求,go1.22.5 版本可以正常编译运行。
新建一个文件夹,用于存放 pg2mysql 工具源码:
$ mkdir /usr/local/pg2mysql
$ cd /usr/local/pg2mysql拉取 pg2mysql 工具源码:
$ git clone https://github.com/pivotal-cf/pg2mysql.git
$ cd pg2mysql/假如接纳手册中的方法go get github.com/pivotal-cf/pg2mysql/cmd/pg2mysql
会报错:
go: go.mod file not found in current directory or any parent directory.
'go get' is no longer supported outside a module.
To build and install a command, use 'go install' with a version,
like 'go install example.com/cmd@latest'
For more information, see https://golang.org/doc/go-get-install-deprecation
or run 'go help get' or 'go help install'.
是由于目前新版本 Go 语言在模块(module)相关的使用方式上有了变革,直接先拉取源码在使用。
进入 pg2mysql 源码文件夹后,可以看到以下文件:
$ ls
LICENSE config.go migrator_test.go postgres.go vendor
Makefile db.go mysql.go postgresrunner verifier.go
README.mdgo.mod mysqlrunner testdata verifier_test.go
cmd go.sum pg2mysql_suite_test.govalidator.go watcher.go
commands migrator.gopg2mysqlfakes validator_test.go接下来构建项目:
$ go mod init pg2mysql
$ go mod tidy构建完成后,可以编译该工具:
$ go build -o pg2mysql cmd/pg2mysql/main.go在文件夹下就会发现 main 可执行文件,接着授权即可运行:
# 授权
$ chmod 755 pg2mysql
# 授权完成即可运行
$ ./pg2mysql -h
2024/10/30 13:58:20 error: Usage:
pg2mysql <migrate | validate | verify>
Application Options:
-c, --config= Path to config file
Help Options:
-h, --help Show this help message
Available commands:
migrate Migrate data from PostgreSQL to MySQL
validateValidate that the data in PostgreSQL can be migrated to MySQL
verify Verify migrated data matches这里有 error 不要紧,看源码可知 log.Fatalf("error: %s", err) 会带有一个error,并不是无法使用。
pg2mysql 使用
创建配置
首先必要创建配置,创建 PostgreSQL 和 MySQL/GreatSQL 的链接信息:
$ cat > config.yml
页:
[1]