当前位置:Gxlcms > 数据库问题 > mysql主从复制

mysql主从复制

时间:2021-07-01 10:21:17 帮助过:28人阅读

-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   

人气教程排行