1. 程式人生 > >使用py-mysql2pgsql 簡單實現mysql數據庫遷移到pgsql

使用py-mysql2pgsql 簡單實現mysql數據庫遷移到pgsql

使用py-mysql2pgsql 簡單實現mysql數據庫遷移到pgsql

參考:https://pypi.python.org/pypi/py-mysql2pgsql


公司的有個項目,原先用的是MySQL數據庫,現在要改成postgres。 於是搜了下,找到個py-mysql2pgsql工具。下面是筆記:


假設我們要把本機的mysql裏面的gitlab_ci_production、gitlabhq_production 這2個庫導入到本機的pgsql中(本地地址:192.168.2.100)


1、安裝pgsql10

yum install https://download.postgresql.org/pub/repos/yum/10/redhat/rhel-6-x86_64/pgdg-centos10-10-1.noarch.rpm


yum install postgresql10 postgresql10-server

yum install postgresql10-devel


service postgresql-10 initdb

chkconfig postgresql-10 on

service postgresql-10 start


修改綁定地址

vim /var/lib/pgsql/10/data/postgresql.conf

listen_addresses = ‘*‘


添加客戶端IP放行(生產環境不建議放行0.0.0.0/0 範圍太大了)

vim pg_hba.conf

host all all 0.0.0.0/0 md5


/etc/init.d/postgresql-10 restart



2、在pgsql裏面創建用戶及相關的庫:

su - postgres

psql

create user gitlab with password ‘123456‘ ;

create database gitlab_ci_production;

create database gitlabhq_production;

alter database gitlab_ci_production owner to gitlab;

alter database gitlabhq_production owner to gitlab;



echo "export PATH=/usr/pgsql-10/bin/:$PATH" >> /etc/profile

source /etc/profile




3、安裝py-mysql2pgsql

pip install py-mysql2pgsql


4、編寫導出文件的yml,如下:

[root@localhost ~]# cat convert.yml 內容如下:

# if a socket is specified we will use that

# if tcp is chosen you can use compression

mysql:

hostname: localhost

port: 3306

socket: /tmp/mysql.sock

username: root

password: 123456

database: gitlab_ci_production

compress: false

destination:

# if file is given, output goes to file, else postgres

file:

postgres:

hostname: 192.168.2.100

port: 5432

username: gitlab

password: 123456

database: gitlab_ci_production


[root@localhost ~]# cat convert2.yml 內容如下:

# if a socket is specified we will use that

# if tcp is chosen you can use compression

mysql:

hostname: localhost

port: 3306

socket: /tmp/mysql.sock

username: root

password: 123456

database: gitlabhq_production

compress: false

destination:

# if file is given, output goes to file, else postgres

file:

postgres:

hostname: 192.168.2.100

port: 5432

username: gitlab

password: 123456

database: gitlabhq_production



5、執行導入數據的命令:

py-mysql2pgsql -v -f convert.yml

py-mysql2pgsql -v -f convert2.yml


6、稍後去pgsql下驗證下數據是否正常


具體參考:https://pypi.python.org/pypi/py-mysql2pgsql

使用py-mysql2pgsql 簡單實現mysql數據庫遷移到pgsql