2013-04-24 168 views
9

您好,我想將Postgres數據庫中的一些錶轉換爲MySQL。理想情況下,我想創建一個將PG錶轉換爲MySQL的腳本,但是如果我們能夠弄清楚如何以非腳本化的方式實現它,我很高興。如何將數據庫從Postgres遷移到MySQL?

首先,我在這裏讀了類似的帖子:Migrate database from Postgres to MySQL

我已經試過這兩種解決方案提出了那裏,但他們我以前不使的伎倆。

重要的是要說我想遷移結構和數據。第一

但第一件事情,這是Postgres的數據庫的SQL轉儲:

-- 
-- PostgreSQL database dump 
-- 

SET statement_timeout = 0; SET client_encoding = 'UTF8'; SET standard_conforming_strings = off; SET check_function_bodies = false; SET client_min_messages = warning; SET escape_string_warning = off; 

SET search_path = public, pg_catalog; 

SET default_tablespace = ''; 

SET default_with_oids = false; 

-- 
-- Name: grupos; Type: TABLE; Schema: public; Owner: postgres; Tablespace: 
-- 

CREATE TABLE grupos (
    "dirGrupo" text, 
    valor real, 
    "flagIntelli" boolean DEFAULT false, 
    "flagQDS" boolean DEFAULT false, 
    finalidad text, 
    "idGrupo" integer DEFAULT nextval('"idGrupo"'::regclass) NOT NULL, 
    "claseDomo" text, 
    instalado boolean DEFAULT true NOT NULL, 
    "codCasa" "char", 
    "codUnidad" integer, 
    protocolo text NOT NULL, 
    escritura boolean, 
    dispositivo text, 
    "tipoDatos" oid, 
    "coordX" integer DEFAULT (-1), 
    "coordY" integer DEFAULT (-1), 
    mapa text, 
    "reglasAsociadas" text); 


ALTER TABLE public.grupos OWNER TO postgres; 

-- 
-- Data for Name: grupos; Type: TABLE DATA; Schema: public; Owner: postgres 
-- 

