[翻译]  Converting an existing MyISAM database to InnoDB with Django

[CHINESE]  使用Django将现有的MyISAM数据库转换为InnoDB


Is there anyway I can convert a full populated MyISAM database to InnoDB (in a way that will create all foreign key contraints, same way it would be if I ran the syncdb command from the beginning)?

无论如何我可以将一个完整填充的MyISAM数据库转换为InnoDB(以某种方式创建所有外键约束,就像我从头开始运行syncdb命令一样)?

4 个解决方案

#1


5  

This might help:

这可能有所帮助:

from django.core.management.base import BaseCommand
from django.db import connections


class Command(BaseCommand):

    def handle(self, database="default", *args, **options):

        cursor = connections[database].cursor()

        cursor.execute("SHOW TABLE STATUS")

        for row in cursor.fetchall():
            if row[1] != "InnoDB":
                print "Converting %s" % row[0],
                print cursor.execute("ALTER TABLE %s ENGINE=INNODB" % row[0])

Add that to your app under the folders management/commands/ Then you can convert all your tables with a manage.py command:

在文件夹管理/命令下将其添加到您的应用程序/然后您可以使用manage.py命令转换所有表:

python manage.py convert_to_innodb

#2


3  

Converting MyISAM to InnoDB with Django.

使用Django将MyISAM转换为InnoDB。

Given the old database is in MyISAM.

鉴于旧数据库在MyISAM中。

Dump the data of old database to json with:

将旧数据库的数据转储到json:

$ python manage.py dumpdata contenttypes --indent=4 --natural > contenttype.json
$ python manage.py dumpdata --exclude contenttypes --indent=4 --natural > everything_else.json

Delete the old database, and create it again.

删除旧数据库,然后重新创建。

Add InnoDB settings in your settings.py like this:

在您的settings.py中添加InnoDB设置,如下所示:

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.mysql',
        'STORAGE_ENGINE': 'InnoDB',
        'NAME': 'yourdbname',
        'USER': '',
        'PASSWORD': '',
        'HOST': '',
        'PORT': '',
        'OPTIONS': {
            'init_command': 'SET storage_engine=InnoDB',  # better to set this in your database config, otherwise django has to do a query everytime
        }
    }
}

Create tables (django also adds the relations) Make sure you don't add an admin user:

创建表(django还添加关系)确保您不添加管理员用户:

$ python manage.py syncdb --migrate

Now you want to empty all the old tables:

现在要清空所有旧表:

$ python manage.py sqlflush | ./manage.py dbshell

Now you can load the new data into the database like so:

现在您可以将新数据加载到数据库中,如下所示:

$ python manage.py loaddata contenttype.json
$ python manage.py loaddata everything_else.json

There you go. I used Django==1.4 for this.

你去吧我使用Django == 1.4。

#3


2  

This really has nothing to do with Django. It's entirely a MySQL thing, and there's documentation on just this type of thing directly from them: http://dev.mysql.com/doc/refman/5.5/en/converting-tables-to-innodb.html

这真的与Django无关。它完全是一个MySQL的东西,直接从它们那里得到关于这类东西的文档:http://dev.mysql.com/doc/refman/5.5/en/converting-tables-to-innodb.html

#4


1  

I had a similar situation where I didn't realize my hosting provider had such an old MySQL version that it still defaulted to MyISAM, but I'd already set up the basic Django tables.

我有一个类似的情况,我没有意识到我的托管服务提供商有这么老的MySQL版本,它仍然默认为MyISAM,但我已经设置了基本的Django表。

This was posted about 2 months after the original question: Convert Legacy Django MySQL DBS from MyISAM to InnoDB.

这是在原始问题发布后约2个月发布的:将Legacy Django MySQL DBS从MyISAM转换为InnoDB。

That and leech/Trey's answer describe the ALTER TABLE ___ ENGINE=INNODB command that converts the table in MySQL, but the problem is that even though the tables are converted, they don't have the foreign key constraints that would have been set up if the tables had been INNODB in the first place.

