zoukankan      html  css  js  c++  java
  • 将sqllite3数据库迁移到mysql

    一.安装python mysql module (OneDrive):

    1.运行python D:OneDriveWorkdjangomysqlregistry.py
    2.http://www.dlldll.com/ 下载libguide40.dll和 libmmd.dll这两个文件,然后拷贝到C:WINDOWS/system32/ 目录下
    3.http://www.codegood.com/downloads 下载MySQL-python x64

    二.准备好mysql数据库:

    0.新建mgmt数据库:

       CREATE DATABASE mgmt CHARACTER SET utf8 COLLATE utf8_general_ci; (注意字符格式)

    1.编辑settings.py,添加要迁移到的新数据库:

    DATABASES = {
        'default': {
            'ENGINE': 'django.db.backends.sqlite3',
            'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
        },
        'mgmtdb':{   #配置第二个数据库节点名称
            'ENGINE': 'django.db.backends.mysql',
            'NAME': 'mgmt', #第二个数据库的名称
            'USER': 'user',
            'PASSWORD': 'pass',
            'HOST':'10.16.2.1',
            'PORT':'3306',   
        }
    }

    2.在新数据库mgmt中新建表(使用syncdb命令同步):

      python manage.py syncdb --database mgmt

    3.在项目根目录新建migratedb.py,如下:

    #!/usr/bin/env python
    from django.contrib.contenttypes.models import ContentType
    
    def run():
    
        def  do(Table):
            if Table is not None:
                table_objects = Table.objects.all()
                for i in table_objects:
                    i.save(using='mgmtdb')  #注意修改数据库节点名称
    
        ContentType.objects.using('mgmtdb').all().delete()  #注意修改数据库节点名称
    
        for i in ContentType.objects.all():
            do(i.model_class())

    4.同步表数据:

      python manage.py shell

      from migratedb import run

      run()

     之后在mysql中可以看到新建的表,表为空

    5.将mgmt数据库更改为默认数据库:

    DATABASES = {
        'default': {
            'ENGINE': 'django.db.backends.mysql',
            'NAME': 'mgmt', #数据库名称
            'USER': 'user',
            'PASSWORD': 'pass',
            'HOST':'10.16.2.1',
            'PORT':'3306',  
        }
    }

    出现以下警告,有几个字段被截断(暂未理会):

    C:Python27libsite-packagesdjango-1.8.4-py2.7.eggdjangodbackendsmysqlase.py:124: Warning: Data truncated for column 'SN' at row 1
      return self.cursor.execute(query, args)
    
    C:Python27libsite-packagesdjango-1.8.4-py2.7.eggdjangodbackendsmysqlase.py:124: Warning: Data truncated for column 'publicPort' at row 1
      return self.cursor.execute(query, args)
    
    C:Python27libsite-packagesdjango-1.8.4-py2.7.eggdjangodbackendsmysqlase.py:124: Warning: Data truncated for column 'privatePort' at row 1
      return self.cursor.execute(query, args)

    参考:http://blog.csdn.net/iloveyin/article/details/49480539

  • 相关阅读:
    LeetCode 79
    LeetCode 437
    LeetCode 783
    LeetCode 59
    LeetCode 每日一题 04/24
    LeetCode 5
    LeetCode 43
    简易多线程任务 往数据库插数据
    定时任务--查数据库--注解实现
    redis 简易 实现
  • 原文地址:https://www.cnblogs.com/dreamer-fish/p/5707286.html
Copyright © 2011-2022 走看看