linux 基于proxy实现mysql读写分离

**

mysql-proxy

**
MySQL Proxy有一项强大功能是实现“读写分离”,基本原理是让主数据库处理写方面事务,让从库处理SELECT查询。

Amoeba for MySQL是一款优秀的中间件软件,同样可以实现读写分离,负载均衡等功能,并且稳定性也高于MySQL Proxy,有兴趣的可以测试一下。

环境描述:

操作系统:CentOS6.5_x64
主服务器Master:172.25.254.1
从服务器Slave:172.25.254.2
调度服务器MySQL-Proxy:172.25.254.1

proxy安装及配置

proxy安装需要lua
官网下载即可

http://www.lua.org/download.html

下载后  make  make  install 即可

安装proxy
http://mirrors.sohu.com/mysql/MySQL-Proxy/
下载后 
tar zxvf mysql-proxy-0.8.4-linux-glibc2.3-x86-64bit.tar.gz
mv mysql-proxy-0.8.4-linux-glibc2.3-x86-64bit /usr/local/mysql_proxy
cd /usr/local/mysql_proxy/
mkdir scripts
cp share/doc/mysql-proxy/rw-splitting.lua scripts/
vim scripts/rw-splitting.lua

if not proxy.global.config.rwsplit then
     proxy.global.config.rwsplit = {
              min_idle_connections = 1,           #默认4           改为1,表示最小链接数只有超过1时才会进行读写分离
             max_idle_connections = 1,           #默认8
         is_debug = false
      }
end

proxy 使用信息

[root@server1 mysql-proxy]# bin/mysql-proxy --help-proxy
Usage:
  mysql-proxy [OPTION...] - MySQL Proxy

proxy-module
  -P, --proxy-address=<host:port>                         listening address:port of the proxy-server (default: :4040)
  -r, --proxy-read-only-backend-addresses=<host:port>     address:port of the remote slave-server (default: not set)
  -b, --proxy-backend-addresses=<host:port>               address:port of the remote backend-servers (default: 127.0.0.1:3306)
  --proxy-skip-profiling                                  disables profiling of queries (default: enabled)
  --proxy-fix-bug-25371                                   fix bug #25371 (mysqld > 5.1.12) for older libmysql versions
  -s, --proxy-lua-script=<file>                           filename of the lua script (default: not set)
  --no-proxy                                              don't start the proxy-module (default: enabled)
  --proxy-pool-no-change-user                             don't use CHANGE_USER to reset the connection coming from the pool (default: enabled)
  --proxy-connect-timeout                                 connect timeout in seconds (default: 2.0 seconds)
  --proxy-read-timeout                                    read timeout in seconds (default: 8 hours)
  --proxy-write-timeout                                   write timeout in seconds (default: 8 hours)

proxy 管理脚本

#!/bin/sh
export LUA_PATH=/usr/local/share/mysql-proxy/?.lua

mode=$1
if [ -z "$mode" ] ; then
  mode="start"
fi

case $mode in
  'start')
    mysql-proxy --daemon \
--admin-address=:4401 \
--proxy-address=:3307 \
--proxy-backend-addresses=:3306 \
--proxy-read-only-backend-addresses=172.25.254.2:3306 \
--proxy-read-only-backend-addresses=172.25.254.3:3306 \
--proxy-lua-script=/usr/local/share/mysql-proxy/rw-splitting.lua
    ;;

  'stop')
    killall mysql-proxy
    ;;

  'restart')
    if $0 stop ; then
      $0 start
    else
      echo  "retart failed!!!"
      exit 1
    fi
    ;;
esac
exit 0
--daemon 采用daemon方式启动
--admin-address=:4401 指定mysql proxy的管理端口,在这里,表示本机的4401端口
--proxy-address=:3307 指定mysql proxy的监听端口,也可以用 127.0.0.1:3307 表示
--proxy-backend-addresses=:3306 指定mysql主机的端口
--proxy-read-only-backend-addresses=192.168.1.1:3306 指定只读的mysql主机端口
--proxy-read-only-backend-addresses=192.168.1.2:3306 指定另一个只读的mysql主机端口
--proxy-lua-script=/usr/local/share/mysql-proxy/rw-splitting.lua 指定lua脚本,在这里,使用的是rw-splitting脚本,用于读写分离

**

测试

**

[root@server1 mysql-proxy]#  mysql -h127.0.0.1 -uroot -pHh~802119323
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 14
Server version: 5.7.17-log MySQL Community Server (GPL)

