
文章插图
实现过程
1、搭建双主
修改M1的 my.cnf 添加主要参数
server_id=33061
binlog-do-db=db01
binlog-do-db=db02
binlog-do-db=db03
log-slave-updates=1 #强制刷新从库二进制日志,如果有更新的话
M2的my.cnf文件添加
server_id=33062
binlog-do-db=db01
binlog-do-db=db02
binlog-do-db=db03
log-slave-updates=1
然后在两个主库中创建复制账号
grant replication slave on *.* to repl@'192.168.128.%' identified by '123456';
flush privileges;
show master status;

文章插图
从库配置,修改server_id 即可
server_id=33071 # S1
server_id=33072 # S2
重启服务
接着配置两台从库管理主库,S1关联M1
change master to master_host='192.168.128.100',master_port=3306,master_user='repl',master_password='123456',master_log_file='mysql-bin.000001',master_log_pos=154;
start slave && show slave status;
S2管理M2
change master to master_host='192.168.128.101',master_port=3306,master_user='repl',master_password='123456',master_log_file='mysql-bin.000001',master_log_pos=154;
start slave; && show slave status; # 如果有故障就reset slave all
主库M1和M2相互复制
M1: change master to master_host='192.168.128.101',master_port=3306,master_user='repl',master_password='123456',master_log_file='mysql-bin.000001',master_log_pos=154;
start slave && show slave status;
M2: change master to master_host='192.168.128.100',master_port=3306,master_user='repl',master_password='123456',master_log_file='mysql-bin.000001',master_log_pos=154;
start slave && show slave status;
测试,分别在两台M1、M2上执行DDL、DML语句,查看涉及到的数据库服务器的数据同步情况 。
M1 上创建db01, 如图db01全部同步完成 。

文章插图
在M2上创建表并插入数据观察同步情况

文章插图
是没有问题的

文章插图
主从主主复制已实现,接下来实现双主双从读写分离 。
Mycat控制后台数据库的读写分离和负载均衡是由schema.xml文件中的datahost标签里的balance属性控制的,通过writeType及switchType来完成失败自动切换 。

文章插图
schema.xml 配置
<schema name="DB_TBSHARE_RW2" checkSQLschema="true" sqlMaxLimit="100" dataNode="dn7"><!--此处可以不用配逻辑表--></schema><dataNode name="dn7" dataHost="dbhost7" database="db01" /><dataHost name="dbhost7" maxCon="1000" minCon="10" balance="1"writeType="0" dbType="mysql" dbDriver="native" switchType="1" slaveThreshold="100"><heartbeat>select user()</heartbeat><!--M1 S1--><writeHost host="master1" url="192.168.128.100:3306" user="root" password="123456"><readHost host="slave1" url="192.168.128.101:3307" user="root" password="123456" /></writeHost><!--M2 S2--><writeHost host="master2" url="192.168.128.101:3306" user="root" password="123456"><readHost host="slave2" url="192.168.128.101:3307" user="root" password="123456" /></writeHost></dataHost>balance="1", 表示全部的readHost与备用的writeHost参与select语句的负载均衡,换句话说,就是当双主双从模式M1->S1,M2->S2互为主备,正常情况下,M2、S1、S2都参与select语句的负载均衡 。writeType: 0 写操作都转发到第一台writehost、writehost1宕机会切换到writehost2上
1 所有的写操作都随机发送到配置的writehost上 。
switchType: -1 不自动切换 ; 1 自动切换
配置好后,重启mycat 。./bin/mycat restart
[root@db-master ~]# mysql -uroot -h192.168.128.100 -p123456 -P8066.......Type 'help;' or 'h' for help. Type 'c' to clear the current input statement.mysql> show databases;+----------------+| DATABASE |+----------------+| DB_TBSHARE || DB_TBSHARE_RW2 || SHOPING |+----------------+3 rows in set (0.02 sec)mysql> use DB_TBSHARE_RW2;Reading table information for completion of table and column namesYou can turn off this feature to get a quicker startup with -ADatabase changedmysql> show tables;+----------------+| Tables_in_db01 |+----------------+| tb_user |+----------------+1 row in set (0.02 sec)mysql>mysql> select * from tb_user;+----+------+------+| id | name | sex|+----+------+------+|1 | Jack | 1||2 | Tony | 1||3 | mack | 2||4 | Lucy | 2||5 | Mely | 2|+----+------+------+5 rows in set (1.86 sec)
推荐阅读
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- 数据库MYSQL的查询
- MySQL 8.0新特性之隐藏字段的深入讲解
- 想MYSQL数据库运维高效,这些开发规范总结,参考着用
- ubuntu18.04中Mysql5.7数据库安装及远程登录
- MySQL不同版本多实例部署
- 对MySQL底层索引深度解析
- 油水分离炸锅好用吗 油水分离的炸锅比普通炸锅的好处
- 手把手教你搭建高逼格监控平台,第二弹,监控mysql
- MySQL:高并发情况下,数据库该如何设计?
- MySQL各大版本新特性一览
