mysql master slave 读写分离_mysql master-slave读写分离

什么是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 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格式包所以呵呵。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值