MySQL 不落地遷移、導入 PostgreSQL - 推薦 rds_dbsync

標簽

PostgreSQL , MySQL , rds_dbsync , mysql , mysqldump , copy , mysql_fdw


背景

將MySQL數據不落地的方式導入PostgreSQL。

1 rds_dbsync (推薦使用)

《MySQL準實時同步到PostgreSQL, Greenplum的方案之一 - rds_dbsync》

這個效率最高,支持不落地,支持流式導入,支持單表并發(通過配置文件,寫WHERE條件,拆成多個并發導同一張表)。

用法

以CentOS 7.x x64為例。

mysql2pgsql已打包所有依賴包,可以不安裝pgsql和mysql。不過你如果想連接數據庫做一些管理工作、或者排錯等,還是有必要安裝一下。

1、pgsql

《PostgreSQL on Linux 最佳部署手冊 - 珍藏級》

《PostgreSQL 10 on ECS 實施 流復制備庫鏡像+自動快照備份+自動備份驗證+自動清理備份與歸檔 - 珍藏級》

《PostgreSQL 10 + PostGIS + Sharding(pg_pathman) + MySQL(fdw外部表) on ECS 部署指南(適合新用戶) - 珍藏級》

su - digoal  
vi .bash_profile  export PS1="$USER@`/bin/hostname -s`-> "  
export PGPORT=1921  
export PGDATA=/data01/pg/pg_root$PGPORT  
export LANG=en_US.utf8  
export PGHOME=/home/digoal/pgsql11  
export LD_LIBRARY_PATH=$PGHOME/lib:/lib64:/usr/lib64:/usr/local/lib64:/lib:/usr/lib:/usr/local/lib:$LD_LIBRARY_PATH  
export PATH=$PGHOME/bin:$PATH:.  
export DATE=`date +"%Y%m%d%H%M"`  
export MANPATH=$PGHOME/share/man:$MANPATH  
export PGHOST=$PGDATA  
export PGUSER=postgres  
export PGDATABASE=postgres  
alias rm='rm -i'  
alias ll='ls -lh'  
unalias vi  

2、mysql

https://dev.mysql.com/downloads/repo/yum/

https://dev.mysql.com/doc/mysql-yum-repo-quick-guide/en/

su - root  
vi /etc/yum.repos.d/mysql.repo  [mysql57-community]  
name=MySQL 5.7 Community Server  
baseurl=http://repo.mysql.com/yum/mysql-5.7-community/el/7/$basearch/  
enabled=1  
gpgcheck=0  
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-mysql  
yum install -y mysql-community-server.x86_64 mysql-community-devel.x86_64  

3、rds_dbsync

詳細配置文檔參考

./mysql2pgsql --help  
ignore copy error count 0 each table  
Unsupported option: -Usage: -l <table list file> -j <thread number> -d -n -f -s -b -h  -l specifies a file with table listed;  -j specifies number of threads to do the job;  -d means get DDL only without fetching data;  -n means no partion info in DDLs;  -f means taking first column as distribution key;  -s specifies the target schema;  -b specifies the buffer size in KB used to sending copy data to target db, the default is 0  

https://github.com/aliyun/rds_dbsync/blob/master/doc/mysql2pgsql_ch.md

https://github.com/aliyun/rds_dbsync/releases

wget https://github.com/aliyun/rds_dbsync/files/1555186/mysql2pgsql.bin.el7.20171213.zip  unzip mysql2pgsql.bin.el7.20171213.zip  

4、rds_dbsync 將mysql遷移到pgsql

確保執行mysql2pgsql的機器,可以同時連接到mysql, pgsql.

如果無法直接互聯,可以使用SSH打通隧道進行互相訪問。

《使用 ssh -R 建立反向/遠程TCP端口轉發代理》

1、配置my.cfg文件,源(mysql)、目標(pgsql)

cd mysql2pgsql.bin.el7.20171213  cd bin  vi my.cfg  [src.mysql]  
host = "數據庫IP"  
port = "數據庫PORT"  
user = "數據庫user"  
password = "數據庫user密碼"  
db = "數據庫名"  
encodingdir = "share"  
encoding = "utf8"  [desc.pgsql]  
connect_string = "host=127.0.0.1 port=1921 dbname=postgres user=postgres password=pgsql"  
target_schema = "public"  

