mysql主从基于docker和django实现读写分离
1.主从搭建
从同步的流程或原理:
- 1)master会将变动记录到二进制日志里面;
- 2)master有一个I/O线程将二进制日志发送到slave;
- 3) slave有一个I/O线程把master发送的二进制写入到relay日志里面;
- 4)slave有一个SQL线程,按照relay日志处理slave的数据;
实操
创建两个文件夹 :
mkdir /home/mysql/data/
touch /home/mysql/conf.d
touch /home/mysql/my.cnf
touch /home/mysql/conf.d
touch /home/mysql/my.cnf
mkdir /home/mysql2/data/
touch /home/mysql/conf.d
touch /home/mysql/my.cnf
修改配置文件:
主库的配置文件,server-id和开启binlog日志
[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
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
从库的配置:
多了下面两行
log-bin=mysql-slave-bin # 指定日志
relay_log=edu-mysql-relay-bin 指定中继日志
[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
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
拉起两个mysql容器:
#启动主库容器(挂载外部目录,端口映射成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
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
创建test用户,并授权
#在主库创建用户并授权
##创建test用户
create user ‘test’@’%’ identified by ‘123’;
##授权用户
grant all privileges on *.* to ‘test’@’%’ ;
###刷新权限
flush privileges;
#查看主服务器状态(显示如下图)
show master status;
# 可以看到日志文件的名字,和现在处在哪个位置
##创建test用户
create user ‘test’@’%’ identified by ‘123’;
##授权用户
grant all privileges on *.* to ‘test’@’%’ ;
###刷新权限
flush privileges;
#查看主服务器状态(显示如下图)
show master status;
# 可以看到日志文件的名字,和现在处在哪个位置
连接从库,配置连接主库
master_log_file 可能不一样 ,用这个命令查看show slave status\G;
#连接从库
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=0;
# 启用从库
start slave;
# 查看从库状态(如下图)
show slave status\G;
####这两个是yes表示配成功 ####
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
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=0;
# 启用从库
start slave;
# 查看从库状态(如下图)
show slave status\G;
####这两个是yes表示配成功 ####
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
测试:
#在主库上创建数据库test1
create database test1;
use test1;
#创建表
create table tom (id int not null,name varchar(100)not null ,age tinyint);
#插入数据
insert tom (id,name,age) values(1,”,20),(2,’yyy’,7),(3,’zzz’,23);
create database test1;
use test1;
#创建表
create table tom (id int not null,name varchar(100)not null ,age tinyint);
#插入数据
insert tom (id,name,age) values(1,”,20),(2,’yyy’,7),(3,’zzz’,23);
2.django实现读写分离
settings.py配置
#1 在setting中配置
DATABASES = {
# 主库
‘default’: {
‘ENGINE’: ‘django.db.backends.mysql’,
‘NAME’: ‘lqz1’,
‘USER’: ‘root’,
‘PASSWORD’: ‘123456’,
‘HOST’: ‘101.133.225.166’,
‘PORT’: 33307,
},
# 从库
‘db1’: {
‘ENGINE’: ‘django.db.backends.mysql’,
‘NAME’: ‘lqz1’,
‘USER’: ‘root’,
‘PASSWORD’: ‘123456’,
‘HOST’: ‘101.133.225.166’,
‘PORT’: 33306,
},
}
DATABASES = {
# 主库
‘default’: {
‘ENGINE’: ‘django.db.backends.mysql’,
‘NAME’: ‘lqz1’,
‘USER’: ‘root’,
‘PASSWORD’: ‘123456’,
‘HOST’: ‘101.133.225.166’,
‘PORT’: 33307,
},
# 从库
‘db1’: {
‘ENGINE’: ‘django.db.backends.mysql’,
‘NAME’: ‘lqz1’,
‘USER’: ‘root’,
‘PASSWORD’: ‘123456’,
‘HOST’: ‘101.133.225.166’,
‘PORT’: 33306,
},
}
手动指定读写分离
####手动来做
# 向default库写,主库
res=models.Book.objects.using(‘default’).create(name=’金瓶梅’,price=33.4)
# 去从库查
res=models.Book.objects.using(‘db1’).all().first()
print(res.name)
# 向default库写,主库
res=models.Book.objects.using(‘default’).create(name=’金瓶梅’,price=33.4)
# 去从库查
res=models.Book.objects.using(‘db1’).all().first()
print(res.name)
自动指定(写router和配置setting)
在根目录下创建一个db_router.py
class Router1:
def db_for_read(self, model, **hints):
return ‘db1’
def db_for_write(self, model, **hints):
return ‘default’
def db_for_read(self, model, **hints):
return ‘db1’
def db_for_write(self, model, **hints):
return ‘default’
在setting中注册
# 注册一下 # 4 以后只要是写操作,就会用default,只要是读操作自动去db1
DATABASE_ROUTERS = [‘db_router.Router1’,]
DATABASE_ROUTERS = [‘db_router.Router1’,]
更细粒度(分库分表时需要)
class Router1:
def db_for_read(self, model, **hints):
if model._meta.model_name == ‘book’:
return ‘db1’
else:
return ‘default’
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’
在数据库迁移时,可以指定把哪个app的表结构迁移到哪个库
# django默认是default
python manage.py migrate app01 –database=default
python manage.py migrate app01 –database=default
到此这篇关于mysql主从基于docker和django实现读写分离的文章就介绍到这了,更多相关mysql 读写分离内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!