proxy实现 mysql 读写分离
实现 mysql 读写分离
图解:
环境:
iptables 和 selinux 关闭
proxy:test2 172.25.1.2
Master: test3 172.25.1.3
Slave:test4 172.25.1.4
环境已经实现 test3(master) 和 test4(slave) 的主从复制
Server2:
[[email protected] ~]# ls
mysql-proxy-0.8.5-linux-el6-x86-64bit.tar.gz
[[email protected] ~]# tar zxf mysql-proxy-0.8.5-linux-el6-x86-64bit.tar.gz -C
/usr/local
[[email protected] ~]# cd /usr/local
[[email protected] local]# ln -s mysql-proxy-0.8.5-linux-el6-x86-64bit/ mysql-proxy
[[email protected] local]# vim ~/.bash_profile
[[email protected] local]# source ~/.bash_profile //使环境变量生效
[[email protected]]# cd /usr/local/mysql-proxy/share/doc/mysql-proxy
[[email protected]]# vim rw-splitting.lua //修改读写分离 lua 脚本
mysql proxy 会检测客户端连接, 当连接没有超过 min_idle_connections 值时, 不会进
行读写分离, 即查询操作会发生到 master 上。
[[email protected] local]# cd /usr/local
[[email protected] local]# cd mysql-proxy
[[email protected] mysql-proxy]# mkdir conf
[[email protected] mysql-proxy]# cd conf
[[email protected] conf]# vim mysql-proxy.conf
[[email protected]]#mysql-proxy--defaults-file=/usr/local/mysql-proxy/conf/mysql-proxy.conf
[[email protected] conf]# mkdir ../logs
[[email protected] conf]# chmod 660 /usr/local/mysql-proxy/conf/mysql-proxy.conf //配置文件必须是 660 权限
[[email protected] conf]# mysql-proxy --defaults-file=/usr/local/mysql-proxy/conf/mysql-proxy.confmysql-proxy //再次执行启动配置文件
[[email protected] conf]# netstat -antlp //查看端口
主备 mysql 开启服务
Test3 和 test4 都需要做解压 mysql 并已搭建好主备模式
Test3:
mysql> grant insert,update on westos.* to [email protected]'%' identified by '@Caoxingxing123';
mysql> show master status;
mysql> show slave status;
Test4:
mysql> stop slave;
mysql>change master to master_host='172.25.1.3',master_user='repl',master_password='@Caoxingxing123', master_auto_position=1;
mysql> start slave
mysql> show slave status\G;
Test3:
mysql> show databases;
mysql> CREATE DATABASE westos;
mysql> USE westos;
mysql> CREATE TABLE userlist(
-> username varchar(10) not null,
-> password varchar(30) not null
-> );
mysql> select * from userlist;
Test4:
mysql> show databases;
mysql> use westos;
mysql> select * from userlist; //数据库已经同步过来了
客户端测试:
[[email protected] ~]# mysql -u root -h 172.25.1.2 -p //会出现如下情况
ERROR 1045 (28000): Unknown error 1045
//注意:root 用户需要在 master 和 slave 做授权
test3 和 test4 都需要执行
mysql> grant insert,update on westos.* to [email protected]'%' identified by '@Caoxingxing123';
//再次尝试登陆
[[email protected] ~]# mysql -h 172.25.1.2 -u cxx -p westos
MySQL [(none)]> use westos;
MySQL [westos]> show tables;
MySQL [westos]> insert into userlist values('user1','111');
MySQL [westos]> select * from userlist; //只能写不能读
master 和 slave 端都可以查看到刚插入的数据
Test3:
Test4:
当连接一个时:
test3:通过监控查看连接情况
[[email protected] ~]# yum install lsof -y
[[email protected] ~]# lsof -i :3306 //下载所需的监控
test4:通过监控查看连接情况
[[email protected] ~]# yum install lsof -y
[[email protected] ~]# lsof -i :3306
当连接两个时:
[[email protected] ~]$ mysql -h 172.25.1.2 -u cxx -p westos //加一个
Test3:
[[email protected] mysql]# lsof -i :3306
Test4:
[[email protected] mysql]# lsof -i :3306
当连接三个时(此时已超过最大连接数):
[[email protected] ~]$ mysql -h 172.25.1.2 -u cxx -p westos
Test3:
Test4:
此时在最后一个连接的客户端写数据
MySQL [westos]> insert into userlist values ('user2','222');
Test3 查看数据:
Test4 查看数据:
实现了读写分离,虽然第三个连接到的时 test4 但是当读数据的时候通过 test4 获取,但
当写数据时自动连接 test3 从而保存数据。
总结:
首先连接的是 master,所以此时是对 master 读写,还没有读写分离,只有当连接数
大于设置的最大连接数时才会出现读写分离。
之前设置了最大连接数为 2,最小连接数为 1。所以默认为当连接数(客户端访问量)
为 1 时,连接的是 master,连接数为 2 时,连接的也是 master;只有当连接数大于 2 时,
后面连接的为 slave,且当读的时候访问的是 slave,写的时候访问的是 master(原因是:
master 和 slave 主从同步是单向的,所以写数据的时候如果写到 slave,slave 是不会同步
给 master 的。读的话就可以,因为数据的一致性, slave 和 master 的数据始终是相同的)。