我对mySQL相当新。今天,我练习使用教程(http://www.elated.com/articles/mysql-for-absolute-beginners/)阅读.sql文件。 .sql文件名为books.sql,只包含一个没有记录的表:
USE bookstore;
DROP TABLE IF EXISTS books;
CREATE TABLE books
(
id int unsigned NOT NULL auto_increment, # Unique ID for the record
title varchar(255) NOT NULL, # Full title of the book
author varchar(255) NOT NULL, # The author of the book
price decimal(10,2) NOT NULL, # The price of the book
PRIMARY KEY (id)
);
现在,我正在尝试读取名为db.sql的.sql文件。这包含大约200行标题行和几个表和字段列表。然后,它包含实际记录。它非常大(751,933行)。
我尝试做的是打开mysql,创建数据库,然后读入这个db.sql文件。我希望将db.sql中的所有表(及其记录)放入此数据库中:
mysql -u root
create database myDataBase;
source /path/db.sql
我收到了一长串错误,开头是:
ERROR 1193 (HY000): Unknown system variable 'statement_timeout'
ERROR 1193 (HY000): Unknown system variable 'client_encoding'
ERROR 1193 (HY000): Unknown system variable 'standard_conforming_strings'
ERROR 1193 (HY000): Unknown system variable 'check_function_bodies'
ERROR 1193 (HY000): Unknown system variable 'client_min_messages'
ERROR 1193 (HY000): Unknown system variable 'escape_string_warning'
ERROR 1193 (HY000): Unknown system variable 'search_path'
ERROR 1193 (HY000): Unknown system variable 'default_tablespace'
ERROR 1193 (HY000): Unknown system variable 'default_with_oids'
ERROR 1046 (3D000): No database selected
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'SEQUENCE academic_academic_id_seq
START WITH 1
INCREMENT BY 1
NO MAX' at line 1
然后以反复声明结束:
ERROR 2005 (HY000): Unknown MySQL server host 'corey.vorland 0' (0)
No connection. Trying to reconnect...
ERROR 2005 (HY000): Unknown MySQL server host 'corey.vorland 0' (0)
ERROR:
Can't connect to the server
db.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: academic; Type: TABLE; Schema: public; Owner: -; Tablespace:
--
CREATE TABLE academic (
academic_id integer NOT NULL,
family_name text,
given_name text,
other_names text,
standrews_link text,
comments text,
reference text,
descendant_count integer
);
还
--
-- Name: academic_academic_id_seq; Type: SEQUENCE; Schema: public; Owner: -
--
CREATE SEQUENCE academic_academic_id_seq
START WITH 1
INCREMENT BY 1
NO MAXVALUE
NO MINVALUE
CACHE 1;
--
-- Name: academic_academic_id_seq; Type: SEQUENCE OWNED BY; Schema: public; Owner: -
--
ALTER SEQUENCE academic_academic_id_seq OWNED BY academic.academic_id;
--
-- Name: academic_academic_id_seq; Type: SEQUENCE SET; Schema: public; Owner: -
--
SELECT pg_catalog.setval('academic_academic_id_seq', 190374, true);
--
-- Name: advises; Type: TABLE; Schema: public; Owner: -; Tablespace:
--
CREATE TABLE advises (
advisor integer NOT NULL,
advisee integer NOT NULL,
advice_type character varying(20),
degree integer NOT NULL
);
我尝试了一些方法,例如在第一个CREATE TABLE命令之前从db.sql中删除一些标题行,但仍然有很多错误。由于我对数据库和SQL非常缺乏经验,所以我对如何解决这个问题感到很茫然。我假设有一些关于这个db.sql文件的问题显然来自PostgreSQL,并且有多个表导致我尝试使用source命令将此db.sql文件读入mysql时出现问题?我甚至可以把它读成mysql吗?
感谢您的任何建议。
答案 0 :(得分:0)
正如您所注意到的,您不能只将.sql文件从一个数据库引擎导入另一个数据库引擎。转储输出中存在语法差异,导致文件导致错误。您必须首先将转储文件从postgre语法/格式转换为符合mysql的语法/格式。
幸运的是,你不是第一个遇到这个问题的人(也不是我认为是最后一个)。
首先,你应该看看convert postgresql dump to mysql。
您会注意到第二步是使用名为'pg2mysql'的PHP函数。它接受一个输入(要转换的postgres代码)并返回mysql代码。
有关此有用工具的详细信息,我将向您推荐创作者的github和website。在网站上,您实际上可以使用提供的textarea将PG转换为Mysql。虽然,建议不要对较大的文件使用textarea转换。