Copyright (c) 2000, 2016, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> show processlist;
+----+------+--------------------+------+-------------+------+---------------------------------------------------------------+------------------+
| Id | User | Host               | db   | Command     | Time | State                                                         | Info             |
+----+------+--------------------+------+-------------+------+---------------------------------------------------------------+------------------+
|  8 | bobo | 172.25.254.2:55477 | NULL | Binlog Dump | 8532 | Master has sent all binlog to slave; waiting for more updates | NULL             |
| 14 | root | localhost:59434    | NULL | Query       |    0 | starting                                                      | show processlist |
+----+------+--------------------+------+-------------+------+---------------------------------------------------------------+------------------+
2 rows in set (0.00 sec)

mysql> 
[root@server5 mysql-proxy]# cat  logs/mysql-proxy.log 
2018-08-14 10:14:45: (message) chassis-unix-daemon.c:136: [angel] we try to keep PID=1142 alive
2018-08-14 10:14:45: (debug) chassis-unix-daemon.c:157: waiting for 1142
2018-08-14 10:14:45: (debug) chassis-unix-daemon.c:121: we are the child: 1142
2018-08-14 10:14:45: (critical) plugin proxy 0.8.5 started
2018-08-14 10:14:45: (debug) max open file-descriptors = 1024
2018-08-14 10:14:45: (message) proxy listening on port 0.0.0.0:3306
2018-08-14 10:14:45: (message) added read/write backend: 172.25.15.11:3306
2018-08-14 10:14:45: (message) added read-only backend: 172.25.15.12:3306
2018-08-14 10:14:45: (debug) now running as user: root (0/0)

根据配置文件 打开三台物理机链接proxy端
根据配置可知 只有两台及以下的虚拟机在链接proxy端时,只会链接至master端,在大于两台时,开始读写分离,通过proxy端的losf软件来查看端口用以测试

如下 在第三次链接时指向了slave端,用以查看数据,在写入时,proxy有将数据写入master,达到读写分离。
[root@server5 mysql-proxy]# lsof -i:3306
COMMAND    PID USER   FD   TYPE DEVICE SIZE/OFF NODE NAME
mysql-pro 1189 root   10u  IPv4  10121      0t0  TCP *:mysql (LISTEN)
mysql-pro 1189 root   11u  IPv4  10192      0t0  TCP 172.25.15.15:mysql->172.25.15.250:33792 (ESTABLISHED)
mysql-pro 1189 root   12u  IPv4  10193      0t0  TCP 172.25.15.15:57490->172.25.15.11:mysql (ESTABLISHED)
mysql-pro 1189 root   13u  IPv4  10194      0t0  TCP 172.25.15.15:mysql->172.25.15.250:33794 (ESTABLISHED)
mysql-pro 1189 root   14u  IPv4  10195      0t0  TCP 172.25.15.15:57491->172.25.15.11:mysql (ESTABLISHED)
mysql-pro 1189 root   15u  IPv4  10196      0t0  TCP 172.25.15.15:mysql->172.25.15.250:33796 (ESTABLISHED)
mysql-pro 1189 root   16u  IPv4  10197      0t0  TCP 172.25.15.15:39735->172.25.15.12:mysql (ESTABLISHED)



测试写入时,注意在第三台打开的主机上链接数据库写入,因为此时proxy将用户指向slave,(没有读写分离时slave无法写入数据,但在proxy调度下可以写入数据)。

客户端 
[kiosk@foundation15 ~]$ mysql  -h 172.25.15.15 -u root -pHh~802119323
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MySQL connection id is 7
Server version: 5.7.17-log MySQL Community Server (GPL)

Copyright (c) 2000, 2016, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

MySQL [(none)]> use westos
Database changed
MySQL [westos]> select * from userlist;
+----------+--------+
| username | passwd |
+----------+--------+
| user1    | 123    |
| user2    | 213    |
+----------+--------+
2 rows in set (0.00 sec)
MySQL [westos]> insert into userlist values('user3','321');
Query OK, 1 row affected (0.03 sec)
MySQL [westos]> 

master端

mysql> use westos
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Database changed
mysql> select * from  userlist;
+----------+--------+
| username | passwd |
+----------+--------+
| user1    | 123    |
| user2    | 213    |
| user3    | 321    |
+----------+--------+
3 rows in set (0.00 sec)

以上测试完成
proxy的详细运作过程可以通过tcpdump -i eth0来查看

猜你喜欢

转载自blog.csdn.net/iaMay_____/article/details/81639143