如果postgresql在本地,可以使用unix socket連接,導入性能比tcp要快一點。例如

connect_string = "host=/tmp dbname=postgres port=1921 user=postgres password=pgsql"  

unix socket dir配置可從配置文件讀取

postgres=# show unix_socket_directories ;  unix_socket_directories   
-------------------------  /tmp,.  
(1 row)  

2、生成mysql 轉換為pgsql 的建表 DDL

./mysql2pgsql -d > ddl.sql  

3、執行輸出的DDL文件

在pgsql對應的數據庫中,執行第二步生成的DDL語句,創建目標表。

psql -f ./ddl.sql -1   

如果有問題,需要手工修復一下。

遷移例子

1、全量遷移

cd mysql2pgsql.bin.el7.20171213/bin  nohup ./mysql2pgsql >./load.log 2>&1 &  

2、選擇性遷移

如果不想遷移所有表的數據,或者某些表只想遷移部分數據,可以寫配置文件。

2.1、甚至可以多個源寫入單個表,例如多個MYSQL節點數據,匯入單個PG節點。

2.2、如果源表與PG的目標表名字不一樣,可以在配置文件中映射表名。(冒號分隔:第一列為mysql里面的表名,第二列為MYSQL里面的表名,或者QUERY)

vi lo.txt  tbl1  
tbl2 : select * from tbl_from_mysql where id<10000;  
tbl2 : select * from tbl_from_mysql where id >= 100000 and id< 10000000;  
tbl3 : tbl_from_mysql_1  
tbl3 : tbl_from_mysql_2  

然后執行

cd mysql2pgsql.bin.el7.20171213/bin  nohup ./mysql2pgsql -l ./lo.txt >./load.log 2>&1 &  

3、并行遷移

默認為5個遷移線程操作(每個線程COPY一張表),通過-j參數指定。

cd mysql2pgsql.bin.el7.20171213/bin  nohup ./mysql2pgsql -l ./lo.txt -j 8 >./load.log 2>&1 &  

4、單表如何支持并行遷移

單表,通過where條件分段,可以實現單表的并行遷移(但是幾個SQL分開執行,他們的SNAPSHOT不一樣,不滿足全局一致性)

vi lo.txt  tbl2 : select * from tbl_from_mysql where id < 1000000;  
tbl2 : select * from tbl_from_mysql where id >= 1000000 and id < 2000000;  
tbl2 : select * from tbl_from_mysql where id >= 2000000 and id < 3000000;  
tbl2 : select * from tbl_from_mysql where id >= 3000000;  
cd mysql2pgsql.bin.el7.20171213/bin  nohup ./mysql2pgsql -l ./lo.txt -j 4 >./load.log 2>&1 &  

2 mysql_fdw

《PostgreSQL 10 + PostGIS + Sharding(pg_pathman) + MySQL(fdw外部表) on ECS 部署指南(適合新用戶) - 珍藏級》

https://github.com/EnterpriseDB/mysql_fdw

http://blog.163.com/digoal@126/blog/static/1638770402011111233524987/

http://blog.163.com/digoal@126/blog/static/163877040201493145214445/

3 mysql

通過管道導入

export PGHOST=  
export PGPORT=  
export PGDATABASE=  
export PGUSER=  
export PGPASSWORD=  nohup mysql -C -h主機 -P端口 -u用戶 -p密碼 庫 -B -e "select * from 表" | psql -c "copy 表 from stdin with  (format csv, HEADER true, null 'NULL', DELIMITER E'\t')" > /dev/null 2>&1 &    

如果表很大,可能OOM,因為需要將數據完全HOLD到mysql客戶端后,才開始輸出。暫不清楚mysql客戶端有沒有流式輸出的功能。

4 mysqldump

通過管道導入

export PGHOST=  
export PGPORT=  
export PGDATABASE=  
export PGUSER=  
export PGPASSWORD=  mysqldump 庫名 -t -h主機 -P端口 -u用戶 -p密碼 --no-create-db --skip-quote-names --skip-add-locks --skip-lock-tables --skip-tz-utc -y --default-character-set=UTF8 -C --compact --compatible=postgresql --tables 表 | psql -f - >/dev/null 2>&1 &    

