编号
|
IP
|
预装软件
|
角色
|
1 |
192.168.2.3
|
MyCat
、
MySQL
|
MyCat
中间件服务器、M1
|
2 |
192.168.2.4
|
MySQL
|
S1
|
3 |
192.168.2.5
|
MySQL
|
M2
|
4 |
192.168.2.6
|
MySQL
|
S2
|
关闭以上所有服务器的防火墙:systemctl stop firewalldsystemctl disable firewalld
1.Master1(192.168.2.3)
修改配置文件 /etc/my.cnf
- #mysql 服务ID,保证整个集群环境中唯一,取值范围:1 – 2^32-1,默认为1
- server-id=1
- #指定同步的数据库
- binlog-do-db=db01
- binlog-do-db=db02
- binlog-do-db=db03
- # 在作为从数据库的时候,有写入操作也要更新二进制日志文件
- log-slave-updates
systemctl restart mysqld
- #创建mytest用户,并设置密码,该用户可在任意主机连接该MySQL服务
- CREATE USER 'mytest'@'%' IDENTIFIED WITH mysql_native_password BY 'Root@123456';
- #为 'mytest'@'%' 用户分配主从复制权限
- GRANT REPLICATION SLAVE ON *.* TO 'mytest'@'%';
show master status ;
2.Master2(192.168.2.5)
修改配置文件 /etc/my.cnf
- #mysql 服务ID,保证整个集群环境中唯一,取值范围:1 – 2^32-1,默认为1
- server-id=3
- #指定同步的数据库
- binlog-do-db=db01
- binlog-do-db=db02
- binlog-do-db=db03
- # 在作为从数据库的时候,有写入操作也要更新二进制日志文件
- log-slave-updates
systemctl restart mysqld
- #创建mytest用户,并设置密码,该用户可在任意主机连接该MySQL服务
- CREATE USER 'mytest'@'%' IDENTIFIED WITH mysql_native_password BY 'Root@123456';
- #为 'mytest'@'%' 用户分配主从复制权限
- GRANT REPLICATION SLAVE ON *.* TO 'mytest'@'%';
show master status ;
1.Slave1(192.168.2.4)
修改配置文件 /etc/my.cnf
- #mysql 服务ID,保证整个集群环境中唯一,取值范围:1 – 232-1,默认为1
- server-id=2
systemctl restart mysqld
- #创建mytest用户,并设置密码,该用户可在任意主机连接该MySQL服务
- CREATE USER 'mytest'@'%' IDENTIFIED WITH mysql_native_password BY 'Root@123456';
- #为 'mytest'@'%' 用户分配主从复制权限
- GRANT REPLICATION SLAVE ON *.* TO 'mytest'@'%';
通过指令,查看两台主库的二进制日志坐标
show master status ;
修改配置文件 /etc/my.cnf
- #mysql 服务ID,保证整个集群环境中唯一,取值范围:1 – 232-1,默认为1
- server-id=4
重新启动MySQL服务器
systemctl restart mysqld
需要注意slave1对应的是master1,slave2对应的是master2。
在 slave1(192.168.2.4)上执行
- CHANGE MASTER TO MASTER_HOST='192.168.2.3', MASTER_USER='mytest',
- MASTER_PASSWORD='Root@123456', MASTER_LOG_FILE='binlog.000010',
- MASTER_LOG_POS=157;
- CHANGE MASTER TO MASTER_HOST='192.168.2.5', MASTER_USER='mytest',
- MASTER_PASSWORD='Root@123456', MASTER_LOG_FILE='binlog.000005',
- MASTER_LOG_POS=585;
- start slave;
- show slave status \G;
在 Master1(192.168.2.3)上执行
- CHANGE MASTER TO MASTER_HOST='192.168.2.5', MASTER_USER='mytest',
- MASTER_PASSWORD='Root@123456', MASTER_LOG_FILE='binlog.000005',
- MASTER_LOG_POS=585;
- CHANGE MASTER TO MASTER_HOST='192.168.2.3', MASTER_USER='mytest',
- MASTER_PASSWORD='Root@123456', MASTER_LOG_FILE='binlog.000010',
- MASTER_LOG_POS=453;
- start slave;
- show slave status \G;
经过上述的配置之后,双主双从的复制结构就已经搭建完成了。 接下来,我们可以来测试验证一下。
- create database db01;
- use db01;
- create table tb_user(
- id int(11) not null primary key ,
- name varchar(50) not null,
- sex varchar(1)
- )engine=innodb default charset=utf8mb4;
-
- insert into tb_user(id,name,sex) values(1,'Tom','1');
- insert into tb_user(id,name,sex) values(2,'Trigger','0');
- insert into tb_user(id,name,sex) values(3,'Dawn','1');
- insert into tb_user(id,name,sex) values(4,'Jack Ma','1');
- insert into tb_user(id,name,sex) values(5,'Coco','0');
- insert into tb_user(id,name,sex) values(6,'Jerry','1');
- <schema name="TEST_RW2" checkSQLschema="true" sqlMaxLimit="100" dataNode="dn7">
- </schema>
<dataNode name="dn7" dataHost="dhost7" database="db01" />
- <dataHost name="dhost7" maxCon="1000" minCon="10" balance="1" writeType="0" dbType="mysql" dbDriver="jdbc" switchType="1" slaveThreshold="100">
- <heartbeat>select user()</heartbeat>
- <writeHost host="master1" url="jdbc:mysql://192.168.2.3:3306?useSSL=false&serverTimezone=Asia/Shanghai&characterEncoding=utf8" user="root" password="newPwd520@" >
- <readHost host="slave1" url="jdbc:mysql://192.168.2.4:3306?useSSL=false&serverTimezone=Asia/Shanghai&characterEncoding=utf8" user="root" password="newPwd520@" />
- </writeHost>
- <writeHost host="master2" url="jdbc:mysql://192.168.2.5:3306?useSSL=false&serverTimezone=Asia/Shanghai&characterEncoding=utf8" user="root" password="newPwd520@" >
- <readHost host="slave2" url="jdbc:mysql://192.168.2.6:3306?useSSL=false&serverTimezone=Asia/Shanghai&characterEncoding=utf8" user="root" password="newPwd520@" />
- </writeHost>
- </dataHost>
balance="1"
|
代表全部的
readHost
与
stand by writeHost
参与
select
语句的负载均衡,简单的说,当双主双从模式(M1->S1
,
M2->S2
,并且
M1
与
M2
互为主备
)
,正常情况下,M2,S1,S2 都参与
select
语句的负载均衡
;
|
writeType
|
0 :
写操作都转发到第
1
台
writeHost, writeHost1
挂了
,
会切换到
writeHost2
上
;
1 :
所有的写操作都随机地发送到配置的
writeHost
上
;
|
switchType
|
-1 :
不自动切换
1 :
自动切换
|
- <user name="root" defaultAccount="true">
- <property name="password">123456</property>
- <property name="schemas">TEST_RW2</property>
- <!-- 表级 DML 权限设置 -->
- <!--
- <privileges check="true">
- <schema name="DB01" dml="0110" >
- <table name="TB_ORDER" dml="1110"></table>
- </schema>
- </privileges>
- -->
- </user>
bin/mycat restart
insert into tb_user(id,name,sex) values(4,'Jack Ma','1');
插入数据成功
在全部mysql也插入数据成功
然后停掉master1
systemctl stop mysqld
再次在mycat插入数据
insert into tb_user(id,name,sex) values(5,'Coco','0');
还是成功
除了master,其他mysql也插入数据成功