INSERT INTO grupos VALUES (NULL, 0, false, false, 'sensor', 10, 'LightSensor', true, 'E', 1, 'x10', false, 'SensorLum1', 7, -1, -1, NULL, NULL); INSERT INTO grupos VALUES (NULL, 0, false, false, 'luz', 11, 'SimpleLamp', true, 'K', 1, 'x10', true, 'Luz4', 1, -1, -1, NULL, NULL); INSERT INTO grupos VALUES (NULL, 0, false, false, 'Radiador de la sala', 298, 'Heater', true, 'B', 1, 'x10', true, 'RadiadorX10', 1, 163, 168, 'Sala Quercus', '108;111;115;117;119;123;127;131;134;136;138;140;144;148;150;152;155;157;159;162;166;169;172;176;179;182;185;188;190;193;196;199;201;205;209;214;218;222;226;230;234;238;250;254;260;264;277;278;279;280;284;293;297;301;305;309;313;315;315;315;315;315;315;315;315;315;315;315;315;315;315;315;315;'); INSERT INTO grupos VALUES ('0/0/5', 1, false, false, 'Interruptor', 213, 'Button', true, NULL, NULL, 'knx', true, 'Interruptor', 1, 301, 326, 'Plano Casa', NULL); INSERT INTO grupos VALUES ('0/0/4', 0, false, false, 'Enchufe4', 316, 'Switch', true, NULL, NULL, 'knx', true, 'Enchufe4', 1, 303, 133, 'Plano Casa', '268;272;276;'); INSERT INTO grupos VALUES ('0/0/2', 0, false, false, 'Enchufe2', 210, 'Switch', true, NULL, NULL, 'knx', true, 'Enchufe2', 1, 141, 322, 'Plano Casa', NULL); INSERT INTO grupos VALUES ('0/0/1', 0, false, false, 'LuzFlexo', 217, 'SimpleLamp', true, NULL, NULL, 'knx', true, 'LuzFlexo', 1, 80, 45, 'Plano Casa', '267;271;275;'); INSERT INTO grupos VALUES ('0/0/3', 1, false, false, 'Router', 221, 'Router', true, NULL, NULL, 'knx', true, 'Router1', 1, 467, 439, 'Plano Casa', NULL); INSERT INTO grupos VALUES ('0/0/6', 21.68, false, false, 'SensorTemperatura', 237, 'TemperatureSensor', true, NULL, NULL, 'knx', false, 'SensorTemperatura', 2, 146, 436, 'Plano Casa', NULL); INSERT INTO grupos VALUES (NULL, 0, false, false, 'SensorX10', 219, 'MotionSensor', true, 'A', 1, 'x10', false, 'SensorX10', 6, 362, 11, 'Plano Casa', '335;336;'); INSERT INTO grupos VALUES ('1/1/5', 1, false, false, '', 12, 'MotionSensor', true, NULL, NULL, 'knx', false, 'SensorPresencia', 6, -1, -1, NULL, NULL); INSERT INTO grupos VALUES ('1/1/2', 50, false, true, 'Luz Intervalo', 248, 'DimmableLight', true, NULL, NULL, 'knx', true, 'LuzHInt', 3, 97, 276, 'Plano Casa', '265;269;273;338;'); INSERT INTO grupos VALUES ('1/1/1', 0, false, false, 'LuzHBin', 215, 'SimpleLamp', true, NULL, NULL, 'knx', true, 'LuzHBin', 1, 357, 189, 'Plano Casa', '266;270;274;'); INSERT INTO grupos VALUES (NULL, 1, false, true, 'Encender la luz del flexo', 291, 'SimpleLamp', true, 'C', 1, 'x10', true, 'BombillaX10', 1, 272, 130, 'Sala Quercus', '107;109;110;112;113;114;116;118;120;121;122;124;125;126;128;129;130;132;133;135;137;139;141;142;143;145;146;147;149;151;153;154;156;158;160;161;163;164;165;167;168;170;171;173;174;175;177;178;180;181;183;184;186;187;189;191;192;194;195;197;198;200;202;203;204;206;207;208;210;211;212;213;215;216;217;219;220;221;223;224;225;227;228;229;231;232;233;235;236;237;239;240;249;251;252;253;255;256;257;258;259;261;262;263;281;282;283;290;291;292;294;295;296;298;299;300;302;303;304;306;307;308;310;311;312;314;316;317;314;316;317;314;316;317;314;316;317;314;316;317;314;316;317;314;316;317;314;316;317;314;316;317;314;316;317;314;316;317;314;316;317;314;316;317;314;316;317;314;316;317;314;316;317;335;336;'); 


-- 
-- Name: grupos_pkey; Type: CONSTRAINT; Schema: public; Owner: postgres; Tablespace: 
-- 

ALTER TABLE ONLY grupos 
    ADD CONSTRAINT grupos_pkey PRIMARY KEY ("idGrupo"); 


-- 
-- Name: grupos_tipoDatos_fkey; Type: FK CONSTRAINT; Schema: public; Owner: postgres 
-- 

ALTER TABLE ONLY grupos 
    ADD CONSTRAINT "grupos_tipoDatos_fkey" FOREIGN KEY ("tipoDatos") REFERENCES "TiposDatos"(id); 


-- 
-- PostgreSQL database dump complete 
-- 

1)第一種方法,使用pG2Mysqlhttp://www.lightbox.ca/pg2mysql.php 簡單和快速(理論上):不幸的是,

這裏是檢索到的文檔:

# Converted with pg2mysql-1.9 
# Converted on Mon, 22 Apr 2013 14:12:28 -0400 
# Lightbox Technologies Inc. http://www.lightbox.ca 

SET SQL_MODE="NO_AUTO_VALUE_ON_ZERO"; SET time_zone="+00:00"; 

CREATE TABLE grupos (
    `dirGrupo` text, 
    valor real, 
    `flagIntelli` bool DEFAULT 0, 
    `flagQDS` bool DEFAULT 0, 
    finalidad text, 
    `idGrupo` int(11) auto_increment NOT NULL, 
    `claseDomo` text, 
    instalado bool DEFAULT 1 NOT NULL, 
    `codCasa` `char`, 
    `codUnidad` int(11), 
    protocolo text NOT NULL, 
    escritura bool, 
    dispositivo text, 
    `tipoDatos` oid, 
    `coordX` int(11) DEFAULT -1 , 
    `coordY` int(11) DEFAULT -1 , 
    mapa text, 
    `reglasAsociadas` text , PRIMARY KEY(`idGrupo`)) TYPE=MyISAM; 

