目录
一、读写分离基本概念:
二、主从服务器配置
三、Django读写分离配置
当一个网站或者项目越来越大的时候,数据越来越多,数据库服务器的压力也会越来越大。为了减轻数据库的压力,提升效率,我们一般会将“读”和“写”操作分布在两个服务器上及主(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
8、将数据库导出文件传递给从服务器minion
scp ./masterdb.sql root@192.168.26.131:/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
1、在项目settings.py文件下编写数据库配置
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',]