分享

Python+Django 数据库读写分离 | HelloWorld

 浸心阁 2015-04-23

首先,数据库设置为master-slave(一主一备)。至于主从配置,可参考:http://www./?p=296
然后,新建一个dbrouters.py文件

  1. # -*- encoding=utf8 -*-  
  2.   
  3. class MasterSlaveRouter(object):  
  4.     """A router that sets up a simple master/slave configuration"""  
  5.   
  6.     def db_for_read(self, model, **hints):  
  7.         """Point all read operations to a random slave"""  
  8.         return 'slave'  
  9.   
  10.     def db_for_write(self, model, **hints):  
  11.         """Point all write operations to the master"""  
  12.         return 'master'  
  13.   
  14.     def allow_relation(self, obj1, obj2, **hints):  
  15.         """Allow any relation between two objects in the db pool"""  
  16.         db_list = ('master','slave')  
  17.         if obj1._state.db in db_list and obj2._state.db in db_list:  
  18.             return True  
  19.         return None  
  20.   
  21.     def allow_syncdb(self, db, model):  
  22.         """Explicitly put all models on all databases."""  
  23.         return True  

最后修改settings.py

  1. ...  
  2. DATABASES = {  
  3.     'default': {  
  4.         'ENGINE': 'django.db.backends.mysql', # Add 'postgresql_psycopg2', 'postgresql', 'mysql', 'sqlite3' or 'oracle'.  
  5.         'NAME': 'dbname',                      # Or path to database file if using sqlite3.  
  6.         'USER': 'username',                      # Not used with sqlite3.  
  7.         'PASSWORD': 'passwd',                  # Not used with sqlite3.  
  8.         'HOST': '1.1.1.1',                      # Set to empty string for localhost. Not used with sqlite3.  
  9.         'PORT': '3306',                      # Set to empty string for default. Not used with sqlite3.  
  10.     },  
  11.     'master': {  
  12.         'ENGINE': 'django.db.backends.mysql', # Add 'postgresql_psycopg2', 'postgresql', 'mysql', 'sqlite3' or 'oracle'.  
  13.         'NAME': 'dbname',                      # Or path to database file if using sqlite3.  
  14.         'USER': 'username',                      # Not used with sqlite3.  
  15.         'PASSWORD': 'passwd',                  # Not used with sqlite3.  
  16.         'HOST': '1.1.1.1',                      # Set to empty string for localhost. Not used with sqlite3.  
  17.         'PORT': '3306',                     # Set to empty string for default. Not used with sqlite3.  
  18.     },  
  19.     'slave': {  
  20.         'ENGINE': 'django.db.backends.mysql', # Add 'postgresql_psycopg2', 'postgresql', 'mysql', 'sqlite3' or 'oracle'.  
  21.         'NAME': 'dbname',                      # Or path to database file if using sqlite3.  
  22.         'USER': 'username',                      # Not used with sqlite3.  
  23.         'PASSWORD': 'passwd',                  # Not used with sqlite3.  
  24.         'HOST': '2.2.2.2',                      # Set to empty string for localhost. Not used with sqlite3.  
  25.         'PORT': '3306',                     # Set to empty string for default. Not used with sqlite3.  
  26.     }  
  27. }  
  28.   
  29. DATABASE_ROUTERS = ['path.dbrouters.MasterSlaveRouter']  
  30. ...  

如此,凡是涉及读操作,都会使用slave数据库;凡是写操作,都会使用master数据库。

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多