主从复制,读写分离

MySQL读写分离-架构

简介

对于很多大型网站(pv值百万、千万)来说,在所处理的业务中,其中有70%的业务是查询(select)相关的业务操作(新闻网站,插入一条新闻。查询操作),剩下的则是写(insert、update、delete,只要能对MySQL的数据造成更改的操作都叫写操作)操作。在使用负载均衡集群之后,可以很大程度的提升网站的整体性能,但是最终的数据处理的压力还是会落到MySQL数据库上,所有很有必要使用一些技术来提升MySQL的负载能力。(读写分离)

写专门交给写服务器处理(一般网站来说写是比较少的 读写比 4:1) 那么需要把读的任务分配多台服务器来完成的架构,就叫做读写分离。

实现方式

第一种:php程序上自己做逻辑判断,写php代码的时候,自己在程序上做逻辑判读写匹配。select,insert、update、delete做正则匹配,根据结果选择写服务器(主服务器)。如果是select操作则选择读服务器(从服务器器) mysql_connect('读写的区分')

第二种:MySQL- Proxy是实现"读写分离(Read/Write Splitting)"的一个软件(MySQL官方提供 ,也叫中间件),基本的原理是让主数据库处理写操作(insert、update、delete),而从数据库处理查询操作(select)。而数据库的一致性则通过主从复制来实现。所以说主从复制是读写分离的基础。

注意:MySQL-proxy它能实现读写语句的区分主要依靠的是内部一个lua脚本(能实现读写语句的判断)。

注意:如果只在主服务器(写服务器)上完成数据的写操作话;这个时候从服务器上没有执行写操作,是没有数据的。这个时候需要使用另外一个技术来实现主从服务器的数据一致性,这个技术叫做 主从复制技术。所以说主从复制是读写分离的基础。

也可以从官方下载。

下载地址:http://mirrors.sohu.com/mysql/MySQL-Proxy/

image

如何查看Linux下c库的版本?

注意通用二进制包需要c库版本一致,或者操作系统的c库比安装包高

uname -a (unix name --all)

拓扑结构-架构图

一主两从

MySQL-proxy 负载均衡的软件(读写分离实现)

使用三台服务器搭建环境

  1. 一台主(写)服务器

    1. 192.168.57.92

    2. 这台主服务器还运行一个读写分离的软件 mysql-proxy

  2. 二台从(读)服务器

    1. 192.168.57.26

    2. 192.168.57.37

    image

    注意事项

  3. 防火墙的关闭

    image
  4. selinux关闭

    image

    selinux永久关闭

    image
    image
  5. root账户必须有远程登录的权限

    image
  6. MySQL的版本需要为 5.5 及以上

    image

    主从复制搭建

  7. 先配置主服务器

  8. 然后在配置从服务器

    主服务操作

    1. 主服务上的配置

    image
  9. 在主服务上添加一个授权的账户,使得到时候从服务器可以使用该账号来完成数据的获取

    GRANT REPLICATION SLAVE ON . TO repuser@"192.168.%.%" IDENTIFIED BY 'admin88';

    添加了一个 repuser的用户,密码为admin88 可以在 192.168.这个网段完成数据的复制。

    image

    查看

    image
  10. 在主服务器上使用 show master status 查看主服务上现在正在使用的bin日志文件和记录的位置。(bin日志文件默认在MySQL的data目录)