INSERT INTO grupos VALUES (NULL, 0, false, false, 'sensor', 10, 'LightSensor', true, 'E', 1, 'x10', false, 'SensorLum1', 7, -1, -1, NULL, NULL); INSERT INTO grupos VALUES (NULL, 0, false, false, 'luz', 11, 'SimpleLamp', true, 'K', 1, 'x10', true, 'Luz4', 1, -1, -1, NULL, NULL); INSERT INTO grupos VALUES (NULL, 0, false, false, 'Radiador de la sala', 298, 'Heater', true, 'B', 1, 'x10', true, 'RadiadorX10', 1, 163, 168, 'Sala Quercus', '108;111;115;117;119;123;127;131;134;136;138;140;144;148;150;152;155;157;159;162;166;169;172;176;179;182;185;188;190;193;196;199;201;205;209;214;218;222;226;230;234;238;250;254;260;264;277;278;279;280;284;293;297;301;305;309;313;315;315;315;315;315;315;315;315;315;315;315;315;315;315;315;315;'); INSERT INTO grupos VALUES ('0/0/5', 1, false, false, 'Interruptor', 213, 'Button', true, NULL, NULL, 'knx', true, 'Interruptor', 1, 301, 326, 'Plano Casa', NULL); INSERT INTO grupos VALUES ('0/0/4', 0, false, false, 'Enchufe4', 316, 'Switch', true, NULL, NULL, 'knx', true, 'Enchufe4', 1, 303, 133, 'Plano Casa', '268;272;276;'); INSERT INTO grupos VALUES ('0/0/2', 0, false, false, 'Enchufe2', 210, 'Switch', true, NULL, NULL, 'knx', true, 'Enchufe2', 1, 141, 322, 'Plano Casa', NULL); INSERT INTO grupos VALUES ('0/0/1', 0, false, false, 'LuzFlexo', 217, 'SimpleLamp', true, NULL, NULL, 'knx', true, 'LuzFlexo', 1, 80, 45, 'Plano Casa', '267;271;275;'); INSERT INTO grupos VALUES ('0/0/3', 1, false, false, 'Router', 221, 'Router', true, NULL, NULL, 'knx', true, 'Router1', 1, 467, 439, 'Plano Casa', NULL); INSERT INTO grupos VALUES ('0/0/6', 21.68, false, false, 'SensorTemperatura', 237, 'TemperatureSensor', true, NULL, NULL, 'knx', false, 'SensorTemperatura', 2, 146, 436, 'Plano Casa', NULL); INSERT INTO grupos VALUES (NULL, 0, false, false, 'SensorX10', 219, 'MotionSensor', true, 'A', 1, 'x10', false, 'SensorX10', 6, 362, 11, 'Plano Casa', '335;336;'); INSERT INTO grupos VALUES ('1/1/5', 1, false, false, '', 12, 'MotionSensor', true, NULL, NULL, 'knx', false, 'SensorPresencia', 6, -1, -1, NULL, NULL); INSERT INTO grupos VALUES ('1/1/2', 50, false, true, 'Luz Intervalo', 248, 'DimmableLight', true, NULL, NULL, 'knx', true, 'LuzHInt', 3, 97, 276, 'Plano Casa', '265;269;273;338;'); INSERT INTO grupos VALUES ('1/1/1', 0, false, false, 'LuzHBin', 215, 'SimpleLamp', true, NULL, NULL, 'knx', true, 'LuzHBin', 1, 357, 189, 'Plano Casa', '266;270;274;'); INSERT INTO grupos VALUES (NULL, 1, false, true, 'Encender la luz del flexo', 291, 'SimpleLamp', true, 'C', 1, 'x10', true, 'BombillaX10', 1, 272, 130, 'Sala Quercus', '107;109;110;112;113;114;116;118;120;121;122;124;125;126;128;129;130;132;133;135;137;139;141;142;143;145;146;147;149;151;153;154;156;158;160;161;163;164;165;167;168;170;171;173;174;175;177;178;180;181;183;184;186;187;189;191;192;194;195;197;198;200;202;203;204;206;207;208;210;211;212;213;215;216;217;219;220;221;223;224;225;227;228;229;231;232;233;235;236;237;239;240;249;251;252;253;255;256;257;258;259;261;262;263;281;282;283;290;291;292;294;295;296;298;299;300;302;303;304;306;307;308;310;311;312;314;316;317;314;316;317;314;316;317;314;316;317;314;316;317;314;316;317;314;316;317;314;316;317;314;316;317;314;316;317;314;316;317;314;316;317;314;316;317;314;316;317;314;316;317;314;316;317;335;336;'); ALTER TABLE grupos 
    ADD CONSTRAINT grupos_pkey PRIMARY KEY ("idGrupo"); 
