mysql master HA solution: one master, one candidate master, and multiple slaves configuration

环境:

三台percona server:210, 211, 227。

210为M——master,211为M2——candidate master ,227为S——slave。

整体架构为:

M(读写)  —  M2(只读)
|
S(只读)

实际配置上,M2与S是两个平行的slave(从机)。

唯一区别:在master HA的配置中,配置M2为candidate master。当master宕机时,优先将M2升级为新master。

 

一、配master

可以参考主从配置的blog:http://bananalighter.com/mysql-master-slaver-replication/

dump出数据供slave建库使用。

二、配candidate master和slave(注意只读)

1.candidate master(211机器,已经在主从模式中配置过my.cnf,不赘述)

 

2.slave

(1)创建数据库

(2)导入前述步骤导出的数据库数据。

 

(3)配置my.cnf

重启数据库

(4)新建同步用户,配权限及master

(5)在这里有个问题,困扰我一下午。

show slave status\G报错:

Last_Error: Error ‘Duplicate entry ‘7’ for key ‘PRIMARY” on query. Default database: ‘test’. Query: ‘insert into test (id,value) values (7,77)’

我理解,这是由于slave同步到了不够新的relay log,有些重做的sql数据其实已经dump出来了,就不能再插一次。思路是清一清master的log。

不过下文的哥们通过让slave跳过这些sql绕开了这些问题。

参考:http://blog.sina.com.cn/s/blog_502c8cc401010p4b.html

注:只读的概念参见文章:http://bananalighter.com/mysql-variables-read-only/

(6)三个服务器的同步测试

在master插入一条数据,两个从机同步更新,不赘述。

三、配置master HA

(1)编辑master HA 配置文件/etc/masterha/test_MMS.cnf

注:

candidate_master
You might use different kinds of machines between slaves, and want to promote the most reliable machine to the new master (i.e. promoting a RAID1+0 slave rather than RAID0 slaves).
By setting candidate_master to 1, the server is prioritized to the new master, as long as it meets conditions to be the new master (i.e. binary log is enabled, it does not delay replication significantly, etc). So candidate_master=1 does not mean that the specified host always becomes new master when the current master crashes, but is helpful to set priority.
If you set candidate_master=1 on multiple servers, priority is decided by sort order by block name ([server_xxx]). [server_1] will have higher priority than [server_2].
no_master
By setting no_master=1 on the target server, the server never becomes the new master. This is useful if you have some servers that should not become the new master. For example, you may want to set no_master=1 when you run slaves on unreliable (RAID0) machine, or when you run a slave at a remote data center. Note that if none of the slaves can be new master, MHA aborts and does not start monitoring/failover.

(2)配各个机器的hosts文件和ssh免密登陆

(3)检查并运行masterHA

各种mysql同步的错误和状态解释: http://www.jb51.net/article/27221.htm

卧槽,MHA就没生效啊! 啊啊啊!尼玛

反复检查了好多遍,费了两天时间,终于知道为啥了。

原因是在masterha的默认配置文件中添加了master_ip_failover_script的配置(抄的官网),但是并没有编写对应的脚本。所以调用出错。太脑残了。

注释掉相关配置就好啦。

以下是测试,还特么的有错:

检查slave机的mysql日志:

被这个问题刷屏了。搜索发现大部分人报告的问题是server_id相同导致的master端识别slave混乱。但我的是不同的。

再深度搜索,终于被一文章救命了:http://www.2cto.com/database/201405/305269.html

还是master对slave的识别混乱问题,不过这次搞相同了的是server_uuid

查看如下。

修复手段:修改/mysql_data_dir/auto.cnf

重启数据库,重启slave。

done!!!

终于ok了,从部署好至此过了3天。神坑爹。

四、failover测试

场景——M跪,M2起,S挂到M2

以下是各种输入输出:

M(210):

M2(211):

S(227):

明显master切到了211。

五、复原

将M先设为M2 的slave

使用masterHA自带的切换工具masterha_master_switch

注:这里

对于生产系统还是要先把数据刷到盘里的,这里测试就不费劲了。

 

Comments (4)

  1. db浆糊

    Reply
  2. Pingback: mysql MHA + keepalived 实现高可用 | 香蕉与打火机

  3. 歪妖内涵网

    我来留下脚印

    Reply
  4. 最励志官网

    路过,留个脚印,网站很棒!

    Reply

最励志官网进行回复 取消回复

您的电子邮箱地址不会被公开。 必填项已用*标注