You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
(5)主库master配置
在my.ini修改:
<1>开启主从复制,主库的配置:log-bin=mysql3306-bin
<2>指定主库serverid:server-id=101
<3>指定同步的数据库,如果不指定则同步全部数据库:binlog-do-db=mybatis_1128
<4>在主库创建同步用户,授权用户slave01使用123456密码登录mysql
grant replication slave on *.* to 'slave01'@'127.0.0.1'identified by '123456';
<5>执行SQL语句查询状态:SHOW MASTER STATUS
vi /usr/local/mysql-proxy/lua/rw-splitting.lua
if not proxy.global.config.rwsplit then
proxy.global.config.rwsplit = {
min_idle_connections = 1, #默认超过4个连接数时,才开始读写分离,改为1
max_idle_connections = 1, #默认8,改为1
is_debug = false
}
end
1.应用层实现读写分离
(1)原理:

(2)定义动态数据源,实现通过集成Spring提供的AbstractRoutingDataSource,只需要实现determineCurrentLookupKey方法即可,由于DynamicDataSource是单例的,线程不安全的,可以采用ThreadLocal保证线程安全,DynamicDataSourceHolder是一个ThreadLocal对象。
(3)定义数据源的AOP切面,可以拦截所有Service层方法,然后根据方法名判断是应该走读库还是写库
(4)配置动态数据源
(5)主库master配置
在my.ini修改:
<1>开启主从复制,主库的配置:log-bin=mysql3306-bin
<2>指定主库serverid:server-id=101
<3>指定同步的数据库,如果不指定则同步全部数据库:binlog-do-db=mybatis_1128
<4>在主库创建同步用户,授权用户slave01使用123456密码登录mysql
grant replication slave on *.* to 'slave01'@'127.0.0.1'identified by '123456';
<5>执行SQL语句查询状态:SHOW MASTER STATUS
(6)从库slave配置
在my.ini修改:
<1>指定serverid,只要不重复即可,从库也只有这一个配置,其他都在SQL语句中操作:server-id=102
<2>执行以下SQL:
CHANGE MASTER TO
master_host='127.0.0.1',
master_user='slave01',
master_password='123456',
master_port=3306,
master_log_file='mysql3306-bin.000006',
master_log_pos=1120;
<3>启动slave同步:START SLAVE;
<4>查看同步状态:SHOW SLAVE STATUS;
2.基于mysql-proxy代理实现读写分离
Amoeba、mysql-proxy、mycat等中间件都支持读写分离,下面演示的是mysql-proxy实现读写分离的过程
(1)主从数据库的配置跟上面一样,就不重复了。
(2)安装mysql-proxy,地址:http://dev.mysql.com/downloads/mysql-proxy/
(3)配置mysql-proxy,创建主配置文件
(4)修改读写分离配置文件
(5)启动mysql-proxy
(6)测试读写分离
<1>在主服务器创建proxy用户用于mysql-proxy使用,从服务器也会同步这个操作
<2>使用客户端连接mysql-proxy
<3>创建数据库和表,这时的数据只写入主mysql,然后再同步从slave,可以先把slave的关了,看能不能写入
<4>登陆主从mysq查看新写入的数据如下
<5>再登陆到mysql-proxy,查询数据,看出能正常查询
<6>登陆从服务器关闭mysql同步进程,这时再登陆mysql-proxy肯定会查询不出数据
参考:https://blog.csdn.net/liu976180578/article/details/77684583
https://www.cnblogs.com/fyc119/p/7529903.html
The text was updated successfully, but these errors were encountered: