在AWS RDS中将sqlite3从Django迁移到mysql始终报告语法错误

时间:2015-03-12 14:34:06

标签: mysql django amazon-web-services sqlite

我在AWS RDS中将django sqlite3迁移到mysql(我对mysql很新)。我所做的是:

(1)首先我通过sudo sqlite3 db.sqlite3 .dump > /home/ubuntu/temp.sql

将sqlite3转储到sql文件

(2)然后我尝试了sudo mysql -h MYRDSHOSTNAME -u MYUSERNAME -P 3306 -p mydb < temp.sql

(3)输入密码后,我得到了这个

  

您的SQL语法有错误;检查与MySQL服务器版本对应的手册,以便在第1行'PRAGMA foreign_keys = OFF'附近使用正确的语法

我尝试删除temp.sql中的第一行,但后来发生了同样的错误,它仍然指向第1行。

我还在RDS中检查mydb以确保它确实在那里。它就在那里(当然是空的)。 enter image description here

RDS上的MySQL引擎版本为5.6.22,而django的计算机版本为5.6.19

有什么我忽略或做错了吗?非常感谢你们!

1 个答案:

答案 0 :(得分:0)

#!/usr/bin/env python

import re
import fileinput

def this_line_is_useless(line):
    useless_es = [
        'BEGIN TRANSACTION',
        'COMMIT',
        'sqlite_sequence',
        'CREATE UNIQUE INDEX',
        'PRAGMA foreign_keys=OFF'
        ]
    for useless in useless_es:
        if re.search(useless, line):
                return True

def has_primary_key(line):
    return bool(re.search(r'PRIMARY KEY', line))

searching_for_end = False
for line in fileinput.input():
    if this_line_is_useless(line): continue

    # this line was necessary because ''); was getting
    # converted (inappropriately) to \');
    if re.match(r".*, ''\);", line):
        line = re.sub(r"''\);", r'``);', line)

    if re.match(r'^CREATE TABLE.*', line):
        searching_for_end = True

    m = re.search('CREATE TABLE "?([A-Za-z_]*)"?(.*)', line)
    if m:
        name, sub = m.groups()
        line = "DROP TABLE IF EXISTS %(name)s;\nCREATE TABLE IF NOT EXISTS %(nam                                                                             e)s%(sub)s\n"
        line = line % dict(name=name, sub=sub)
    else:
        m = re.search('INSERT INTO "([A-Za-z_]*)"(.*)', line)
        if m:
                line = 'INSERT INTO %s%s\n' % m.groups()
                line = line.replace('"', r'\"')
                line = line.replace('"', "'")
    line = line.replace('AUTOINCREMENT','AUTO_INCREMENT')
    #line = line.replace('UNIQUE ','')
    line = line.replace('"','')
    line = re.sub(r"(?<!')'t'(?=.)", r"1", line)
    line = re.sub(r"(?<!')'f'(?=.)", r"0", line)

    # Add auto_increment if it's not there since sqlite auto_increments ALL
    # primary keys
    if searching_for_end:
        if re.search(r"integer(?:\s+\w+)*\s*PRIMARY KEY(?:\s+\w+)*\s*,", line):
            line = line.replace("PRIMARY KEY", "PRIMARY KEY AUTO_INCREMENT")
        # replace " and ' with ` because mysql doesn't like quotes in CREATE com                                                                             mands

    # And now we convert it back (see above)
    if re.match(r".*, ``\);", line):
        line = re.sub(r'``\);', r"'');", line)

    if searching_for_end and re.match(r'.*\);', line):
        searching_for_end = False

    if re.match(r"CREATE INDEX", line):
        line = re.sub('"', '`', line)

    print line,

最后,我找到了答案。下面是我的用于将sqlite3转储转换为mysql的python脚本。它实际上来自Quick easy way to migrate SQLite3 to MySQL?我自己的修改,因为在我的情况下不应该删除UINIQUE(由于版本更新我猜)。无论如何,我使用下面的命令转储sqlite3并在mysql端导入它。完成。

sqlite3 sample.db .dump | python dump_for_mysql.py > dump.sql