linux运维进阶-Mysql的读写分离

以下操作基于RHEL6.5

准备:三台虚拟机

 master:server3 172.25.13.3

 slave:server4 172.25.13.4

 proxy:server5 172.25.13.5 安装proxy         lsof           mysql client


一.配置proxy


 server5:


mkdir /usr/local/mysql-proxy/log
  mkdir /usr/local/mysql-proxy/etc

  vim  /usr/local/mysql-proxy/share/doc/mysql-proxy/rw-splitting.lua  
     40 min_idle_connections = 1,  #最小连接数
     41 max_idle_connections = 2,  #连接数到2后开始负载均衡

  vim /usr/local/mysql-proxy/etc/mysql-proxy.conf

   [mysql-proxy]
   user=root
   daemon=true
   keepalive=true
   log-level=debug
   log-file=/usr/local/mysql-proxy/log/mysql-proxy.log
   admin-username=admin
   admin-password=westos
   admin-lua-script=/usr/local/mysql-proxy/lib/mysql-proxy/lua/admin.lua
   proxy-address=172.25.13.5:3306
   proxy-backend-addresses=172.25.13.3:3306
   proxy-read-only-backend-addresses=172.25.13.4:3306

/usr/local/mysql-proxy/bin/mysql-proxy --plugins=admin --plugins=proxy --defaults-file=/usr/local/mysql-proxy/etc/mysql-proxy.conf
 chmod 660 etc/mysql-proxy.conf

 netstat -antlp



server3:(master)


mysql -p            #输入密码
 grant select, update, insert on *.* to proxy@'172.25.13.%' identified by 'Wrh.6666';
 flush privileges;

    


测试:

在proxy上:


mysql -uadmin -pwestos -h 172.25.13.5 -P 4041
 select * from backends;
 #持续查看  如果别的主机链接proxy 并执行读写操作 那么proxy对去调度master 或slave
 #lsof 会查看到连接记录。


linux运维进阶-Mysql的读写分离_第1张图片


在物理机上测试(安装好mariadb即可)


mysql -h 172.25.13.5 -uproxy -pWrh.6666
 use westos
 select * from usertb;
 insert into usertb values ('user6','666');


 

在slave(server4)上测试:

 mysql -p
 use westos
 select * from usertb;



linux运维进阶-Mysql的读写分离_第2张图片 


打开proxy的另外一个shell:

 

lsof -i :3306


linux运维进阶-Mysql的读写分离_第3张图片


结果:

   物理机连接了proxy并进行了两次写操作,转到了master主机server3;

  server5连接了proxy并进行了读操作,转到了slave从机server4.

    从而实现了Mysql的读写分离,也可以说是负载均衡。



你可能感兴趣的:(mysql)