![image](http://upload-images.jianshu.io/upload_images/2506965-1412706bdce93869.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)

![image](http://upload-images.jianshu.io/upload_images/2506965-5efb64daedaa2d59.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)

### 从服务器的配置
  1. 修改从服务器的配置文件
![image](http://upload-images.jianshu.io/upload_images/2506965-b0de09365cff7b0b.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
  1. 使用在主服务器上的授权账号去连接主服务器,等待主服务器的数据的写入后,通知获取
# CHANGE MASTER TO

MASTER_HOST='192.168.57.92', MASTER_USER='repuser',

MASTER_PASSWORD='admin88',

MASTER_LOG_FILE='master-bin.000002',

MASTER_LOG_POS=107;

使用上面的语句连接主服务器后,使用

# show slave status\G

![image](http://upload-images.jianshu.io/upload_images/2506965-46641eebdacac68d.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
  1. 开启从服务器
![image](http://upload-images.jianshu.io/upload_images/2506965-27371a63d75689ba.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)

![image](http://upload-images.jianshu.io/upload_images/2506965-cc84d90993de0428.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)

命令:
  1. stop slave 可以停止 从服务器

  2. reset slave 清空从服务器的规则

  3. start slave 启动从服务器

  4. show slave status 查看从服务器状态

  5. show master status 查看主服务器状态

## 读写分离搭建

mac的前世今生

https://yunpan.cn/cY4gQ6sXWaTXd 访问密码 7a0b

下载mysql-proxy

[http://mirrors.sohu.com/mysql/MySQL-Proxy/](http://mirrors.sohu.com/mysql/MySQL-Proxy/)
  1. mysql-proxy上传
![image](http://upload-images.jianshu.io/upload_images/2506965-0a038feba4d71e6d.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
  1. 解压操作
#tar -zxvf mysql-proxy-0.8.4-linux-rhel5-x86-32bit.tar.gz **-C** /usr/local/

![image](http://upload-images.jianshu.io/upload_images/2506965-986c892f240243bf.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
  1. 为mysql-proxy创建一个符号链接
![image](http://upload-images.jianshu.io/upload_images/2506965-e217b914ad06fd39.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
  1. 添加一个mysql-proxy用户
# useradd mysql-proxy

![image](http://upload-images.jianshu.io/upload_images/2506965-6314368c9b59b197.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
  1. 命令查看
![image](http://upload-images.jianshu.io/upload_images/2506965-f4be9cdbcc1b3002.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)

6\. 在下面路径创建一个admin.lua脚本

![image](http://upload-images.jianshu.io/upload_images/2506965-c8a4a3ceb634b198.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)

参照

![image](http://upload-images.jianshu.io/upload_images/2506965-096b47a2695de799.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)

7\. 服务启动

#

./mysql-proxy --daemon --log-level=debug

--log-file=/var/log/mysql-proxy.log

--plugins='proxy'

读写分离脚本

--proxy-lua-script='/usr/local/mysql-proxy/share/doc/mysql-proxy/rw-splitting.lua'

写服务器

--proxy-backend-addresses='192.168.57.92:3306'

读服务器

--proxy-read-only-backend-addresses='192.168.57.29:3306'

--proxy-read-only-backend-addresses='192.168.57.37:3306'

--plugins='admin'

--admin-username='admin'

--admin-password='admin88' --admin-lua-script='/usr/local/mysql-proxy/share/doc/mysql-proxy/admin.lua'

![image](http://upload-images.jianshu.io/upload_images/2506965-229b920acff1066b.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)

相关参数

![image](http://upload-images.jianshu.io/upload_images/2506965-93b1fb56293ebc93.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)

## MySQL读写分离的监控

mysql-proxy提供一个端口用来监控MySQL读写的实现

使用的默认端口是 4041

1\. 使用下面的命令连接mysql-proxy的4041端口

# mysql -uadmin -padmin88 -h 192.168.57.92 --port 4041

![image](http://upload-images.jianshu.io/upload_images/2506965-f9aa500924946c97.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
  1. 使用下面的命令查看读写服务器的状态
# select * from backends;

![image](http://upload-images.jianshu.io/upload_images/2506965-597530e776ad14f0.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
  1. 如果效果不明显,可以修改下面的文件参数
![image](http://upload-images.jianshu.io/upload_images/2506965-213d2737c3ace22c.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)

![image](http://upload-images.jianshu.io/upload_images/2506965-bee72b385081987f.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)

你可能感兴趣的:(主从复制,读写分离)