天堂1111 发表于 2018-9-27 06:32:46

mysql master-slave读写分离

什么是mysql_proxy?  mysql_proxy是一个简单的位于客户端和mysql服务器程序,它可以监测分析改变起通信,灵活行,允许很多用途,常使用的:负载均衡;备援;查询分析;查询过滤等等。
  下面我将要使用mysql_proxy来实现读写分离.首先我这篇文章是结合上一篇mysql master-slave 同步来写的,清大家一定要结合上篇文章。
  linux OS : ubuntu 8.04.1
  software : mysql-serverlibmysqlclient15-dev libmysqlclient15off lua libeventdb-dev
  mysql1:192.168.6.4         //
  mysql2:192.168.6.5      // 接上篇文章的两台 mysql服务器
  mysql_proxy : 192.168.6.3
  在 mysql1 于mysql2上执行
  建立一个空的数据库 test;
  建立一个空表
  create table proxy(id int(5),name
  char(10));
  1.在mysql-proxy服务器上安装 mysql-proxy
  apt-get installlibmysqlclient15-dev libmysqlclient15offlibeventdb-dev
  lua5.1
  tar zxvf mysql-proxy-0.6.1.tar.gz
  cd mysql-proxy-0.6.1
  ./configure --prefix=/usr/local/mysql-proxy
  make
  make install
  /usr/local/mysql-proxy/sbin/mysql-proxy --help-all
  Usage:
  mysql-proxy - MySQL Proxy
  Help Options:
  -?, --help                                          Show help options
  --help-all                                          Show all help options
  --help-admin                                        Show options for the admin-module
  --help-proxy                                        Show options for the proxy-module
  admin module
  --admin-address=                         listening address:port of internal admin-server (default: :4041)
  proxy-module
  --proxy-address=                         listening address:port of the proxy-server (default: :4040)
  --proxy-read-only-backend-addresses=   address:port of the remote slave-server (default: not set)
  --proxy-backend-addresses=               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
  --proxy-lua-script=                           filename of the lua script (default: not set)
  --no-proxy                                          Don't start proxy-server
  Application Options:
  -V, --version                                       Show version
  --daemon                                          Start in daemon-mode
  --pid-file=                                 PID file in case we are started as daemon
  --admin-address=    管理地址 内部管理服务器默认端口 4041
  --proxy-address=   代理地址端口代理服务器 默认端口4040
  --proxy-read-only-backend-addresses= 代理只读后端服务器地址
  --proxy-backend-addresses= 代理后断服务器地址 远程后断服务器默认 127.0.0.1:3306
  --proxy-skip-profiling    还不清楚什么意思~
  -proxy-fix-bug-25371   应该是修复了漏洞吧,解释是这样的。
  --proxy-lua-script=proxy-lua脚本 默认是没有设置的
  --no-proxy               没有代理不启动代理服务器
  -V, --version            显示 mysql-proxy版本
  --daemon                   以守护进程启动模式
  --pid-file=          daemon 启动模式的 pid文件
  mysql-proxy 启动,这里我用的一个CU 朋友写的脚本,期间有些改动也是这位朋友告诉我几个要改的地方。再次感谢 KDr2 这个脚本我会上传开放给大家
  /usr/loca/mysql-proxy/sbin/mysql-proxy --proxy-read-only-backend-addresses=192.168.6.5:3306 --proxy-backend-addresses=192.168.6.4:3306--proxy-lua-script=/usr/local/mysql-proxy/mysql.lua&
  netstat -ant
  tcp      0      0 0.0.0.0:4040            0.0.0.0:*               LISTEN
  tcp      0      0 0.0.0.0:4041            0.0.0.0:*               LISTEN
  启动成功
  可以远程连接了,在一台linux主机上连接,进行一些读写操作
  mysql -uroot -p -P4040 -h192.168.6.3
  ludy@ludy:~$ mysql -uroot -p -P4040 -h192.168.6.3
  Enter password:
  Welcome to the MySQL monitor.Commands end with ; or \g.

  Your MySQL connection>  Server version: 5.0.51a-3ubuntu5.4-log
  Type 'help;' or '\h' for help. Type '\c' to clear the buffer.
  mysql> use test;
  Database changed
  mysql> insert into proxy(name) values("10");
  Query OK, 1 row affected (0.01 sec)
  mysql>select * from proxy;
  Empty set (0.00 sec)
  我们插入了数据没有结果~这就对了读写分离了~~~~
  接下来我们进入 mysql1 192.168.6.4看看
  ludy@ludy:~$ mysql -uroot -p -h192.168.6.4
  Enter password:
  Welcome to the MySQL monitor.Commands end with ; or \g.

  Your MySQL connection>  Server version: 5.0.51a-3ubuntu5.4-log (Ubuntu)
  Type 'help;' or '\h' for help. Type '\c' to clear the buffer.
  mysql> use test;
  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 proxy;
  +------+------+

  |>  +------+------+
  | NULL | 10   |
  +------+------+
  1 row in set (0.00 sec)
  有数据 ~~ 最后 看看 mysql-proxy 的日志~
  vim /var/log/mysql-proxy.log
   info:starting connect_server ...
  2 debug:
  3 .connected_clients = 0
  4 debug:
  5 .idling_connections = 1
  6 debug:
  7 .type = 1
  8 debug: .state = 1
  9 debug: .connected_clients = 0
  10 debug: .idling_connections = 0
  11 debug: .type = 2
  12 debug: .state = 0
  13 info: server open new connection
  14 debug:
  15 debug: authed backend = 0
  16 debug: used db =
  17 debug: Query Target is
  18 debug: transaction res : 0
  19 debug:
  20 debug: authed backend = 0
  21 debug: used db =
  22 debug: Query Target is
  23 debug: transaction res : 0
  很明显了 读是在 192.168.6.5 写及一些事务是在192.168.6.4 执行的。
  这样,写在 master 读在salvae 上,然后 master -->slave 同步,简单的负载均衡 ~ 好了我把脚本方上来,大家测试成功后最好修改脚本
  找到   local log_level=1
  改为   local log_level=2
  因为 debug 很耗资源
  顺便我也把我修改的 官方 rw-splitting.lua 方上来 很多人说官方的不起作用
  我想是大家没看 清楚脚本吧。官方的 rw-splitting.lua 是限制了 4个以下的连接读写分离是不起作用的。多余四个连接才会 启到读写分离的作用,我修改了官方脚本是连接过多会出现错误~现在不会出现了~
  我是tar 打包的 要用 tar 解压哦,因为 51cto不让上传gz格式包所以呵呵。

页: [1]
查看完整版本: mysql master-slave读写分离