码迷,mamicode.com
首页 > 数据库 > 详细

mysql主从复制

时间:2020-04-02 16:10:00      阅读:84      评论:0      收藏:0      [点我收藏+]

标签:and   数据   自动   xxx   mys   迁移   创建   vip   one   

一: 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主从复制

标签:and   数据   自动   xxx   mys   迁移   创建   vip   one   

原文地址:https://www.cnblogs.com/zhuangshenhao/p/12127380.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!