在解析時有幾個錯誤

當我使用任何數據庫管理器導入此文件到數據庫中,我收到什麼,但錯誤:

The first error 出於某種原因,解析器標記字符這些符號之間:「」。 如果我們仔細看看,有時候列的名字是在''和其他時間之間,爲什麼?

我刪除周圍焦炭這些符號,並繼續做同樣的進口,但再次另一個錯誤朝上:

The second error

類型OID?那是什麼?閱讀我看到這是一個用於主鍵的類型...在這一點上,我決定:

fuck this shit! 我已經夠了!

2)第二種方法,我會嘗試與Mysql Workbench

我安裝在Ubuntu這個功能,我給它一個嘗試。有一個選項可以遷移數據庫,太棒了!

1 .-我介紹了源和測試連接: The source connection test

2:然後將目的地和測試連接:(!嘿,這sh-- ..thing可能工作:)) The destination connection test

3.-下一個畫面,一切都很好: Things are working

4。 - 現在我要求選擇模式,我選擇默認選項: Select the schema

5.-我收到以下錯誤,在指定的點失敗,「反向工程選擇的圖式」。

Starting... 
Connect to source DBMS... 
- Connecting... 
Connecting to [email protected]=Postgresql;SERVER=158.49.245.68;PORT=5432... 
Opening ODBC connection to DRIVER=Postgresql;SERVER=158.49.245.68;PORT=5432;DATABASE=QDSDatabase;UID=postgres... 
Connected 
Connect to source DBMS done 
Reverse engineer selected schemata.... 
Reverse engineering public from QDSDatabase 
- Reverse engineering catalog information 
Traceback (most recent call last): 
    File "/usr/lib/mysql-workbench/modules/db_postgresql_re_grt.py", line 335, in reverseEngineer 
    return PostgresqlReverseEngineering.reverseEngineer(connection, catalog_name, schemata_list, context) 
    File "/usr/lib/mysql-workbench/modules/db_generic_re_grt.py", line 207, in reverseEngineer 
    catalog = cls.reverseEngineerCatalog(connection, catalog_name) 
    File "/usr/lib/mysql-workbench/modules/db_generic_re_grt.py", line 367, in reverseEngineerCatalog 
    cls.reverseEngineerSequences(connection, schema) 
    File "/usr/lib/mysql-workbench/modules/db_postgresql_re_grt.py", line 76, in reverseEngineerSequences 
    min_value, max_value, start_value, increment_by, last_value, is_cycled, ncache = cls.execute_query(connection, seq_details_query % (schema.name, seq_name)).fetchone() 
    File "/usr/lib/mysql-workbench/modules/db_generic_re_grt.py", line 56, in execute_query 
    return cls.get_connection(connection_object).cursor().execute(query, *args, **kwargs) 
pyodbc.ProgrammingError: ('42P01', '[42P01] ERROR: no existe la relaci\xc3\xb3n \xc2\xabpublic.idtipodatos\xc2\xbb;\nError while executing the query (7) (SQLExecDirectW)') 

