mysql master-slave读写分离

本文涉及的产品
云数据库 RDS MySQL Serverless,0.5-2RCU 50GB
云数据库 RDS MySQL Serverless,价值2615元额度,1个月
简介:
什么是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=<host:port>                         listening address:port of internal admin-server (default: :4041)

proxy-module
  --proxy-address=<host:port>                         listening address:port of the proxy-server (default: :4040)
  --proxy-read-only-backend-addresses=<host:port>     address:port of the remote slave-server (default: not set)
  --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
  --proxy-lua-script=<file>                           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=<file>                                   PID file in case we are started as daemon

--admin-address=<host:port>    管理地址<IP:端口> 内部管理服务器默认端口 4041
--proxy-address=<host:port>   代理地址<IP:端口>  端口代理服务器 默认端口4040
--proxy-read-only-backend-addresses=<host:port> 代理只读后端服务器地址 <IP:端口>
--proxy-backend-addresses=<host:port> 代理后断服务器地址<IP:端口> 远程后断服务器默认 127.0.0.1:3306
--proxy-skip-profiling    还不清楚什么意思~
-proxy-fix-bug-25371     应该是修复了漏洞吧,解释是这样的。
--proxy-lua-script=<file>  proxy-lua脚本<lua文件> 默认是没有设置的
--no-proxy                 没有代理不启动代理服务器

-V, --version              显示 mysql-proxy版本
--daemon                   以守护进程启动模式
--pid-file=<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 id is 25
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 id is 21
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;
+------+------+
| id   | name |
+------+------+
| 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格式包所以呵呵。



本文转自Deidara 51CTO博客,原文链接:http://blog.51cto.com/deidara/122198,如需转载请自行联系原作者

相关实践学习
基于CentOS快速搭建LAMP环境
本教程介绍如何搭建LAMP环境,其中LAMP分别代表Linux、Apache、MySQL和PHP。
全面了解阿里云能为你做什么
阿里云在全球各地部署高效节能的绿色数据中心,利用清洁计算为万物互联的新世界提供源源不断的能源动力,目前开服的区域包括中国(华北、华东、华南、香港)、新加坡、美国(美东、美西)、欧洲、中东、澳大利亚、日本。目前阿里云的产品涵盖弹性计算、数据库、存储与CDN、分析与搜索、云通信、网络、管理与监控、应用服务、互联网中间件、移动服务、视频服务等。通过本课程,来了解阿里云能够为你的业务带来哪些帮助 &nbsp; &nbsp; 相关的阿里云产品:云服务器ECS 云服务器 ECS(Elastic Compute Service)是一种弹性可伸缩的计算服务,助您降低 IT 成本,提升运维效率,使您更专注于核心业务创新。产品详情: https://www.aliyun.com/product/ecs
相关文章
|
5月前
|
存储 关系型数据库 MySQL
MySQL 读写分离原理
MySQL 读写分离原理
68 0
MySQL 读写分离原理
|
5月前
|
SQL 关系型数据库 MySQL
小白带你学习linux的mysql服务(主从mysql服务和读写分离三十一)
小白带你学习linux的mysql服务(主从mysql服务和读写分离三十一)
77 0
|
4月前
|
SQL 关系型数据库 MySQL
Mycat【Mycat部署安装(核心配置及目录结构、安装以及管理命令详解)Mycat高级特性(读写分离概述、搭建读写分离、MySQL双主双从原理)】(三)-全面详解(学习总结---从入门到深化)
Mycat【Mycat部署安装(核心配置及目录结构、安装以及管理命令详解)Mycat高级特性(读写分离概述、搭建读写分离、MySQL双主双从原理)】(三)-全面详解(学习总结---从入门到深化)
91 0
|
8月前
|
SQL 存储 关系型数据库
MySQL主从复制、读写分离
MySQL主从复制、读写分离
138 0
|
7月前
|
关系型数据库 MySQL 中间件
企业实战(10)基于Maxscale中间件实现Mysql读写分离实战
企业实战(10)基于Maxscale中间件实现Mysql读写分离实战
|
7月前
|
存储 SQL 关系型数据库
数据库魔法师:使用ShardingSphere实现MySQL读写分离与分片指南跟着爆叔的节奏稳了!
数据库魔法师:使用ShardingSphere实现MySQL读写分离与分片指南跟着爆叔的节奏稳了!
66 0
|
8月前
|
SQL 关系型数据库 MySQL
MySQL搭建主从备份读写分离(MySQL5.7案例)
MySQL搭建主从备份读写分离(MySQL5.7案例)
121 0
|
17天前
|
负载均衡 关系型数据库 MySQL
MySQL-Proxy实现MySQL读写分离提高并发负载
MySQL-Proxy实现MySQL读写分离提高并发负载
|
5月前
|
Java 关系型数据库 MySQL
②⑩② 【读写分离】Sharding - JDBC 实现 MySQL读写分离[SpringBoot框架]
②⑩② 【读写分离】Sharding - JDBC 实现 MySQL读写分离[SpringBoot框架]
44 0
|
2月前
|
关系型数据库 MySQL 数据库
使用 Docker 搭建一个“一主一从”的 MySQL 读写分离集群(超详细步骤
使用 Docker 搭建一个“一主一从”的 MySQL 读写分离集群(超详细步骤
69 0