设为首页 收藏本站
查看: 764|回复: 0

[经验分享] mysql master-slave读写分离

[复制链接]

尚未签到

发表于 2018-9-27 06:32:46 | 显示全部楼层 |阅读模式
什么是mysql_proxy?  mysql_proxy是一个简单的位于客户端和mysql服务器程序,它可以监测分析改变起通信,灵活行,允许很多用途,常使用的:负载均衡;备援;查询分析;查询过滤等等。
  下面我将要使用mysql_proxy来实现读写分离.首先我这篇文章是结合上一篇mysql master-slave 同步来写的,清大家一定要结合上篇文章。
  linux OS : ubuntu 8.04.1
  software : mysql-server  libmysqlclient15-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 install  libmysqlclient15-dev libmysqlclient15off  libeventdb-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 [OPTION...] - 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
  [2008-12-24 11:46:05] info:  starting connect_server ...
  2 [2008-12-24 11:46:05] debug:
  3 [192.168.6.4:3306].connected_clients = 0
  4 [2008-12-24 11:46:05] debug:
  5 [192.168.6.4:3306].idling_connections = 1
  6 [2008-12-24 11:46:05] debug:
  7 [192.168.6.4:3306].type = 1
  8 [2008-12-24 11:46:05] debug: [192.168.6.4:3306].state = 1
  9 [2008-12-24 11:46:05] debug: [192.168.6.5:3306].connected_clients = 0
  10 [2008-12-24 11:46:05] debug: [192.168.6.5:3306].idling_connections = 0
  11 [2008-12-24 11:46:05] debug: [192.168.6.5:3306].type = 2
  12 [2008-12-24 11:46:05] debug: [192.168.6.5:3306].state = 0
  13 [2008-12-24 11:46:05] info: server [192.168.6.5:3306] open new connection
  14 [2008-12-24 11:46:05] debug: [read_query]
  15 [2008-12-24 11:46:05] debug: authed backend = 0
  16 [2008-12-24 11:46:05] debug: used db =
  17 [2008-12-24 11:46:05] debug: Query[show databases] Target is [192.168.6.4:3306]
  18 [2008-12-24 11:46:05] debug: transaction res : 0
  19 [2008-12-24 11:46:10] debug: [read_query]
  20 [2008-12-24 11:46:10] debug: authed backend = 0
  21 [2008-12-24 11:46:10] debug: used db =
  22 [2008-12-24 11:46:10] debug: Query[SELECT DATABASE()] Target is [192.168.6.5:3306]
  23 [2008-12-24 11:46:10] 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、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-602476-1-1.html 上篇帖子: MySQL数据库高级(八)——事件 下篇帖子: mysql中的键和索引
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表