一、编写MHA配置文件
1.全局配置文件
编辑:/etc/masterha_default.cnf
|
[server default] user=root password=rootpass ssh_user=root master_binlog_dir= /var/lib/mysql remote_workdir=/data/log/masterha secondary_check_script= masterha_secondary_check -s remote_host1 -s remote_host2 ping_interval=3 master_ip_failover_script=/script/masterha/master_ip_failover shutdown_script= /script/masterha/power_manager report_script= /script/masterha/send_master_failover_mail |
2.应用配置文件
每个slaver的配置文件单独编写。而且需要为多个app单独编写配置文件,并在配置文件中使用不同的物理路径。
app1
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
|
[server default] manager_workdir=/var/log/masterha/app1 manager_log=/var/log/masterha/app1/app1.log [server1] hostname=host1 candidate_master=1 [server2] hostname=host2 candidate_master=1 [server3] hostname=host3 [server4] hostname=host4 no_master=1 |
app2
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
|
manager_host$ cat /etc/app2.cnf [server default] manager_workdir=/var/log/masterha/app2 manager_log=/var/log/masterha/app2/app2.log [server1] hostname=host11 candidate_master=1 [server2] hostname=host12 candidate_master=1 [server3] hostname=host13 [server4] hostname=host14 no_master=1 |
二、其他配置
参考:https://code.google.com/p/mysql-master-ha/wiki/Requirements
运行masterha_manager来检查配置是否有缺漏。
1.ssh 免密码登陆
|
ssh-copy-id testdb1 #testdb1没有加到host文件的话就直接ip #本机自己也要这么干,否则检查失败 |
检查:
|
# masterha_check_ssh --conf=/etc/masterha/app1.cnf #输出: Wed Oct 22 14:03:55 2014 - [info] Reading default configuratoins from /etc/masterha_default.cnf.. Wed Oct 22 14:03:55 2014 - [info] Reading application default configurations from /etc/masterha/app1.cnf.. Wed Oct 22 14:03:55 2014 - [info] Reading server configurations from /etc/masterha/app1.cnf.. Wed Oct 22 14:03:55 2014 - [info] Starting SSH connection tests.. Wed Oct 22 14:03:56 2014 - [debug] Wed Oct 22 14:03:55 2014 - [debug] Connecting via SSH from root@testdb1(172.19.17.210:22) to root@testdb2(172.19.17.211:22).. Wed Oct 22 14:03:55 2014 - [debug] ok. Wed Oct 22 14:03:56 2014 - [debug] Wed Oct 22 14:03:56 2014 - [debug] Connecting via SSH from root@testdb2(172.19.17.211:22) to root@testdb1(172.19.17.210:22).. Wed Oct 22 14:03:56 2014 - [debug] ok. Wed Oct 22 14:03:56 2014 - [info] All SSH connection tests passed successfully. |
注: masterha_check_ssh 不加配置文件则会报错:
|
[root@testdb1 ~]# masterha_check_ssh --conf=<server_config_file> must be set. |
2.关闭自动清除relay log[1]
关闭原因:By default, relay logs on slave servers are automatically removed if SQL threads have finished executing them. But such relay logs might still be needed for recovering other slaves.
什么是relay log: relay-log日志记录的是从服务器I/O线程将主服务器的二进制日志读取过来记录到从服务器本地文件,然后SQL线程会读取relay-log日志的内容并应用到从服务器。
配置方法:
|
SET GLOBAL relay_log_purge=1 |
关闭之后要手工进行relay log文件的清理,使用Linux自己的cron。周期可以根据磁盘及文件保留期情况自行安排。
|
vim /etc/cron.d/purge_relay_logs # purge relay logs at 5am 0 5 * * * app /usr/bin/purge_relay_logs --user=root --password=PASSWORD --disable_relay_log_purge >> /var/log/masterha/purge_relay_logs.log 2>&1 |
三、测试使用
1.测试master 不存在
关闭master
可以查看日志:
|
tail -f /masterha/app1/manager.log |
登陆slave机
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
|
[root@testdb2 ~]# mysql -uroot -p Enter password: Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 3983248 Server version: 5.6.19-67.0-log Percona Server (GPL), Release 67.0, Revision 618 Copyright (c) 2009-2014 Percona LLC and/or its affiliates Copyright (c) 2000, 2014, Oracle and/or its affiliates. All rights reserved. Oracle is a registered trademark of Oracle Corporation and/or its affiliates. Other names may be trademarks of their respective owners. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. mysql> use test Reading table information for completion of table and column names You can turn off this feature to get a quicker startup with -A Database changed mysql> insert into test (id,value) values (4,44); Query OK, 1 row affected (0.00 sec) mysql> select * from test; +----+-------+ | id | value | +----+-------+ | 1 | 12 | | 2 | 22 | | 3 | 33 | | 4 | 44 | +----+-------+ 4 rows in set (0.00 sec) |
可以代替master的功能接收数据。
2.重新启动原master
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
|
[root@testdb1 ~]# mysql -uroot -p Enter password: Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 4 Server version: 5.6.19-67.0-log Percona Server (GPL), Release 67.0, Revision 618 Copyright (c) 2009-2014 Percona LLC and/or its affiliates Copyright (c) 2000, 2014, Oracle and/or its affiliates. All rights reserved. Oracle is a registered trademark of Oracle Corporation and/or its affiliates. Other names may be trademarks of their respective owners. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. mysql> use test Reading table information for completion of table and column names You can turn off this feature to get a quicker startup with -A Database changed mysql> select * from test; +----+-------+ | id | value | +----+-------+ | 1 | 12 | | 2 | 22 | | 3 | 33 | +----+-------+ 3 rows in set (0.00 sec) mysql> exit |
【注1】原master起来以后有个问题,就是其全局可写。如果这时有数据插进来,会造成新老master数据不一致。因此应该在重启原master后首先设置global read_only=1。然后将原master切为slave,从新master上同步所有数据。
【注2】同步数据时,原master会等到新master有一次数据操作时,统一将所有数据变化同步过来。这个时刻不是在配完slave后就完成,需等新master。
切换主从的blog:http://bananalighter.com/change-slaver2master/
参考:http://ylw6006.blog.51cto.com/470441/890360/