那个和leech / Trey的答案描述了在MySQL中转换表的ALTER TABLE ___ ENGINE = INNODB命令,但问题是即使转换了表,它们也没有设置的外键约束。表格首先是INNODB。

I found, on django-admin.py and manage.py, that python manage.py sqlall appname "prints the CREATE TABLE and initial-data SQL statements for the given app name(s)."

我发现,在django-admin.py和manage.py上,python manage.py sqlall appname“打印给定应用程序名称的CREATE TABLE和初始数据SQL语句。”

I looked at INSTALLED_APPS in settings.py and ended up running something like python manage.py sqlall admin auth contenttypes sessions sites messages staticfiles (one for each django.contrib.appname in INSTALLED_APPS). That showed the initial CREATE TABLE statements, indexes and foreign key constraints:

我在settings.py中查看了INSTALLED_APPS并最终运行了类似python的东西manage.py sqlall admin auth contenttypes会话站点消息staticfiles(INSTALLED_APPS中每个django.contrib.appname一个)。这显示了初始的CREATE TABLE语句,索引和外键约束:

ALTER TABLE `django_admin_log` ADD CONSTRAINT `content_type_id_refs_id_288599e6` FOREIGN KEY (`content_type_id`) REFERENCES `django_content_type` (`id`);
ALTER TABLE `django_admin_log` ADD CONSTRAINT `user_id_refs_id_c8665aa` FOREIGN KEY (`user_id`) REFERENCES `auth_user` (`id`);
ALTER TABLE `auth_permission` ADD CONSTRAINT `content_type_id_refs_id_728de91f` FOREIGN KEY (`content_type_id`) REFERENCES `django_content_type` (`id`);
ALTER TABLE `auth_group_permissions` ADD CONSTRAINT `permission_id_refs_id_a7792de1` FOREIGN KEY (`permission_id`) REFERENCES `auth_permission` (`id`);
ALTER TABLE `auth_group_permissions` ADD CONSTRAINT `group_id_refs_id_3cea63fe` FOREIGN KEY (`group_id`) REFERENCES `auth_group` (`id`);
ALTER TABLE `auth_user_user_permissions` ADD CONSTRAINT `permission_id_refs_id_67e79cb` FOREIGN KEY (`permission_id`) REFERENCES `auth_permission` (`id`);
ALTER TABLE `auth_user_groups` ADD CONSTRAINT `group_id_refs_id_f0ee9890` FOREIGN KEY (`group_id`) REFERENCES `auth_group` (`id`);
ALTER TABLE `auth_user_user_permissions` ADD CONSTRAINT `user_id_refs_id_f2045483` FOREIGN KEY (`user_id`) REFERENCES `auth_user` (`id`);
ALTER TABLE `auth_user_groups` ADD CONSTRAINT `user_id_refs_id_831107f1` FOREIGN KEY (`user_id`) REFERENCES `auth_user` (`id`);
ALTER TABLE `auth_message` ADD CONSTRAINT `user_id_refs_id_9af0b65a` FOREIGN KEY (`user_id`) REFERENCES `auth_user` (`id`);

After converting all the tables to ENGINE=INNODB I ran the foreign key constraints above and believe I should have the database in the same state as it would have been if my database had defaulted to creating INNODB tables in the first place.

在将所有表转换为ENGINE = INNODB之后,我运行了上面的外键约束,并且相信我应该让数据库处于与我的数据库首先默认创建INNODB表时相同的状态。

Incidentally, as Michael van de Waeter mentioned in his answer, if you want any new tables Django creates to be INNODB by default, you should add 'OPTIONS': {"init_command": "SET storage_engine=INNODB",} to the DATABASES dict in settings.py

顺便提一下,正如Michael van de Waeter在他的回答中提到的,如果你想要任何新表格,Django默认创建为INNODB,你应该在DATABASES dict中添加'OPTIONS':{“init_command”:“SET storage_engine = INNODB”,}在settings.py中


注意!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系我们删除。



 
© 2014-2018 ITdaan.com 粤ICP备14056181号