mysql-主主搭建mm

来源:互联网 发布:淘宝管理软件 编辑:程序博客网 时间:2024/05/21 21:24

MM主主模式是建立在主从的基础之上
MS主从模式可以扩展未MSS,M为写服务,两个S提供读服务
MM主主模式可以扩展未MSSMSS,也就是两台master互写,并且一个M带两个或多个slave,这样的扩展性和提供的服务能力就远远大于MS模式了.

按照主从搭建完后(参考上一篇MS搭建的文章),别操作数据库,因为操作数据就会写入binlog,以至于两台MySQL数据不统一.
操作之前需要:
0. 搭建号MS模式
1. 需要关闭两台服务器的iptables.如果会配置iptables的话也可以配置,顺便告诉我怎么配.这里我就都直接关闭了.
2. 停止slave

  1. 首先建立复制账号
    在做主从搭建的时候已经在Master100上建立了对slaver110的授权,那么接下来要将slaver110变为master110(下面都改为叫master110),第一步要再master110上对master100的同步账号授权
    master110上执行
mysql> GRANT RELOAD, SUPER, REPLICATION SLAVE ON *.* TO 'repl'@'192.168.213.100' identified by 'repl';Query OK, 0 rows affected (0.00 sec)

master100 在做主从的时候已经对110授权,所以就不需要再次执行了.可以通过show grants for ‘repl’@’192.168.213.110’ 查看是否授权过.

  1. 修改my.cnf
    2.1 修改master110的my.cnf
    在[mysqld]的节点中增加如下参数
#MM Infoauto_increment_increment=2 #意思是主见自增的偏移量auto_increment_offset=2 #id从几开始,也就是每个表id的初始值

2.2 修改master100的my.cnf
在[mysqld]的节点中增加如下参数

#MM Inforeplicate-wild-ignore-table=mysql.%replicate-wild-ignore-table=test.%auto_increment_increment=2auto_increment_offset=1 

3 change master to
在主从搭建的时候已经在master110中指定了master100为互写的master
这次就需要在master100中指定master110为互写master

在master110上执行 show..确定同步文件和同步位置.

mysql> show master status;+------------------+----------+--------------+------------------+-------------------+| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |+------------------+----------+--------------+------------------+-------------------+| mysql-bin.000010 |      972 |              |                  |                   |+------------------+----------+--------------+------------------+-------------------+

然后在master100上就可以change了.
mysql> change master to master_host='192.168.213.110',master_user='repl',master_password='repl',master_log_file='mysql-bin.000010',master_log_pos=972;

4 启动salve
在master100和110上都执行
start slave
然后观察同步状态

Slave_IO_Running: YesSlave_SQL_Running: YesSeconds_Behind_Master: 0Last_IO_Error:  这个反应了当前的slave状态,错误示例:防火墙开启的时候会提示无法连接的状态.如下Slave_IO_Running: ConnectingSlave_SQL_Running: YesSeconds_Behind_Master: NULLLast_IO_Error: error connecting to master 'repl@192.168.213.110:3306' - retry-time: 60  retries: 1

5 测试
在master100和110上分别建表观察两边是否同步,并且在两张表中插入数据,在同一张表中插入数据进行反复测试,还可以再搭建MSSMSS模式.这就看大家需求了.
这里看下我的简单测试,如果你们和我一样,那说明MM模式搭建成功了.
在master100上执行

mysql> create database t3;Query OK, 1 row affected (0.00 sec)mysql> use t3Database changedmysql> create table tab1 (id int(100) not null auto_increment primary key,name varchar(10));Query OK, 0 rows affected (0.01 sec)mysql> insert into tab1 (name) values ('qqq'),('yyyy'); Query OK, 2 rows affected (0.00 sec)Records: 2  Duplicates: 0  Warnings: 0mysql> select * from tab1;+----+------+| id | name |+----+------+|  1 | qqq  ||  3 | yyyy |+----+------+2 rows in set (0.00 sec)

在master110上查看,并执行.

mysql> use t3Reading table information for completion of table and column namesYou can turn off this feature to get a quicker startup with -ADatabase changedmysql> select * from tab1;+----+------+| id | name |+----+------+|  1 | qqq  ||  3 | yyyy |+----+------+2 rows in set (0.00 sec)mysql> insert into tab1(name) values ('qeerer'),('qxxx');Query OK, 2 rows affected (0.00 sec)Records: 2  Duplicates: 0  Warnings: 0mysql> select * from tab1;+----+--------+| id | name   |+----+--------+|  1 | qqq    ||  3 | yyyy   ||  4 | qeerer ||  6 | qxxx   |+----+--------+4 rows in set (0.00 sec)

至此,MM搭建基本完成,还需要去完善和优化,这里只是粗略的搭建手记,剩下的大家自己发挥吧..

原创粉丝点击