django数据库读写分离,分库
读写分离
在settings中配置不同名称的数据库连接参数,并配置一条数据库选择路由
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
},
'db1': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': os.path.join(BASE_DIR, 'db1.sqlite3'),
},
}
(1)第一种方法:
手动选择要使用的数据库
m1.UserType.objects.using('default').create(title='VVIP')
m2.Users.objects.using('db1').create(name='VVIP',email='xxx')
(2)方法二:
定义一下路由类,自动执行数据类
在seetings 中加入一条配置
DATABASE_ROUTERS = ['db_router.Router1',]
class Router1:
def db_for_read(self, model, **hints):
"""
Attempts to read auth models go to auth_db.
"""
if model._meta.model_name == 'usertype':
return 'db1'
else:
return 'default' def db_for_write(self, model, **hints):
"""
Attempts to write auth models go to auth_db.
"""
return 'default'
为读写操作指定类
这样在执行查询和修改时候就无需指定数据库
多应用分库
创建数据库时候执行指定的命令
app01中的表在default数据库创建
app02中的表在db1数据库创建 # 第一步:
python manage.py makemigraions # 第二步:
app01中的表在default数据库创建
python manage.py migrate app01 --database=default # 第三步:
app02中的表在db1数据库创建
python manage.py migrate app02 --database=db1
对数据库迁移和读写操作进行约束
数据库迁移时进行约束:
class Router1:
def allow_migrate(self, db, app_label, model_name=None, **hints):
"""
All non-auth models end up in this pool.
"""
if db=='db1' and app_label == 'app02':
return True
elif db == 'default' and app_label == 'app01':
return True
else:
return False # 如果返回None,那么表示交给后续的router,如果后续没有router,则相当于返回True def db_for_read(self, model, **hints):
"""
Attempts to read auth models go to auth_db.
"""
if model._meta.app_label == 'app01':
return 'default'
else:
return 'db1' def db_for_write(self, model, **hints):
"""
Attempts to write auth models go to auth_db.
"""
if model._meta.app_label == 'app01':
return 'default'
else:
return 'db1'
django数据库读写分离,分库的更多相关文章
- Django 数据库读写分离 分库分表
多个数据库 配置: DATABASES = { 'default': { 'ENGINE': 'django.db.backends.sqlite3', 'NAME': os.path.join(BA ...
- django数据库读写分离
django数据库读写分离 1. 配置数据库 settings.py文件中 用SQLite: DATABASES = { 'default': { 'ENGINE': 'django.db.backe ...
- Django的数据库读写分离
Django的数据库读写分离 1.首先是配置数据库 在settings.py文件中增加多个数据库的配置: DATABASES = { 'default': { 'ENGINE': 'django.db ...
- 学会数据库读写分离、分表分库——用Mycat,这一篇就够了!
系统开发中,数据库是非常重要的一个点.除了程序的本身的优化,如:SQL语句优化.代码优化,数据库的处理本身优化也是非常重要的.主从.热备.分表分库等都是系统发展迟早会遇到的技术问题问题.Mycat是一 ...
- 学会数据库读写分离、分表分库——用Mycat
系统开发中,数据库是非常重要的一个点.除了程序的本身的优化,如:SQL语句优化.代码优化,数据库的处理本身优化也是非常重要的.主从.热备.分表分库等都是系统发展迟早会遇到的技术问题问题.Mycat是一 ...
- (转)学会数据库读写分离、分表分库——用Mycat,这一篇就够了!
原文:https://www.cnblogs.com/joylee/p/7513038.html 系统开发中,数据库是非常重要的一个点.除了程序的本身的优化,如:SQL语句优化.代码优化,数据库的处理 ...
- 数据库读写分离、分表分库——用Mycat
转: https://www.cnblogs.com/joylee/p/7513038.html 系统开发中,数据库是非常重要的一个点.除了程序的本身的优化,如:SQL语句优化.代码优化,数据 ...
- Django----配置数据库读写分离
Django配置数据库读写分离 https://blog.csdn.net/Ayhan_huang/article/details/78784486 https://blog.csdn.net/ayh ...
- spring+mybatis利用interceptor(plugin)兑现数据库读写分离
使用spring的动态路由实现数据库负载均衡 系统中存在的多台服务器是"地位相当"的,不过,同一时间他们都处于活动(Active)状态,处于负载均衡等因素考虑,数据访问请求需要在这 ...
随机推荐
- Mybatis <Sql>标签
重复的SQL预计永远不可避免,<sql>标签就是用来解决这个问题的 <sql id="sql1">id,name,age,gender</sql> ...
- struts 页面调用Action的指定方法并传递参数
如果为action配置了类,那么默认就会执行Action类的excute方法,Action类的写法三种: ① public class Action1 { public String execute( ...
- C-Scanf连续调用多次并且存在%c的问题
问题现象: 当程序中存在多个scanf时,针对第一个scanf的输入,一般用户会以空白字符(空格.换行.tab.换页符)等结束.但若后面有一个scanf(“%c”,&ch),则刚才输入的空白字 ...
- 解决 adb devices :???????????? no permissions 方法
- To Use Genymotion
Chinese Site:http://www.genymotion.cn/ Offical Site:http://www.genymotion.com/ Not available in Chi ...
- base、this、new、override、abstract、virtual、static
前言 本文主要来讲解一下C#中,自己觉得掌握的不怎么样或者用的不多,不太熟悉的关键字,主要包括base.this.new.override.abstract.virtual以及针对static字段和s ...
- mysql sql_mode配置
查看mysql sql_mode SELECT @@GLOBAL.sql_mode; 修改mysql sql_mode: set global sql_mode=''; 修改my.ini: sql_m ...
- php处理XML数据
把XML转换成对象直接调用里面的属性 <?php$note=<<<XML<note><to>Tove</to><from>Jan ...
- js中formData的使用
FormData 对象的使用 https://developer.mozilla.org/zh-CN/docs/Web/API/FormData/Using_FormData_Objects http ...
- unity5, animator state machine, 无条件transition实现播放动画序列
今天遇到这样一个需求,我有一个名为happy的animation clip和一个名为speak的animation clip.想实现当主角胜利后播放动序列: happy->speak->h ...