Traceback (most recent call last): 
    File "/usr/share/mysql-workbench/libraries/workbench/wizard_progress_page_widget.py", line 192, in thread_work 
    self.func() 
    File "/usr/lib/mysql-workbench/modules/migration_schema_selection.py", line 160, in task_reveng 
    self.main.plan.migrationSource.reverseEngineer() 
    File "/usr/lib/mysql-workbench/modules/migration.py", line 332, in reverseEngineer 
    self.state.sourceCatalog = self._rev_eng_module.reverseEngineer(self.connection, self.selectedCatalogName, self.selectedSchemataNames, self.state.applicationData) 
SystemError: ProgrammingError("('42P01', '[42P01] ERROR: no existe la relaci\xc3\xb3n \xc2\xabpublic.idtipodatos\xc2\xbb;\nError while executing the query (7) (SQLExecDirectW)')"): error calling Python module function DbPostgresqlRE.reverseEngineer 
ERROR: Reverse engineer selected schemata: ProgrammingError("('42P01', '[42P01] ERROR: no existe la relaci\xc3\xb3n \xc2\xabpublic.idtipodatos\xc2\xbb;\nError while executing the query (7) (SQLExecDirectW)')"): error calling Python module function DbPostgresqlRE.reverseEngineer 
Failed 

6.-如果我嘗試用其他類型的架構(POINT 4.-)程序突然關閉的: Exit(-1)

這一切,我已經在幾個方面與MySQL工作臺,試過這裏面看起來很棒,但不幸的是沒有工作。

任何想法?請記住,postgres SQL是在文章的開頭,所以任何人都可以嘗試遷移。我將非常感謝所收到的任何幫助。

JesúsRedondo。

+4

嗯,這是一個很好的故事,但解決方案已經很完善:做模式轉儲,然後每個錶轉儲到CSV。手轉換架構並將其加載到MySQL中,然後將數據導入到每個表中。如果你不喜歡這種方法,你可以使用Pentaho Kettle或Talend Studio等ETL工具來進行數據遷移。模式轉換工具也存在,但通常花費$。一件事**絕對保證不工作**只是轉儲數據庫並嘗試將其加載到MySQL中。 – 2013-04-24 11:26:14

+1

是的,我只是想避免手工轉換模式。正如Richar Huxton評論說的,我真的不明白PostgreSQL和MySQL足夠深入,無法做到這一點。但老實說,我認爲我沒有其他機會,只能嘗試一下。 – Jesus 2013-04-24 15:41:29

+0

由於此操作已關閉,因此無法回答,但應先學習SQL。如果您不想嘗試,請嘗試其他自動工具之一:https://wiki.postgresql.org/wiki/Converting_from_other_Databases_to_PostgreSQL#MySQL或嘗試[Pentaho Kettle](http://community.pentaho.com/projects/數據集成/)。 – Chloe 2015-09-22 21:37:28

回答

2

這對你來說恐怕會很困難。

  1. 你似乎並不理解PostgreSQL的
  2. 你似乎並不瞭解MySQL的
  3. 你似乎並不理解你的數據庫架構

如果這是你的整個架構,只需將其複製到您選擇的文本編輯器中並手動更新即可。

什麼「oid」列用於我不能說,但它與主鍵無關(可能是一個大對象或「blob」?)。

引用僅用於已使用混合大小寫標識LikeThis的情況。默認情況下,SQL不區分大小寫。

Mysql沒有真正的「布爾」類型,您可能會想要使用bittinyint

模式中的nextval()調用是爲了生成唯一的id。這是一個PostgreSQL的東西,在MySQL中不支持 - 你可能需要一個auto_increment修飾符。

這些提示和pg2mysql輸出之間應該讓你開始。

+0

謝謝,我將嘗試解析Schema並將數據從一個數據庫傳遞到另一個數據庫以CSV格式進行編寫。 我不知道爲什麼在那裏使用oid。我沒有設計數據庫模式,但恐怕設計師們都不知道:) 感謝您的報價和布爾提示! – Jesus 2013-04-24 16:02:56

+1

@Jesus「oid」是舊的('WITH OIDS')PostgreSQL表中的內部列。它仍然用在系統目錄中,但通常沒有其他用處。將其用於任何事情的可能性不大,在進行數據遷移時通常可以省略。 「oid」是「對象標識符」。查看PostgreSQL文檔瞭解所有細節。 – 2013-04-24 22:38:12