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)状态,处于负载均衡等因素考虑,数据访问请求需要在这 ...
随机推荐
- hdu 3572 Task Schedule(最大流&&建图经典&&dinic)
Task Schedule Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) To ...
- ubuntu卸载第三方库
下面以pcl和opencv为例进行说明. 参考资料: https://www.cnblogs.com/txg198955/p/5990295.html ubuntu卸载opencv并重装opencv ...
- 从源代码制作iDempiere Server安装软件(Ubuntu Desktop 12.04 LTS 64位)
怀揣着为中小企业量身定做一整套开源软件解决方案的梦想开始了一个网站的搭建.http://osssme.org/ 在Eclipse中寻找org.adempiere.server-feature项目 右击 ...
- jBoss无法通过IP地址访问,只能用localhost\127.0.0.1访问
http://feng88724.iteye.com/blog/263211 JBOSS版本:4.2.2GA 症状:服务器无法通过IP地址去访问,只能用127.0.0.1或者localhost来访问. ...
- java基础讲解06-----字符串
1. package test; public class chb01 { public static void main(String[] ggs) { /** ...
- 使用swagger实现在线api文档自动生成 在线测试api接口
使用vs nuget包管理工具搜索Swashbuckle 然后安装便可 注释依赖于vs生成的xml注释文件
- 使用css3属性transition实现页面滚动
<!DOCTYPE html> <html> <head> <meta http-equiv="Content-type" content ...
- js+CSS 实现可以编辑的下拉列表框
<html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="C ...
- 【剑指offer】Q17:合并两个排序的链表
def Merge(head1, head2): if head1 == None: return head2 if head2 == None: return head1 psuhead = Lis ...
- layui基础上的tree菜单动态渲染;
var layout=[ { title:'脚本对象名称', treeNodes:true, headerClass:'value_col', colClass:'value_col', style: ...