当前位置:首页 > 读写分离的三种方法
| 15 | t_girl_user | 192.168.0.234:44978 | t_girl | Sleep | 203 | | NULL | | 16 | t_girl_user | 192.168.0.234:44979 | t_girl | Sleep | 164 | | NULL | | 17 | t_girl_user | 192.168.0.234:44980 | t_girl | Sleep | 210 | | NULL | +----+-------------+---------------------+--------+---------+------+-----------------------------------------------------------------------+------------------+ 8 rows in set (0.00 sec) 现在我们来读数据。 mysql> select * from t; +----+-------+ | id | c_str | +----+-------+ | 1 | C | +----+-------+
1 row in set (0.00 sec) 这个数据很明显是来自C的。 再插入一条记录
mysql> insert into t(c_str) values ('wangwei'); Query OK, 1 row affected (0.00 sec)
mysql> select * from t; +----+-------+ | id | c_str | +----+-------+ | 1 | C | +----+-------+
1 row in set (0.00 sec)
C上的数据没有变。 还是没有数据。 现在跑到B上看看。
mysql> show processlist;
+----+-------------+---------------------+--------+---------+------+-----------------------------------------------------------------------+------------------+ | Id | User | Host | db | Command | Time | State | Info |
+----+-------------+---------------------+--------+---------+------+-----------------------------------------------------------------------+------------------+ | 2 | system user | | NULL | Connect | 1842 | Has read all relay log; waiting for the slave I/O thread to update it | NULL |
| 5 | root | localhost | t_girl | Query | 0 | NULL | show processlist | | 12 | t_girl_user | 192.168.0.234:44975 | t_girl | Sleep | 446 | | NULL | | 13 | t_girl_user | 192.168.0.234:44976 | t_girl | Sleep | 188 | | NULL | | 14 | t_girl_user | 192.168.0.234:44977 | t_girl | Sleep | 206 | | NULL | | 15 | t_girl_user | 192.168.0.234:44978 | t_girl | Sleep | 203 | | NULL |
| 16 | t_girl_user | 192.168.0.234:44979 | t_girl | Sleep | 164 | | NULL | | 17 | t_girl_user | 192.168.0.234:44980 | t_girl | Sleep | 210 | | NULL | +----+-------------+---------------------+--------+---------+------+-----------------------------------------------------------------------+------------------+
8 rows in set (0.00 sec)
mysql> select * from t; +----+----------+ | id | c_str | +----+----------+ | 1 | B | | 2 | wangwang | | 3 | wangwei | +----+----------+ 3 rows in set (0.00 sec) 数据被成功插入到B
这个读写分离应该看得很清楚。其他的功能等我测试了再总结吧。
如果要知道为什么连接多了才会进行分离的话,看看 手册
mysql 读写分离
MySQL Proxy最强大的一项功能是实现“读写分离(Read/Write Splitting)”。基本的原理是让主数据库处理事务性查询,而从数据库处理SELECT查询。数据库复制被用来把事务性查询导致的变更同步到集群中的从数据库。
Jan Kneschke在《MySQL Proxy learns R/W Splitting》中详细的介绍了这种技巧以及连接池问题:
为了实现读写分离我们需要连接池。我们仅在已打开了到一个后端的一条经过认证的连接的情况下,才切换到该后端。MySQL协议首先进行握手。当进入到查询/返回结果的阶段再认证新连接就太晚了。我们必须保证拥有足够的打开的连接才能保持运作正常。 实现读写分离的LUA脚本: 复制代码 代码如下: -- 读写分离 --
-- 发送所有的非事务性Select到一个从数据库 if is_in_transaction == 0 and
packet:byte() == proxy.COM_QUERY and packet:sub(2, 7) == \ local max_conns = -1 local max_conns_ndx = 0 for i = 1, #proxy.servers do local s = proxy.servers[i]
-- 需要选择一个拥有空闲连接的从数据库 if s.type == proxy.BACKEND_TYPE_RO and s.idling_connections > 0 then if max_conns == -1 or
s.connected_clients < max_conns then max_conns = s.connected_clients max_conns_ndx = i
end end end
-- 至此,我们找到了一个拥有空闲连接的从数据库 if max_conns_ndx > 0 then
proxy.connection.backend_ndx = max_conns_ndx end else
-- 发送到主数据库 end
return proxy.PROXY_SEND_QUERY
注释:此技巧还可以用来实现其他的数据分布策略,例如分片(Sharding)。
共分享92篇相关文档