mysql主从复制
Posted zhuangshenhao
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了mysql主从复制相关的知识,希望对你有一定的参考价值。
一: mysql主从复制大体流程
1. master会将变动记录到二进制日志里面
2. master有一个i/o将二进制日志发送到slave
3. slave有一个i/o线程把master发送来的二进制写入到relay日志里面
4. slave有一个SQL线程, 按照relay日志处理slave的数据
二: 主库和从库的配置
注意: 主库和从库的版本要一样
1. 主库配置
[mysqld] character-set-server=utf8 collation-server=utf8_general_ci server-id=100 log-bin=mysql-bin [client] default-character-set=utf8 [mysql] default-character-set=utf8
2. 从库配置
[mysqld] character-set-server=utf8 collation-server=utf8_general_ci server-id=101 log-bin=mysql-slave-bin relay_log=edu-mysql-relay-bin [client] default-character-set=utf8 [mysql] default-character-set=utf8
三: 操作步骤
四: Python代码的实现(读写分离)
1. 实现读写分离的准备
settings.py配置
DATABASES = { ‘default‘: { ‘ENGINE‘: ‘django.db.backends.mysql‘, ‘NAME‘: ‘test‘, ‘USER‘: ‘root‘, ‘PASSWORD‘: ‘123456‘, ‘HOST‘: ‘39.105.40.122‘, ‘PORT‘: 33307, }, ‘slave‘: { ‘ENGINE‘: ‘django.db.backends.mysql‘, ‘NAME‘: ‘test‘, ‘USER‘: ‘root‘, ‘PASSWORD‘: ‘123456‘, ‘HOST‘: ‘39.105.40.122‘, ‘PORT‘: 33306, } } DATABASE_ROUTERS = [‘router.Router1‘,]
views.py
from django.shortcuts import render,HttpResponse # Create your views here. from app01.models import Book def index(request): # Book.objects.using("default").create(title="xx",price=10) # ret=Book.objects.all().using("slave") #using 是queryset对象的方法 # print(ret) # Book.objects.create(title="yyy", price=100) ret = Book.objects.all() print(ret) return HttpResponse(‘ok‘)
创建router.py文件
class Router1:
# 只读数据库 def db_for_read(self, model, **hints): """ Attempts to read auth models go to auth_db. """ return ‘slave‘
# 只写数据库 def db_for_write(self, model, **hints): """ Attempts to write auth models go to auth_db. """ return ‘default‘
# 数据库进行迁移的时候进行约束(但不会报错) def allow_migrate(self, db, app_label, model_name=None, **hints): # print("zzzz") if db == ‘slave‘ and app_label=="app01": return False
2. 实现读写分离(手动和自动操作)
2.1 共同操作
python manage.py makemigrations # python manage.py migrate app名字 --database=配置文件数据库名称 注意: 写数据库 python manage.py migrate app01 --database=default
2.2 手动操作(一般不使用)
# 用using说明你要往哪个库写还是读 models.UserType.objects.using(‘default‘).create(title=‘普通用户‘) result = models.UserType.objects.all().using(‘slave‘)
2.3自动操作
创建router.py文件(在里面写入如上面的router.py文件)
问题: (分库)
app01中的表在default数据库创建
app02中的表在db1数据库创建
如何解决不同的应用使用不同的数据库?
实现步骤:(settings配置如上)
1.
python manage.py makemigrations
2.
# app01中的表在default数据库创建 python manage.py migrate app01 --database=default
3.
# app02中的表在db1数据库创建 python manage.py migrate app02 --database=db1
手动操作
m1.UserType.objects.using(‘default‘).create(title=‘VVIP‘) m2.Users.objects.using(‘db1‘).create(name=‘VVIP‘,email=‘xxx‘)
自动操作
class Router1: 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‘ DATABASE_ROUTERS = [‘db_router.Router1‘, ]
# 使用: m1.UserType.objects.using(‘default‘).create(title=‘VVIP‘) m2.Users.objects.using(‘db1‘).create(name=‘VVIP‘, email=‘xxx‘) # 其他: 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‘
以上是关于mysql主从复制的主要内容,如果未能解决你的问题,请参考以下文章