目录
一、读写分离基本概念:
当一个网站或者项目越来越大的时候,数据越来越多,数据库服务器的压力也会越来越大。为了减轻数据库的压力,提升效率,我们一般会将“读”和“写”操作分布在两个服务器上及主(master)和从(minion)服务器。
从服务器会自动备份主服务器的数据,从而承担“读”的操作,主服务器承担“写”的操作
二、主从服务器配置
1、确定master 和 minion的 ip 地址
主服务器:192.168.26.130
从服务器:192.168.26.131
2、确定双方都有mariadb,并且进入生产模式
[root@bogon ~]# systemctl start mariadb
[root@localhost /]# MysqL -u root -h localhost -p
进入生产模式
[root@bogon ~]# MysqL_secure_installation
3、双方都关闭selinux和防火墙
[root@bogon ~]# systemctl stop firewalld
[root@bogon ~]# setenforce 0
4、master服务器创建数据库(masterdb)
MariaDB [(none)]> create database masterdb charset=utf8;
5、master服务器授权所有用户可以操作表(用户名root,密码:admin)
MariaDB [(none)]> GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY 'admin' WITH GRANT OPTION;
MariaDB [(none)]> flush privileges;
6、master服务器在测试的库当中创建测试表(master_table)
MariaDB [masterdb]> create table master_table(id int primary key auto_increment,name char(32));
7、导出数据库
[root@bogon opt]# MysqLdump -h localhost -u root -padmin masterdb > masterdb.sql
scp ./masterdb.sql [email protected]:/opt
9、从服务器创建同名的数据库
create database masterdb charset=utf8;
10、从服务器导入数据表
[root@bogon opt]# MysqL -h localhost -u root -padmin masterdb < masterdb.sql
11、从服务器启动主从备份的配置
[root@bogon opt]# vim /etc/my.cnf
[MysqLd]
datadir=/var/lib/MysqL
socket=/var/lib/MysqL/MysqL.sock
server-id=1
log_bin=MysqL-bin?
binlog-do-db=masterdb
binlog-ignore-db=MysqL
# disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0
# Settings user and group are ignored when systemd is used.
# If you need to run MysqLd under a different user or group,
# customize your systemd unit file for mariadb according to the
# instructions in http://fedoraproject.org/wiki/Systemd
[MysqLd_safe]
log-error=/var/log/mariadb/mariadb.log
pid-file=/var/run/mariadb/mariadb.pid
#
# include all files from the config directory
#
!includedir /etc/my.cnf.d
12、从服务器重启数据库服务,并查看master状态
[root@bogon opt]# systemctl restart mariadb
[root@bogon opt]# MysqL -u root -h localhost -padmin
MariaDB [(none)]> show master status;
13、配置主服务器主从备份
[root@localhost opt]# vim /etc/my.cnf
[MysqLd]
datadir=/var/lib/MysqL
socket=/var/lib/MysqL/MysqL.sock
server-id=2
log_bin=MysqL-bin?
binlog-do-db=masterdb
binlog-ignore-db=MysqL
# disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0
# Settings user and group are ignored when systemd is used.
# If you need to run MysqLd under a different user or group,
# customize your systemd unit file for mariadb according to the
# instructions in http://fedoraproject.org/wiki/Systemd
[MysqLd_safe]
log-error=/var/log/mariadb/mariadb.log
pid-file=/var/run/mariadb/mariadb.pid
#
# include all files from the config directory
#
!includedir /etc/my.cnf.d
14、主服务器重启服务器
[root@localhost opt]# systemctl restart mariadb
15、从服务器上指定要进行主从备份的主服务器
MariaDB [(none)]> change master to MASTER_HOST='192.168.26.130',MASTER_USER='root',MASTER_PASSWORD='admin',MASTER_LOG_FILE='MysqL-bin?.000002',MASTER_LOG_POS=245;
16、双方重启服务器
[root@localhost opt]# systemctl restart mariadb
17、从服务器上启动主从备份,并查看状态
MariaDB [(none)]> slave start;
MariaDB [(none)]> show slave status\G;
16、测试:在主服务器添加数据,在从服务器查看是否有数据
主服务器:
MariaDB [masterdb]> INSERT INTO master_table VALUES();
从服务器:
MariaDB [masterdb]> select * from master_table
三、Django读写分离配置
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.MysqL',
'NAME': 'masterdb',
'HOST': "192.168.26.130",
"USER": "root",
"PASSWORD": "admin"
},#主服务器
'minion': {
'ENGINE': 'django.db.backends.MysqL',
'NAME': 'masterdb',
'HOST': "192.168.26.131",
"USER": "root",
"PASSWORD": "admin"
}#从服务器
}
2、Django手动读写分离
同步数据库
python manage.py migrate --database default
python manage.py migrate --database minion
models.Products.objects.using('default').create()
models.Products.objects.using('minion').all()
定义Router类
新建myrouter.py脚本,定义Router类:
class Router:
def db_for_read(self, model, **hints):
return 'db2'
def db_for_write(self, model, **hints):
return 'default'
配置Router
settings.py中指定DATABASE_ROUTERS
DATABASE_ROUTERS = ['myrouter.Router',]
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。