django数据库分库migrate

最近在研究微服务和分布式,设计到了数据库分库,记录一下
首先,创建多个数据库,如果是已经生成的数据库,可以分库,这里我是新创建的项目,刚好可以用来尝试
我是用docker创建的mysql数据库容器
拉取docker镜像:
docker pull mysql:5.7
创建容器:
docker run --name=mysql5.7 -p 13306:3306 -e MYSQL_ROOT_PASSWORD=123456 -d mysql:5.7
创建多个mysql容器之后,django配置好相应数据库,我这里分成了三个数据库,一个是默认,放django的user,group,permission,session之类的,另外一个是order和sale两个app的,这里我打算下一步微服务的时候再拆开,这里先记录分库的事情,数据库代码如下:

DATABASES = {      'default': {          'ENGINE': 'django.db.backends.mysql',          'HOST': '192.168.1.112',          'PORT': '13308',          'NAME': 'default',          'USER': 'root',          'PASSWORD': '0123456789',          'TEST': {              'MIRROR': 'default'          }      },      'sale': {          'ENGINE': 'django.db.backends.mysql',          'HOST': '192.168.1.112',          'PORT': '13306',          'NAME': 'sale',          'USER': 'root',          'PASSWORD': '0123456789',          'TEST': {              'MIRROR': 'default'          }      },      'order': {          'ENGINE': 'django.db.backends.mysql',          'HOST': '192.168.1.112',          'PORT': '13307',          'NAME': 'order',          'USER': 'root',          'PASSWORD': '0123456789',          'TEST': {              'MIRROR': 'default'          }      }  }

分库之后,在settings.py同级目录下,创建一个databaserouter

DATABASE_MAPPING = {      # example:      # 'app_name':'database_name',      'sale': 'sale',      'order': 'order',      'admin': 'default',      'auth': 'default',      'contenttypes': 'default',      'sessions': 'default',  }      class DBAppsRouter(object):      def db_for_read(self, model, **hints):          """"Point all read operations to the specific database."""          if model._meta.app_label in DATABASE_MAPPING:              return DATABASE_MAPPING[model._meta.app_label]          return None        def db_for_write(self, model, **hints):          """Point all write operations to the specific database."""          if model._meta.app_label in DATABASE_MAPPING:              return DATABASE_MAPPING[model._meta.app_label]          return None        def allow_relation(self, obj1, obj2, **hints):          """Allow any relation between apps that use the same database."""          db_obj1 = DATABASE_MAPPING.get(obj1._meta.app_label)          db_obj2 = DATABASE_MAPPING.get(obj2._meta.app_label)          if db_obj1 and db_obj2:              if db_obj1 == db_obj2:                  return True              else:                  return False          return None        def allow_syncdb(self, db, model):          """Make sure that apps only appear in the related database."""            if db in DATABASE_MAPPING.values():              return DATABASE_MAPPING.get(model._meta.app_label) == db          elif model._meta.app_label in DATABASE_MAPPING:              return False          return None        def allow_migrate(self, db, app_label, model=None, **hints):          """          Make sure the auth app only appears in the 'auth_db'          database.          """          if db in DATABASE_MAPPING.values():              return DATABASE_MAPPING.get(app_label) == db          elif app_label in DATABASE_MAPPING:              return False          return None

这里一定要把DATABASE_MAPPING里面的app名字和app名字对应,不然会读写不到,具体可以看上述代码,然后在模型类后面给每个模型添加一个app_label:

    class Meta:          app_label = 'order'

最后就是migrate数据库了,先makemigrations order,然后migrate --database order
后面sale app一样