如果在mysql服務器上運行,可以dump CSV格式。

MySQL沒有像PostgreSQL這樣的COPY to stdout或COPY from stdin這樣的COPY協議,只有服務端COPY。

mysqldump有一些格式問題(即使使用--compatible=postgresql),可能導致數據導入到PG時出錯。

參考

man mysql

man mysqldump

https://github.com/aliyun/rds_dbsync

https://github.com/EnterpriseDB/mysql_fdw

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/275369.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/275369.shtml
英文地址,請注明出處:http://en.pswp.cn/news/275369.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

asp.net 六大對象之Request、Response

ASP.NET的六大對象&#xff0c;本質上只是 Context 里面的屬性&#xff0c;嚴格上不是對象。 1.Request-->讀取客戶端在Web請求期間發送的值 2.Response-->封裝了頁面執行期后返回到Http客戶端的輸出 3.Server-->提供對服務器上的屬性和方法的訪問 4.Application-->…

對微型計算機工作影響最小的因數是,(已)保護試題9

繼電保護試題9一、填空題(每空1分&#xff0c;共10分)1、交流電的三要素是最大值(幅值)、角頻率、初相位。2、三極管實現放大作用的外部條件是發射結正向偏置&#xff0c;集電結反向偏置。3、為了確保方向過電流保護在反向兩相短路時不受非故障相電流的影響&#xff0c;保護裝置…

1年工作經驗8月份大廠面試全記錄

大家好&#xff0c;我是若川。最近組織了源碼共讀活動《1個月&#xff0c;200人&#xff0c;一起讀了4周源碼》&#xff0c;已經有超50人提交了筆記&#xff0c;群里已經有超1200人&#xff0c;感興趣的可以點此鏈接掃碼加我微信 ruochuan12本文來自讀者AaronKwong 投稿。他參與…

axure ui設計_了解針對UI / UX設計人員的Axure RP 9

axure ui設計Axure is a powerful prototyping software with a lot of history. It has been around for many years and is available for Windows and Mac. Axure gained a lot of functionality over time, and today you can look at Axure as all in one tool. You can b…

如何不讓FCKEditor自動添加P標簽

使用FCKeditor編輯器的時候&#xff0c;點開里面就會自動添加上<P></P>的標簽&#xff0c;按回車也會自動添加上相同的標簽修改/include/FCKeditor/fckconfig.js找到FCKConfig.EnterMode p; //p | div | brFCKConfig.ShiftEnterMode br; //p | div | br替換成FCKCo…

Greenplum 優化CASE - 對齊JOIN字段類型,使用數組代替字符串,降低字符串處理開銷,列存降低掃描開銷...

標簽 PostgreSQL , 數組 , 字符串 , 字符串處理 , JOIN , where , 類型一致性 背景 Greenplum通常被用作OLAP&#xff0c;在一些用戶使用過程中&#xff0c;可能因為數據結構設計&#xff0c;SQL問題等原因導致性能不佳&#xff0c;雖然通過增加節點可以解決問題&#xff0c;但…

杭州 3~5年 前端面經,高頻面試題總結

大家好&#xff0c;我是若川。假期歸來&#xff0c;國慶期間沒有更文&#xff0c;不用想每天發什么文章&#xff0c;不用擔心閱讀量&#xff0c;其實感覺挺好。最近組織了源碼共讀活動《1個月&#xff0c;200人&#xff0c;一起讀了4周源碼》&#xff0c;已經有超100人提交了筆…

職稱以考代評學院考計算機嗎,軟考與職稱的關系,軟考是以考代評,不用另外再去評審...

Hokfung(cnitpm.com) 15:16:45軟考與職稱關系&#xff1a;通過了軟考,我們所獲得的只是一種資格,是否聘任相應的職稱,完全取決于各單位的實際情況&#xff0c;國家有關部門并沒有直接的規定。事實上,通過評審方法(也就是常說的"評職稱”) 得到的也只是一個資格,單位既可以…

figma下載_我關于Figma文件封面的故事

figma下載It was 8:40 AM in the morning. I woke up from the bed as my subconscious memory reminded me of the team meeting at 9 AM to discuss what I am working on.早上8:40。 我從床上醒來&#xff0c;因為我的潛意識使我想起了上午9點的團隊會議&#xff0c;討論我的…

圖解選擇排序與插入排序

上一篇詳述了冒泡排序及其優化&#xff0c;有興趣的可以看看&#xff1a; 如何優化冒泡排序&#xff1f; 一、選擇排序&#xff08;SelectionSort&#xff09; 算法思想&#xff1a;首先在未排序序列中找到最小&#xff08;大&#xff09;元素&#xff0c;存放到排序序列的起始…

2011年上半年網頁游戲開測數據報告發布

網頁游戲上半年統計數據顯示&#xff0c;2011年上半年&#xff0c;網頁游戲開測信息總數為304款&#xff0c;排除重復開測信息&#xff0c;在2011年1月1日至6月30日這段期間&#xff0c;共收錄開測&#xff08;含首次開測或更名的&#xff09;的數據為129條。 新公布的產品&…

計算機python程序設計導論,程序設計導論:Python計算與應用開發實踐(原書第2版)...

程序設計導論&#xff1a;Python計算與應用開發實踐(原書第2版)語音編輯鎖定討論上傳視頻《程序設計導論&#xff1a;Python計算與應用開發實踐(原書第2版)》是2018年機械工業出版社出版的圖書&#xff0c;作者是[美] 盧博米爾佩爾科維奇(Ljubomir Perkovic)。書 名程序設計…

vue-cli 將被 create-vue 替代?初始化基于 vite 的 vue3 項目為何如此簡單?

大家好&#xff0c;我是若川。最近組織了源碼共讀活動《1個月&#xff0c;200人&#xff0c;一起讀了4周源碼》&#xff0c;已經有超50人提交了筆記&#xff0c;群里已經有超1500人&#xff0c;感興趣的可以點此鏈接掃碼加我微信 ruochuan12create-vue公開了&#xff0c;可以使…

lynda ux_如何進入UX領域

lynda uxI often get asked “What is the right path I should take to get into UX?” and more often than not, I do not have a direct answer. I usually ask a lot of questions about their background, before assessing their current skills with the things they …

php字符串學習筆記

在這里記錄下今天的所得首先對字符串處理進行分類今天主要記錄有以下字符串的格式化字符串的連接與分割字符串的比較使用字符串函數匹配和替換子字符串使用正則表達式1.字符串的格式化<?php //整理字符串的第一步是清理字符串中的多余的空格 // trim() ltrim() rtrim() // …

This is a Blog Test

Blog Test Hello, everyone! I am going to write blog to record the knowledge about the computer technology involved when I study. Please feel free to comment on any mistakes. Thank you! print("Hello")轉載于:https://blog.51cto.com/12370958/2379111

可以測試體育跑步的軟件,某高校現跑步打卡神器 能檢測出是在走還是跑

[摘要]近日&#xff0c;一批高大上的“陽光跑步神器”在東莞一所高校火了&#xff01;之所以稱之“神器”&#xff0c;是由于這批機器能檢測到你在走路還是在跑步&#xff0c;如果走路數據將中斷。消息一出&#xff0c;學生們有贊成&#xff0c;也有大呼“吃不消”。東莞某高校…

一道很熟悉的前端面試題,你怎么答?

大家好&#xff0c;我是若川。最近這幾年&#xff0c;云計算的普及和 HTML5 技術的快速發展&#xff0c;越來越多的應用轉向了瀏覽器 / 服務器&#xff08;B/S&#xff09;架構&#xff0c;這種改變讓瀏覽器的重要性與日俱增&#xff0c;視頻、音頻、游戲幾大核心場景也都在逐漸…

:尋找指定和的整數對_尋找時間:如何增加設計的時間

:尋找指定和的整數對Good design derives from good thinking. And good thinking is highly correlated to how much time you spend. In every place I’ve been though, every designer seems to be thirsty for more time to design. Why does this happen, to a point whe…

JavaScript命名空間namespace的實現方法

網上有很多了&#xff0c;這里給出一個&#xff0c;其實思路就是A{}; A.b{};其實b是A的一個屬性。只是做了一些封裝&#xff0c;最后的效果是可以直接定義多個namespace&#xff1a; 1: My.namespace("Company", "Company.Feed", "Company.Feed.Mess…