MySQL主从搭建,读写分离

一、主从配置

     

# mysql主从配置的流程大体如图:

1)master会将变动记录到二进制日志里面;

2)master有一个I/O线程将二进制日志发送到slave;

3) slave有一个I/O线程把master发送的二进制写入到relay日志里面;

4)slave有一个SQL线程,按照relay日志处理slave的数据;

二、操作

  正常是准备两台安装好Mysql的服务器,这里就用Docker来做演示

  用Docker拉起两个mysql容器,步骤如下: 

# 拉取mysql5.7镜像
docker pull mysql:5.7
#在home目录下创建mysql文件夹,下面创建data和conf.d文件夹
mkdir /home/mysql
mkdir /home/mysql/conf.d
mkdir /home/mysql/data/
 
# 创建my.cnf配置文件
touch /home/mysql/my.cnf

mkdir /home/mysql2
mkdir /home/mysql2/conf.d
mkdir /home/mysql2/data/
touch /home/mysql2/my.cnf

# 主库配置
[mysqld]
user=mysql
character-set-server=utf8
default_authentication_plugin=mysql_native_password
secure_file_priv=/var/lib/mysql
expire_logs_days=7
sql_mode=STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_ENGINE_SUBSTITUTION
max_connections=1000
server-id=100  
log-bin=mysql-bin
[client]
default-character-set=utf8
[mysql]
default-character-set=utf8


# 从库配置
[mysqld]
user=mysql
character-set-server=utf8
default_authentication_plugin=mysql_native_password
secure_file_priv=/var/lib/mysql
expire_logs_days=7
sql_mode=STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_ENGINE_SUBSTITUTION
max_connections=1000
server-id=101 
log-bin=mysql-slave-bin
relay_log=edu-mysql-relay-bin
[client]
default-character-set=utf8
[mysql]
default-character-set=utf8


# 拉起容器
#启动主库容器(挂载外部目录,端口映射成33307,密码设置为123456)
docker run  -di -v /home/mysql/data/:/var/lib/mysql -v /home/mysql/conf.d:/etc/mysql/conf.d -v /home/mysql/my.cnf:/etc/mysql/my.cnf -p 33307:3306 --name mysql-master -e MYSQL_ROOT_PASSWORD=123456 mysql:5.7
#启动从库容器(挂载外部目录,端口映射成33306,密码设置为123456)
docker run  -di -v /home/mysql2/data/:/var/lib/mysql -v /home/mysql2/conf.d:/etc/mysql/conf.d -v /home/mysql2/my.cnf:/etc/mysql/my.cnf -p 33306:3306 --name mysql-slave -e MYSQL_ROOT_PASSWORD=123456 mysql:5.7

远程连接入主库和从库

#连接主库
mysql -h 172.16.209.100 -P 33307 -u root -p123456
#在主库创建用户并授权
##创建test用户
create user 'test'@'%' identified by '123';
##授权用户
grant all privileges on *.* to 'test'@'%' ;
###刷新权限
flush privileges;
#查看主服务器状态(显示如下图)
show master status;

    

#连接从库
mysql -h 172.16.209.100 -P 33306 -u root -p123456
#配置详解
/*
change master to 
master_host='MySQL主服务器IP地址', 
master_user='之前在MySQL主服务器上面创建的用户名', 
master_password='之前创建的密码', 
master_log_file='MySQL主服务器状态中的二进制文件名', 
master_log_pos='MySQL主服务器状态中的position值';
*/
#命令如下
change master to master_host='101.133.225.166',master_port=33307,master_user='test',master_password='123',master_log_file='mysql-bin.000003',master_log_pos=736;
#启用从库
start slave;
#查看从库状态(如下图)
show slave status\G;

              

    若是Slave_IO_Running为Connecting:则可能是防火墙问题

# //临时关闭
systemctl stop firewalld
# //禁止开机启动防火墙
systemctl disable firewalld  

 三、Django实现读写分离:

  配置settings:

DATABASES = {
    # 只写
    'default': {
        'ENGINE': 'django.db.backends.mysql',
        'NAME': 'test',
        'USER': 'root',
        'PASSWORD': '123456',
        'HOST': '10.0.0.200',
        'PORT': 33307,

    },
    # 只读
    'db1': {
        'ENGINE': 'django.db.backends.mysql',
        'NAME': 'test',
        'USER': 'root',
        'PASSWORD': '123456',
        'HOST': '10.0.0.200',
        'PORT': 33306,

    }
}

  手动指定:

def index(request):
    # 手动指定哪个库
    res = models.Book.objects.using('default').create(name='jpm', price=66.3)
    print(res.name)
    res1 = models.Book.objects.using('db1').all().first()
    print(res1.name)
    return HttpResponse('ok')

  自动配置:

# 在任意位置创建一个py文件:

-read&write.py

class Routers:

    def db_for_read(self, model, **hints):
        return 'db1'  # 只读配置

    def db_for_write(self, model, **hints):
        return 'default'  # 只写配置


# 然后在settings里面注册:
# 注册
DATABASE_ROUTERS = ['read&write.Routers']

  更细的:

#  更细粒度
class Routers:
    def db_for_read(self, model, **hints):
        if model._meta.model_name == 'book':
            return 'db1'
        else:
            return 'default'

    def db_for_write(self, model, **hints):
        return 'default'

  tips:

# 在数据库迁移时,可以指定把哪个app的表结构迁移到哪个库
python manage.py migrate app01 --database=default

 

posted @ 2020-09-07 19:40  三根烦恼丝啊  阅读(202)  评论(0编辑  收藏  举报